gpt4 book ai didi

c# - grid selectedindexchanged问题

转载 作者:行者123 更新时间:2023-11-30 21:14:29 25 4
gpt4 key购买 nike

//aspx文件中的代码:

        <html>
<body>
<form>
<asp:GridView ID="grid" runat="server" AutoGenerateColumns="False"
onselectedindexchanged="grid_SelectedIndexChanged" >



<Columns>
<asp:BoundField DataField="RollID" HeaderText="RollID" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:ButtonField CommandName="Select" Text="Select" />


</Columns>
</asp:GridView>
</div><br />

<asp:label ID="Label" runat="server" text=""></asp:label>

</form>
</body>
</html>

//代码隐藏文件:

        protected void grid_SelectedIndexChanged(object sender, GridViewRowEventArgs e)
{

RowIndex = grid.SelectedIndex;
GridViewRow row = grid.Rows[RowIndex];
string a = row.Cells[4].Text;
Label.Text = "You selected " + a + ".";

}

!!!问题是,虽然我能够在 GridView 表单中打印数据,但是当我选择一行时,我无法使用“标签”服务器控件打印出消息“您选择了..等..”。

“任何人都可以解决这个问题吗”...

最佳答案

不要使用 SelectedIndexChanged 事件。相反,使用 RowCommand 事件:

protected void grid_RowCommand(object sender, GridViewCommandEventArgs e) {
if (e.CommandName == "Select") {
int RowIndex = Convert.ToInt32(e.CommandArgument);
GridViewRow row = grid.Rows[RowIndex];
string a = row.Cells[4].Text;
Label.Text = "You selected " + a + ".";
}
}

MSDN 链接:http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand.aspx

关于c# - grid selectedindexchanged问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6219779/

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