在codebehind ASP.Net按钮调用codebehind功能 [英] ASP.Net Button in codebehind that calls codebehind function
问题描述
我使用Telerik的的RadControls,在我的codebehind我有以下的功能,其中一部分添加按钮页脚。
保护小组RadGrid1_ItemCreated(BYVAL发件人为对象,BYVAL E上GridItemEventArgs)
如果TypeOf运算e.Item是GridDataItem然后
昏暗editLink为超链接= DirectCast(e.Item.FindControl(EditLink),超链接)
editLink.Attributes(的href)=#
editLink.Attributes(onclick事件)= [字符串] .Format(回归ShowEditForm({0},{1}');,e.Item.OwnerTableView.DataKeyValues(e.Item.ItemIndex)( ID),e.Item.ItemIndex)
万一 ''按钮添加到网格的页脚
如果TypeOf运算e.Item是GridFooterItem然后
昏暗footerItem作为GridFooterItem = e.Item
科瑞票务按钮
昏暗BTN1作为新按钮()
btn1.Text =创建门票
btn1.Attributes.Add(RUNAT,服务器)
btn1.Attributes.Add(onclick事件,btnCreate_Click)
footerItem.Cells(2).Controls.Add(BTN1)
''显示所有门票按钮
昏暗BTN2作为新按钮()
btn2.Text =显示所有门票
btn2.Attributes.Add(RUNAT,服务器)
btn2.Attributes.Add(onclick事件,btnAll_Click)
footerItem.Cells(2).Controls.Add(BTN2)
万一 结束小组
随着这我有我的codebehind以下两个函数,我希望得到单击按钮时调用。
保护小组btnCreate_Click(BYVAL发件人为对象,BYVAL E上System.EventArgs)
的Response.Redirect(itrequest.aspx,假)
结束小组保护小组btnAll_Click(BYVAL发件人为对象,BYVAL E上System.EventArgs)
的Response.Redirect(itall_v2.aspx,假)
结束小组
我的问题是,这些功能都没有得到所谓的在呈现的页面。什么是困惑我的是,当我定义与标记相同的属性,这些按钮,它们工作得很好。我不明白有什么区别定义按钮标记VS code之间的后面。为什么没有这些功能充分利用我在后面的code定义按钮叫什么?是做的工作,而且我已经在我的标记用于测试目的注释掉的按钮如下:
<% - < ASP:表ID =表2=服务器HorizontalAlign =左>
< ASP:&的TableRow GT;
< ASP:TableCell的>
< BR />
< ASP:按钮的ID =Button1的=服务器文本=创建门票的OnClick =btnCreate_Click/>
< ASP:按钮的ID =Button2的=服务器文本=显示所有门票的OnClick =btnAll_Click/>
< / ASP:TableCell的>
< / ASP:&的TableRow GT;
< / ASP:表> - %GT;
nandokakimoto是正确的,但在VB的语法是:
的AddHandler btn1.Click,AddressOf btn1_Click
一个奇怪的语法,你即使AddHandler的似乎是一个功能不使用支架。
问候
I'm using Telerik RadControls, in my codebehind I have the following function, a portion of which adds buttons to the footer.
Protected Sub RadGrid1_ItemCreated(ByVal sender As Object, ByVal e As GridItemEventArgs)
If TypeOf e.Item Is GridDataItem Then
Dim editLink As HyperLink = DirectCast(e.Item.FindControl("EditLink"), HyperLink)
editLink.Attributes("href") = "#"
editLink.Attributes("onclick") = [String].Format("return ShowEditForm('{0}','{1}');", e.Item.OwnerTableView.DataKeyValues(e.Item.ItemIndex)("ID"), e.Item.ItemIndex)
End If
''Add buttons to footer of grid
If TypeOf e.Item Is GridFooterItem Then
Dim footerItem As GridFooterItem = e.Item
''Creat Ticket button
Dim btn1 As New Button()
btn1.Text = "Create Ticket"
btn1.Attributes.Add("runat", "server")
btn1.Attributes.Add("OnClick", "btnCreate_Click")
footerItem.Cells(2).Controls.Add(btn1)
''Show All Tickets button
Dim btn2 As New Button()
btn2.Text = "Show All Tickets"
btn2.Attributes.Add("runat", "server")
btn2.Attributes.Add("OnClick", "btnAll_Click")
footerItem.Cells(2).Controls.Add(btn2)
End If
End Sub
Along with this I have the following two functions in my codebehind that I wish to call when the buttons get clicked.
Protected Sub btnCreate_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Response.Redirect("itrequest.aspx", False)
End Sub
Protected Sub btnAll_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Response.Redirect("itall_v2.aspx", False)
End Sub
My problem is that these functions are not getting called in the rendered page. What's confusing me is that when I define these button with the same attributes in markup, they work fine. I don't understand what the difference is between defining the buttons in markup vs code behind. Why aren't these functions getting called from the buttons that I define in the code behind? The buttons that do work and that I have commented out in my markup for testing purposes are as follows.
<%--<asp:Table ID="Table2" runat="server" HorizontalAlign="Left">
<asp:TableRow>
<asp:TableCell>
<br />
<asp:Button ID="Button1" runat="server" Text="Create Ticket" OnClick="btnCreate_Click" />
<asp:Button ID="Button2" runat="server" Text="Show All Tickets" OnClick="btnAll_Click" />
</asp:TableCell>
</asp:TableRow>
</asp:Table>--%>
nandokakimoto is correct, but the syntax in VB is:
AddHandler btn1.Click, AddressOf btn1_Click
A strange syntax where you don't use brackets even though AddHandler appears to be a function.
Regards
这篇关于在codebehind ASP.Net按钮调用codebehind功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!