在gridview中如何使用行命令事件按钮

本文关键字:命令 事件 按钮 何使用 gridview | 更新日期: 2023-09-27 18:13:04

我在aspx中使用gridview,我有两个页面注册和详细信息。Aspx一旦注册完成,它应该进入详细信息页面。aspx我在GV中保留了一个网格视图,我应该使用行命令事件作为按钮,它应该显示所有学生的结果,编辑按钮作为最后一列,为所有学生我使用项目模板。但是在行命令事件,我不知道的功能来写,如果用户点击编辑它应该去编辑页面使用用户id, id应该是中午可编辑模式和其他字段可以编辑。

details.aspx

   <asp:GridView ID="GridView3" runat="server" CellPadding="3" ForeColor="#333333" GridLines="None" AutoGenerateColumns="false"  OnRowCommand="GridView3_RowCommand" OnSelectedIndexChanged="GridView3_SelectedIndexChanged">
     <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
         <Columns>
             <asp:BoundField DataField="UserName" HeaderText="UserName" ReadOnly="True"/>
             <asp:BoundField DataField="Password" HeaderText="Password" />
             <asp:BoundField DataField="FirstName" HeaderText="FirstName" />
             <asp:BoundField DataField="LastName" HeaderText="LastName" />
             <asp:BoundField DataField="EMail" HeaderText="Emaid-ID" />
             <asp:BoundField DataField="PhoneNo" HeaderText="Phone Number" />
             <asp:BoundField DataField="Location" HeaderText="Location" />
              <asp:TemplateField ShowHeader="False">
                <ItemTemplate>
                   <asp:Button ID="btnedit" runat="server" Text="Edit" CommandName="Edit" CommandArgument="UserName"/>
                </ItemTemplate>          
             </asp:TemplateField> 
         </Columns>
     <EditRowStyle BackColor="#999999" />
     <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
     <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
     <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
     <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
     <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
     <SortedAscendingCellStyle BackColor="#E9E7E2" />
     <SortedAscendingHeaderStyle BackColor="#506C8C" />
     <SortedDescendingCellStyle BackColor="#FFFDF8" />
     <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
 </asp:GridView>
    </div>
</form>

details.aspx.cs

 protected void Page_Load(object sender, EventArgs e)
 {
    string connection2 = System.Web.Configuration.WebConfigurationManager.ConnectionStrings        ["connection1"].ConnectionString;
    SqlConnection con = new SqlConnection(connection2);
    con.Open();
    SqlCommand cmd = new SqlCommand("select * from User_Information", con);
    SqlDataReader rdr = cmd.ExecuteReader();
    GridView3.DataSource = rdr;
    GridView3.DataBind();
    con.Close();
}
protected void GridView3_RowCommand(object sender, GridViewCommandEventArgs e)
{
}

protected void GridView3_SelectedIndexChanged(object sender, EventArgs e)
{
}

}

在gridview中如何使用行命令事件按钮

首先,您的按钮控件CommandArgument属性必须在每行中有一个唯一的值:

 <asp:Button ID="btnedit" runat="server" Text="Edit" CommandName="Edit" 
             CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" />

然后在GridView3_RowCommand事件后面的代码中,您将有如下代码:

protected void GridView3_RowCommand(object sender, GridViewCommandEventArgs e)
{
    int index = 0;
    GridViewRow row;
    GridView grid = sender as GridView;
    switch (e.CommandName)
    {
        case "Edit":
            index = Convert.ToInt32(e.CommandArgument);
            row = grid.Rows[index];
            //use row to find the selected controls you need for edit, update, delete here
            // e.g. HiddenField value = row.FindControl("hiddenVal") as HiddenField;
            break;
    }
}

有两种方法

方法1

请更改这些标记

  1. Change CommandName="EditUserName"
  2. 省略CommandArgument。我们不需要这个

后台代码

protected void GridView3_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "EditUserName")
    {
        //first find the button that got clicked
        var clickedButton = e.CommandSource as Button;
        //find the row of the button
        var clickedRow = clickedButton.NamingContainer as GridViewRow;
        //now as the UserName is in the BoundField, access it using the cell index.
        var clickedUserName = clickedRow.Cells[0].Text;
    }
}

方法2

给出CommandArgument。你可以给出很多不同的参数,比如

  1. CommandArgument="<%# Container.DataItemIndex %>"
  2. CommandArgument="<%# Container.DisplayIndex %>"
  3. CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" (Ali给的)

现在在代码中,执行

protected void GridView3_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "EditUserName")
    {            
        var clickedUserName = CustomersTable
            .Rows[Convert.ToInt32(e.CommandArgument)]//find the row with the clicked index
            .Cells[0]//find the index of the UserName cell
            .Text;//grab the text
    }
}

p。S:

1。更改CommandName的原因是,如果CommandName="Edit",它将触发RowEditing事件,这将给您这个错误

未处理的GridView 'GridView3'触发事件RowEditing。

2。将Page_Load代码放在if(!IsPostBack)中,否则上述方法都不起作用。