linkbutton或button单击不在gridview ItemTemplate中激发

本文关键字:ItemTemplate gridview button 单击 linkbutton | 更新日期: 2023-09-27 17:59:56

hi我的asp.net网页中有一个网格视图,在用户选择3个下拉列表后,该视图会被限制
下拉列表的自动回发设置为true,现在我的问题是,当我在我的gridview ItemTemplate中放置按钮、imagebutton或linkbutton时,onclick事件没有启动!这是我的代码

    <asp:GridView ID="GridView1" runat="server"   Width="90%" Height="100%" OnRowCommand="GridView1_RowCommand"  OnPageIndexChanging="GridView1_PageIndexChanging" style="margin:20px; vertical-align:top;"  PageSize="10" AllowPaging="True" 
        AllowSorting="True"  
        AutoGenerateColumns="False" DataKeyNames="WFStudentID" ShowHeader="true" BorderWidth="1px">
        <Columns>
          <asp:TemplateField  ShowHeader="true">
            <ItemTemplate >
              <tr>
                <td>
                  <asp:Label ID="Label1" ForeColor="Silver" Font-Size="Medium" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
                </td>
                <td>
                  <asp:Label ID="Label2" runat="server" ForeColor="Silver" Font-Size="Medium"  Text='<%# Bind("Family") %>'></asp:Label>
                </td>
                <td>
                  <asp:HyperLink ID="HyperLink1" ForeColor="Silver" Font-Size="Medium" Target="_blank" NavigateUrl='<%# Bind("WFStudentFilePath") %>' runat="server">دانلود</asp:HyperLink>
                </td>
                <td>
                  <asp:Label ID="Label7" runat="server" ForeColor="Silver" Font-Size="Medium"  Text='<%# Bind("WFStudentDate") %>'></asp:Label>
                </td>
                <td>
                  <asp:Button ID="Deny" Text="deny"  OnClick="Deny_Click1"       runat="server" />
                </td>
                <td>
                  <asp:ImageButton ID="accept" OnClick="accept_Click"  runat="server" />
                </td>
                <td>
                  <asp:TextBox ID="des" TextMode="MultiLine" Height="100px" Width="200px" runat="server"></asp:TextBox>
                </td>
              </tr>
              <br />
            </ItemTemplate>
          </asp:TemplateField>
        </Columns>
    </asp:GridView>  

protected void Deny_Click1(object sender, EventArgs e)
{
  Response.Redirect("home.aspx");
}

linkbutton或button单击不在gridview ItemTemplate中激发

它可能正在启动,但会抛出一个异常(catch块会忽略该异常)。例外是因为这行:

LinkButton链接=(LinkButton)发件人;

发送方是Button,而不是LinkButton,因此强制转换无效,将引发异常。

您可以检查以下内容::

http://www.dotnetbull.com/2013/05/how-to-handle-click-event-of-linkbutton.html

验证您的代码,如

GridView中的按钮不响应直接事件。他们必须通过GridView的RowCommand事件实现。我已经看到您已经实现了OnRowCommand="GridView1_RowCommand"。所以现在您需要对图像按钮进行以下更改:
<asp:ImageButton ID="accept"  runat="server" CommandName="Accept" />

现在在GridView1_RowCommand事件处理程序中查找"Accept"命令名。

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
  if (e.CommandName == "Accept")
  {
    // Your code goes here
  }
}

我希望这能有所帮助。