我有一个gridView。在代码背后我想得到Id和一个名字
<asp:GridView ID="GridViewCategory" runat="server"
OnSelectedIndexChanged="GridViewCategory_SelectedIndexChanged"
OnPageIndexChanging="GridViewCategory_PageIndexChanging"
AllowPaging="True" DataKeyNames="Id"
CellPadding="4" ForeColor="#333333" GridLines="None"
>
<Columns>
<asp:BoundField DataField="Id" HeaderText="Идент. номер" ItemStyle-Wrap="false" ItemStyle-Width="20%" />
<asp:BoundField DataField="Name" HeaderText="ALl name For" ItemStyle-Wrap="false" ItemStyle-Width="60%" />
<asp:CommandField SelectText="Избери" ShowSelectButton="True" />
</Columns>
我得到了这个:
protected void GridViewCategory_SelectedIndexChanged(object sender, EventArgs e)
{
int abv = Convert.ToInt32(GridViewCategory.SelectedValue);
//Label labelName = (Label)GridViewCategory.SelectedRow.FindControl("Name"); this is not working I think because it is BoundField And rows don't have Id only DataField
}
但如何获取此Id的名称
答案 0 :(得分:0)
您可以将Name添加为另一个datakeyname
<asp:GridView ID="GridViewCategory" runat="server"
OnSelectedIndexChanged="GridViewCategory_SelectedIndexChanged"
OnPageIndexChanging="GridViewCategory_PageIndexChanging"
AllowPaging="True" DataKeyNames="Id, Name"
CellPadding="4" ForeColor="#333333" GridLines="None"
>
<Columns>
<asp:BoundField DataField="Id" HeaderText="Идент. номер" ItemStyle-Wrap="false" ItemStyle-Width="20%" />
<asp:BoundField DataField="Name" HeaderText="ALl name For" ItemStyle-Wrap="false" ItemStyle-Width="60%" />
<asp:CommandField SelectText="Избери" ShowSelectButton="True" />
</Columns>
然后在你的代码背后:
protected void GridViewCategory_SelectedIndexChanged(object sender, EventArgs e)
{
int index = GridViewCategory.SelectedIndex;
int abv = Convert.ToInt32(this.GridViewCategory.DataKeys[index]["Id"]);
string name = this.GridViewCategory.DataKeys[index]["Name"].ToString();
}
希望这有帮助