ASP.NET Gridview按钮单击“未启动”

ASP.NET Gridview按钮单击“未启动”,asp.net,Asp.net,大家好,我有一个简单的ASP按钮内的网格。但onclick事件似乎没有启动。我哪里出错了 这是我的aspx页面的第一行 <%@ Page Title="Trainer Data" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeFile="TrainerData.aspx.vb" Inherits="TrainerData"%> 如果btnExecutiveGenerate_Click

大家好,我有一个简单的ASP按钮内的网格。但onclick事件似乎没有启动。我哪里出错了

这是我的aspx页面的第一行

<%@ Page Title="Trainer Data" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeFile="TrainerData.aspx.vb" Inherits="TrainerData"%>
如果btnExecutiveGenerate_Click是vb.net事件处理程序,则将OnClientClick事件更改为OnClick

<asp:Button ID="btnExecutiveGenerate" runat="server" Text="Generate"    
 OnClick="btnExecutiveGenerate_Click"/>
如果btnExecutiveGenerate_Click是vb.net事件处理程序,则将OnClientClick事件更改为OnClick

<asp:Button ID="btnExecutiveGenerate" runat="server" Text="Generate"    
 OnClick="btnExecutiveGenerate_Click"/>

您需要在RowCommand事件中处理Gridview的按钮单击事件

注意:请查看添加到按钮的CommandName和CommandArgument属性

<asp:GridView ID ="gvExecSummary" runat="server" CssClass="gridview" AllowSorting="false" AllowPaging="false" AutoGenerateColumns="false" Width="98%" >
<RowStyle Height="22px" OnRowCommand="gvExecSummary_RowCommand"  />
<AlternatingRowStyle Height="22px" CssClass="bg" BackColor="LightGray"/>
<HeaderStyle Height="22px" BackColor="#4b6c9e" Font-Bold="true"/>
<Columns>
<asp:TemplateField  HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="5%" HeaderText="Action">
<ItemTemplate>
<asp:Button ID="btnExecutiveGenerate" runat="server" Text="Generate"   CommandName="GenerateExecutive" CommandArgument="<%#((GridViewRow)Container).RowIndex %>"  />
</ItemTemplate>
</asp:TemplateField>

您需要在RowCommand事件中处理Gridview的按钮单击事件

注意:请查看添加到按钮的CommandName和CommandArgument属性

<asp:GridView ID ="gvExecSummary" runat="server" CssClass="gridview" AllowSorting="false" AllowPaging="false" AutoGenerateColumns="false" Width="98%" >
<RowStyle Height="22px" OnRowCommand="gvExecSummary_RowCommand"  />
<AlternatingRowStyle Height="22px" CssClass="bg" BackColor="LightGray"/>
<HeaderStyle Height="22px" BackColor="#4b6c9e" Font-Bold="true"/>
<Columns>
<asp:TemplateField  HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="5%" HeaderText="Action">
<ItemTemplate>
<asp:Button ID="btnExecutiveGenerate" runat="server" Text="Generate"   CommandName="GenerateExecutive" CommandArgument="<%#((GridViewRow)Container).RowIndex %>"  />
</ItemTemplate>
</asp:TemplateField>
使用命令Argumnet

<asp:TemplateField>
<ItemTemplate>
  <asp:Button ID="AddButton" runat="server" 
  CommandName="AddToCart" 
  CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>"
  Text="Add to Cart" />
   </ItemTemplate> 
</asp:TemplateField>
使用命令Argumnet

<asp:TemplateField>
<ItemTemplate>
  <asp:Button ID="AddButton" runat="server" 
  CommandName="AddToCart" 
  CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>"
  Text="Add to Cart" />
   </ItemTemplate> 
</asp:TemplateField>

这可能对某些人有所帮助,但我遇到编辑按钮没有在行内触发事件,而不是在页脚。 原因是gridview包含一个标签,该标签与页面上其他位置的标签具有相同的ID。 似乎这并没有给页脚行带来问题,因为页脚行不包含任何这样的标签。
我希望这对某人有所帮助。

这可能对某人有所帮助,但我遇到编辑按钮未在行内触发事件,而不是在页脚触发事件。 原因是gridview包含一个标签,该标签与页面上其他位置的标签具有相同的ID。 似乎这并没有给页脚行带来问题,因为页脚行不包含任何这样的标签。
我希望这对某人有所帮助。

您有BTNEXecuritiveGenerate\u Click的代码吗?如果是这样的话,你能把它也发布到这里吗?嗨,请检查我的编辑。@Aishvarya你是如何将数据加载到gridview的?你在用页面加载的方法吗?你能用那个代码更新吗?嗨,我在dropwdown的选定索引上加载网格,下面是代码。。Dim dbl作为新vtidal.vtidaldatacontext Dim dsGetFacDtls=来自dbl.usp_GetFacultyDetails0.AsQueryable,其中dF.SchoolMasterID=ValCboSubSchoolSel.SelectedValue按dF.SchoolID选择dF订单,dF.FacultyID gvExecSummary.DataSource=dsGetFacDtls gvExecSummary.databind您有btnExecutiveGenerate的代码吗?如果是这样的话,你能把它也发布到这里吗?嗨,请检查我的编辑。@Aishvarya你是如何将数据加载到gridview的?你在用页面加载的方法吗?你能用那个代码更新吗?嗨,我在dropwdown的选定索引上加载网格,下面是代码。。Dim dbl作为新vtidal.vtidaldatacontext Dim dsGetFacDtls=来自dbl.usp_GetFacultyDetails0.AsQueryable,其中dF.SchoolMasterID=ValCboSubSchoolSel.SelectedValue按dF.SchoolID选择dF订单,dF.FacultyID gvExecSummary.DataSource=dsGetFacDtls gvExecSummary.databind他刚才说>P.S.我甚至尝试过onclick,但也不起作用。他刚才说>P.S.我甚至尝试过onclick,但也不起作用。我的e是GridViewRowEventArgs类型,所以我无法访问CommandName属性。@Aishvarya-你能帮我吗复杂的GridView onRow命令处理程序上的EventArgs必须为GridViewCommandEventArgs类型。这将有CommandName和CommandArgument属性可用。我认为这段代码被施了魔法,根本不起作用。正如你所看到的,这是C代码。在发布之前我已经试过了。您能告诉我们您遇到了什么错误吗?请告诉我们我的e是GridViewRowEventArgs类型,因此我无法访问CommandName属性。@Aishvarya-您能详细说明一下吗?GridView onRow命令处理程序上的EventArgs必须为GridViewCommandEventArgs类型。这将有CommandName和CommandArgument属性可用。我认为这段代码被施了魔法,根本不起作用。正如你所看到的,这是C代码。在发布之前我已经试过了。你能分享一下你的错误吗?让我们分享一下
<asp:TemplateField>
<ItemTemplate>
  <asp:Button ID="AddButton" runat="server" 
  CommandName="AddToCart" 
  CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>"
  Text="Add to Cart" />
   </ItemTemplate> 
</asp:TemplateField>
Protected Sub GridView1_RowCommand(ByVal sender As Object, _ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs)
 If (e.CommandName = "AddToCart") Then
  ' Retrieve the row index stored in the CommandArgument property.
  Dim index As Integer = Convert.ToInt32(e.CommandArgument)

  ' Retrieve the row that contains the button 
  ' from the Rows collection.
  Dim row As GridViewRow = GridView1.Rows(index)

  ' Add code here to add the item to the shopping cart.

 End If
End Sub