// 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 + ".";
}!!!问题是,虽然我能够打印网格视图表单中的数据,但是当我选择一行时,我无法打印出消息“You selected..etc..”使用"Label“服务器控件。
“能不能请any1解决我的问题”...
发布于 2011-06-03 04:36:07
不要使用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
发布于 2011-06-03 04:15:36
如何选择行?您可能希望在网格视图上设置此属性,以便通过允许控件生成一个按钮来允许选择发生(网格视图知道):
AutoGenerateSelectButton="True"
当然,您可以查看有关此here.的更多信息,这不是创建命令按钮的唯一方法,但如果它不符合您的目的,则必须进行后续操作。
https://stackoverflow.com/questions/6219779
复制相似问题