https://i.stack.imgur.com/NYKn8.pngI want to call a function in ascx.vb file from .ascx page on the click of a link? Please help me. Any Refrence for the same ?
<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" Text="Edit" onclick="getDemo('abc')"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
----------------------Server Side -------------------------------
Function getDemo(ByVal x As String) As Boolean
Dim c As Int16
c = 9
End Function
I see as noted in comments as to why you can't just drap + drop in the link button (or even a nice regular styled asp.net button (no real reason to use a link button).
And might have been nice to point out that your button is to be in a gridview.
so, you can do it this way:
So, drop in a gridview - let wizard built it. Now blow out the data source on the page (don't need). Drag + drop a plane jane asp.net button onto the page (drop it outside of the gv). Now move the button inside the GV.
We now have this:
Ok, now our code to load the grid, looks like this: (always, but always always check the ispostback = false - first page grid load).
So, we have this:
Ok, and now we see/have this:
Ok, so now lets wire up the button click. It is JUST a plane jane asp.net button.
So we can't double click on the button to create the event stub for the button (its inside of the GV).
So, in the markup, start typing the event name in like this:
Note VERY close, you get a intel-sense dialog. In that above dialog, choose create new event. It will "seem" like nothing occured. We now see/have this:
Now, if we flip to code behind, you see the code event has been wired up for you. So, on that row click, lets get that row.
So you are 100% free to drop in a button, or link button (not really sure why you would use a link buttion here).
The "trick" or issue is how to wire up the click event - you have to type in the markup, since as noted, you can't just click on the button to create a event in the code behind, since it is inside of a grid.
but, as per above, you can still create the event by typing in the markup.
At that point, you have a regular plane jane button, and as the click event shows, you can grab the current GridViewRow by using btn.Parent.Parent.
At that point, you have all the information about that row - row index, the primary key.
NOTE VERY careful in above how I did NOT have to expose the PK id. I did not hide the row, or even EVER expose the database PK id's in that GV.
This nice feature is how/why the DataKeys setting exists. It lets you use/have/get/enjoy use of the database row PK's in the GV, but NOT have to ever expose the PK 's client side, nor do you even have to render the PK row id's in the GV. So, note how I setup DataKeys="ID". That's is a great feature since as noted, you can now use + enjoy and get the PK database ID, but NOT have to display, include, or even show or include the PK's in the GV. Needless to say, that is a great security feature, since in most cases one should not display nor include the database ID's in the client side (it is a big security risk).
The above process will work the same if you drop in a link button, but I can't see why you would when a plane jane regular button can (and should) be used here.
And you can do the same with a image button, or whatever you want. The main concept here is HOW to create the click event, and then in that code you can see how easy it is to get the grid view row.
Once you have the row, then you can get cells/values, or even as noted the row index. And with row index, then you can get the nice and wonderful database PK id that was not exposed anywhere in the client side GV.