【问题标题】:linkbutton or button onClick not fireing inside gridview ItemTemplate链接按钮或按钮 onClick 未在 gridview ItemTemplate 内触发
【发布时间】:2014-04-23 19:04:37
【问题描述】:

嗨,我在我的 asp.net 网页中有一个 gridview,在用户选择 3 个下拉列表后有界。
下拉列表的自动回发设置为 true,现在我的问题是当我在 gridview ItemTemplate 中放置一个按钮、图像按钮或链接按钮时,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");
}

【问题讨论】:

  • 你在这里查看答案了吗? stackoverflow.com/a/11455262/382214
  • 只是为了仔细检查...您是否在Deny_Click1 的最顶部放置了一个断点。您正在吞下潜在的异常,所以问题可能是存在异常,但您没有意识到。
  • @ClaudioRedi 我把 Response.redirect("home.aspx") 它没有触发!!
  • @ewitkows 那不是我的问题..
  • 删除按钮上的 Onclick 事件并在那里设置命令名和参数。

标签: c# asp.net gridview


【解决方案1】:

它可能正在触发,但它会抛出异常(您的 catch 块会忽略该异常)。例外是因为这行:

LinkBut​​ton Link = (LinkBut​​ton)sender;

发送者是Button,不是LinkBut​​ton,所以强制转换无效,会抛出异常。

【讨论】:

    【解决方案2】:

    你可以检查这个::

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

    像这样验证你的代码

    【讨论】:

      【解决方案3】:

      GridView 中的按钮不响应直接事件。他们必须通过 GridView 的 RowCommand 事件实现。我看到你已经实现了OnRowCommand="GridView1_RowCommand"。所以现在您需要对图像按钮进行以下更改:

      <asp:ImageButton ID="accept"  runat="server" CommandName="Accept" />
      

      现在在 GridView1_RowCommand 事件处理程序中查找“接受”命令名称。

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

      我希望这会有所帮助。

      【讨论】:

      • 它不会命中 GridView1_RowCommand 事件处理程序吗?它会回发吗?
      • 你完全错了。 GridView 内的按钮不响应直接事件?从何时起?请不要提供错误信息!
      • 谢谢@naveen。更正了自己。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-21
      • 2022-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多