我在使用Telerik RadControls,在我的代碼隱藏中我有以下功能,其中一部分將按鈕添加到頁腳。ASP.Net在代碼隱藏的調用代碼隱藏功能的按鈕
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
隨着這個,我有我的代碼隱藏以下兩個函數,我希望在點擊按鈕時調用。
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
我的問題是這些函數沒有在呈現的頁面中調用。令我困惑的是,當我在標記中使用相同的屬性定義這些按鈕時,它們工作正常。我不明白定義標記中的按鈕與後面的代碼之間有什麼區別。爲什麼這些函數不能從我在後面的代碼中定義的按鈕中調用?這些工作的按鈕以及我爲了測試目的而在我的標記中註釋過的按鈕如下所示。
<%--<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和bgs264的幫助。 – kingrichard2005 2010-07-27 15:49:17