gpt4 book ai didi

c# - 在 gridview 中如何为按钮使用 rowcommand 事件

转载 作者:太空狗 更新时间:2023-10-29 22:18:58 25 4
gpt4 key购买 nike

我在 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" />
<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>

细节.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)
{

}

}

最佳答案

首先,您的按钮控件 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;
}
}

关于c# - 在 gridview 中如何为按钮使用 rowcommand 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24773645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com