在gridview中如何使用rowcommand事件作为按钮

Sam*_*ner 5 c# asp.net gridview button rowcommand

我在aspx中使用gridview,我有两个页面注册和details.aspx一旦注册完成它应该转到详细信息页面详细信息.aspx我保留了一个gridview在那个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>
Run Code Online (Sandbox Code Playgroud)

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)
{

}
Run Code Online (Sandbox Code Playgroud)

}

Ali*_*Ali 6

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

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

然后,在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;
    }
}
Run Code Online (Sandbox Code Playgroud)


nav*_*een 6

两种方法要做到这一点

方法1

请在标记中更改这些内容

  1. 更改 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

方法2

给一个CommandArgument.您可以提供许多不同的参数

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

现在在代码中,执行此操作

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
    }
}
Run Code Online (Sandbox Code Playgroud)

PS:

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

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

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