在gridview中如何对按钮使用rowcommand事件 [英] In gridview how to use rowcommand event for a button

查看:19
本文介绍了在gridview中如何对按钮使用rowcommand事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 aspx 中使用 gridview 并且我有两个页面,注册和 details.aspx 一旦注册完成,它应该转到 details.aspx 中的详细信息页面我在那个 GV 中保留了一个 gridview 我应该使用行命令事件一个按钮,它应该显示学生的所有结果,编辑按钮作为我使用项目模板的所有学生的最后一列.但是在行命令事件中,我不知道要编写的函数,如果用户单击编辑它应该使用用户 ID 转到编辑页面,该 ID 应该是中午可编辑模式,其他字段可以编辑.

详细信息.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"/><列><asp:BoundField DataField="UserName" HeaderText="UserName" ReadOnly="True"/><asp:BoundField DataField="密码" HeaderText="密码"/><asp:BoundField DataField="FirstName" HeaderText="FirstName"/><asp:BoundField DataField="LastName" HeaderText="LastName"/><asp:BoundField DataField="EMail" HeaderText="Emaid-ID"/><asp:BoundField DataField="PhoneNo" HeaderText="电话号码"/><asp:BoundField DataField="Location" HeaderText="Location"/><asp:TemplateField ShowHeader="False"><项目模板><asp:Button ID="btnedit" runat="server" Text="Edit" CommandName="Edit" CommandArgument="UserName"/></ItemTemplate></asp:TemplateField></列><EditRowStyle BackColor="#999999"/><FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White"/><HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White"/><PagerStyle BackColor="#284775" ForeColor="White" Horizo​​ntalAlign="Center"/><RowStyle BackColor="#F7F6F3" ForeColor="#333333"/><SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333"/><SortedAscendingCellStyle BackColor="#E9E7E2"/><SortedAscendingHeaderStyle BackColor="#506C8C"/><SortedDescendingCellStyle BackColor="#FFFFF8"/><SortedDescendingHeaderStyle BackColor="#6F8DAE"/></asp:GridView>

</表单>

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();关闭();}protected void GridView3_RowCommand(对象发送者,GridViewCommandEventArgs e){}protected void GridView3_SelectedIndexChanged(object sender, EventArgs e){}

}

解决方案

首先你的按钮控件 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){整数索引 = 0;GridViewRow 行;GridView grid = sender as GridView;开关(e.CommandName){案例编辑":index = Convert.ToInt32(e.CommandArgument);row = grid.Rows[index];//使用row在此处查找需要编辑、更新、删除的选中控件//例如HiddenField value = row.FindControl("hiddenVal") as HiddenField;休息;}}

I am using a gridview in aspx and i have two pages that registration and details.aspx once registration completed it should goto details page in details.aspx i have kept a gridview in that GV i am supposed be use row command event for a button it should show the all the rsults for the students with the edit button as the last column for all the students i used item template for that. but in row command event i dont know the function to write if user clicking edit it should goto the edit page using the userid the id should be noon editable mode and other fields can editable.

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

}

}

解决方案

First your button control CommandArgument property must have a unique value in each row:

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

Then on your code behind GridView3_RowCommand event you will have something like the code below:

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;
    }
}

这篇关于在gridview中如何对按钮使用rowcommand事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆