Jun 15th, 2007, 02:27 PM
#1
Thread Starter
Fanatic Member
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>
Last edited by ZeBula8; Jun 22nd, 2007 at 06:49 PM .
Reason: RESOLVED
Jun 15th, 2007, 03:44 PM
#2
Re: call code-behind method from markup
just wondering why don't you use the button's click event just double click on the button and it will add the markup and the method in the code-behind
Jun 15th, 2007, 03:51 PM
#3
Thread Starter
Fanatic Member
Re: call code-behind method from markup
because the button is in the header of a gridview and can't be 'double-clicked' unfortunately.
Jun 15th, 2007, 03:58 PM
#4
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>
Jun 15th, 2007, 04:01 PM
#5
Re: call code-behind method from markup
but just so you know it can be clicked too... i'm making screen shots for you
Jun 15th, 2007, 04:04 PM
#6
Re: call code-behind method from markup
look at the image i've made 3 steps... enjoy
Attached Images
Jun 15th, 2007, 04:24 PM
#7
Thread Starter
Fanatic Member
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?
Jun 15th, 2007, 04:32 PM
#8
Re: call code-behind method from markup
lets see code because i have no problem doing it
Jun 15th, 2007, 04:37 PM
#9
Thread Starter
Fanatic Member
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>
Jun 15th, 2007, 04:39 PM
#10
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>
Jun 15th, 2007, 04:46 PM
#11
Thread Starter
Fanatic Member
Re: call code-behind method from markup
i removed the Handles... nothing. Event still doesn't fire.
this is a killer.
Jun 15th, 2007, 04:57 PM
#12
Thread Starter
Fanatic Member
Re: call code-behind method from markup
vbdotnetboy, thanks for all of your help though.
Jun 15th, 2007, 04:58 PM
#13
Re: call code-behind method from markup
look at the markup i posted.... notice the OnClick attribute
Jun 15th, 2007, 04:59 PM
#14
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>
Jun 15th, 2007, 10:14 PM
#15
Thread Starter
Fanatic Member
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.
Jun 19th, 2007, 10:01 AM
#16
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.
Jun 22nd, 2007, 06:40 PM
#17
Thread Starter
Fanatic Member
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.
Jun 25th, 2007, 08:56 AM
#18
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.
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width