嗨,在我的gridview中的这个字段中,我想在单击select命令字段时传递id值,但我不希望id字段可见,所以我将visible属性设置为false;但是,当我在SelectedIndexChanged事件上传递它时,值为“”,但当visible属性设置为“true”时,文本值正常传递。这样做的正确方法是什么?
<asp:BoundField DataField="Project_ID" Visible="false"/>
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
String ProjID = GridView1.SelectedRow.Cells[10].Text;
}
答案 0 :(得分:0)
试试这个:
<强>的.aspx 强>
<asp:Gridview id="GridView1" runat="server" DataKeyNames="Project_ID" />
<强>的.cs 强>
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
if(gridView.SelectedDataKey != null)
{
var selectedId = GridView1.SelectedDataKey.Value;
}
}
在这里,您可以在Gridviews中找到有关DataKeys的更多信息:http://msdn.microsoft.com/de-de/library/system.web.ui.webcontrols.gridview.selecteddatakey(v=vs.110).aspx
答案 1 :(得分:0)
我在winform中做过类似的事情也许可以帮到你。这就是我用过的东西
int rowindex = dataGridView1.CurrentRow.Index;
string ProjID= dataGridView1.Rows[rowindex].Cells[10].Value.ToString();
答案 2 :(得分:0)
您可以在gridView ItemTemplate中使用HiddenField来保存ID并在onrowcommand事件中使用它,如下所示:
的.aspx
<asp:GridView ID="gridProject" runat="server" onrowcommand="gridProject_RowCommand"> <Columns> <ItemTemplate> <asp:HiddenField ID="hidProjectID" runat="server" Value='<%# ((DataRowView)Container.DataItem)["Project_ID"] %>' /> <asp:Button ID="btnProject" runat="server" Text="use pro id" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" CommandName="DoSomething"></asp:Button> </ItemTemplate> </Columns> </asp:GridView>
aspx.cs
protected void gridProject_RowCommand(object sender, GridViewCommandEventArgs e)
{
int index = 0;
GridViewRow gridRow;
GridView grid = sender as GridView;
try
{
switch (e.CommandName)
{
case "DoSomething":
index = Convert.ToInt32(e.CommandArgument);
row= gridProject.Rows[index];
string Id = ((HiddenField)row.FindControl("hidProjectID")).Value;
//do whatever you want here
break;
// and you can have as many commands as you want here
}
}
catch { //display error }
}