call code-behind method from markup
How do I call a code-behind method from my markup?
I want to call this method in the OnClick event as shown below: AddRow()
Code:
<asp:TemplateField>
<HeaderTemplate>
<asp:Button ID="AddButton" runat="server" Text="Add" OnClick='<%#AddRow()%>' />
</HeaderTemplate>
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:LinkButton ID="DeleteLinkButton" runat="server" CausesValidation="False" CommandArgument='<%# Container.DataItemIndex %>'
CommandName="Delete" Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
Re: call code-behind method from markup
just wondering why don't you use the button's click event:confused: just double click on the button and it will add the markup and the method in the code-behind
Re: call code-behind method from markup
because the button is in the header of a gridview and can't be 'double-clicked' unfortunately.
Re: call code-behind method from markup
ok then in your code-behind do this
vb Code:
Protected Sub AddRow_Click(ByVal sender As Object, ByVal e As System.EventArgs)
'do your stuff here
End Sub
in the markup
Code:
<asp:TemplateField>
<HeaderTemplate>
<asp:Button ID="AddButton" runat="server" Text="Add" OnClick="AddRow_Click" />
</HeaderTemplate>
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:LinkButton ID="DeleteLinkButton" runat="server" CausesValidation="False" CommandArgument='<%# Container.DataItemIndex %>'
CommandName="Delete" Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
Re: call code-behind method from markup
but just so you know it can be clicked too... i'm making screen shots for you
1 Attachment(s)
Re: call code-behind method from markup
look at the image i've made 3 steps... enjoy
Re: call code-behind method from markup
thanks for the image help...
it created the click event okay, and when i put a break on the event it is never hit when i click the button on the page.
any idea why?
Re: call code-behind method from markup
lets see code because i have no problem doing it
Re: call code-behind method from markup
Double-clicking the button created the code stub below. I put a breakpoint on Public.. but nothing when I click the button.
Code:
Public Sub AddNewRowButton_OnClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddNewRowButton.Click
Console.WriteLine()
End Sub
partial markup:
Code:
<asp:TemplateField>
<HeaderTemplate>
<asp:Button ID="AddNewRowButton" runat="server" Text="Add" />
</HeaderTemplate>
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:LinkButton ID="DeleteLinkButton" runat="server" CausesValidation="False" CommandArgument='<%# Container.DataItemIndex %>'
CommandName="Delete" Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
Re: call code-behind method from markup
no no no you can't do the Handles click part from within a template
vb Code:
Public Sub AddNewRowButton_OnClick(ByVal sender As Object, ByVal e As System.EventArgs)
Console.WriteLine()
End Sub
Code:
<asp:TemplateField>
<HeaderTemplate>
<asp:Button ID="AddNewRowButton" runat="server" Text="Add" OnClick="
AddNewRowButton_OnClick" />
</HeaderTemplate>
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:LinkButton ID="DeleteLinkButton" runat="server" CausesValidation="False" CommandArgument='<%# Container.DataItemIndex %>'
CommandName="Delete" Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
Re: call code-behind method from markup
i removed the Handles... nothing. Event still doesn't fire.
this is a killer.
Re: call code-behind method from markup
vbdotnetboy, thanks for all of your help though.
Re: call code-behind method from markup
look at the markup i posted.... notice the OnClick attribute
Re: call code-behind method from markup
Code:
<asp:TemplateField>
<HeaderTemplate>
<asp:Button ID="AddNewRowButton" runat="server" Text="Add" OnClick="
AddNewRowButton_OnClick" />
</HeaderTemplate>
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:LinkButton ID="DeleteLinkButton" runat="server" CausesValidation="False" CommandArgument='<%# Container.DataItemIndex %>'
CommandName="Delete" Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
Re: call code-behind method from markup
Yes, I put the OnClick method just like you have it and still the event doesn't fire.
I'm wondering if Masterpages has something to do with this since this page has master.
I just tried this same code at home, without master page, and the event fires just fine.
Re: call code-behind method from markup
Is that a gridview? If so, use the Gridview's RowCommand event. It handles all events bubbled up from inside the grid, and you will need to set a commandsource and commandname property for it.
Re: call code-behind method from markup
okay... i have finally got the event to fire.
It seems that if markup's <%#Eval()%> bindings cause errors for the gridview the event will not fire. I corrected my errors and now the event fires.
thanks for all of your help again.
Re: call code-behind method from markup
<%# %> is an evil curse placed within the domain of ASP.NET by Lucifer himself to lure naive programmers. Use the events provided with the gridview and you won't have problems. The event corresponding to that is the RowDataBound event.