简体   繁体   English

代码隐藏中的ASP.Net Button调用codebehind函数

[英]ASP.Net Button in codebehind that calls codebehind function

I'm using Telerik RadControls, in my codebehind I have the following function, a portion of which adds buttons to the footer. 我正在使用Telerik RadControls,在我的代码隐藏中,我有以下功能,其中一部分为页脚添加了按钮。

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>--%>

Attributes.Add() function is used to add HTML attributes to elements in the page. Attributes.Add()函数用于将HTML属性添加到页面中的元素。 So you are adding client side script in your code. 因此,您要在代码中添加客户端脚本。 To add a code behind event to your button, use should use the following code: 要在事件后面添加代码,请使用以下代码:

   btn1.Click += new EventHandler(btn1_Click);

nandokakimoto is correct, but the syntax in VB is: nandokakimoto是正确的,但VB中的语法是:

AddHandler btn1.Click, AddressOf btn1_Click

A strange syntax where you don't use brackets even though AddHandler appears to be a function. 一种奇怪的语法,即使AddHandler似乎是一个函数,也不使用括号。

Regards 问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM