请教关于Asp.net中GridView和模板列的问题?
在GridView中有两个模板列,一个为DropDownList,一个为LinkButton,然后给LinkButton定义了一个Command,给Command传入了一个参数,如下所示:
<asp:GridView ID= "GridView1 " runat= "server " DataSourceID= "DataSource1 "
AutoGenerateColumns= "false " AllowSorting= "True ">
<Columns>
<asp:TemplateField HeaderText= "类别 ">
<ItemTemplate>
<asp:DropDownList runat= "server " DataSourceID= "cat "
DataTextField= "类别 " DataValueField= "类别 " ID= "ddl ">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID= "LinkButton3 " runat= "server " OnCommand= "Test " CommandArgument= ' <%#Eval( "项目号 ")%> ' > 确定 </asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
然后我想在LinkButton的Command事件中得到当前行中的DropDownList的值,方法如下:
我知道是应该先得到当前行的索引,然后用如下的方法:
protected void Test(object sender, CommandEventArgs e)
{
int rowindex;//?????如何得到该行的索引,e传进来的是我定义的一个值,不是索引号。
DropDownList ddl = (DropDownList)(WorkList.Rows[rowindex].FindControl( "ddl "));
}
谢谢!!!
[解决办法]
选中行?
rowindex = gvUsers.SelectedIndex;
如果你自己定义事件,应该把触发时间的index放到e中。
[解决办法]
你不一定是这样知道你的索引的阿;
如下:
<asp:LinkButton ID= "lbtnModify " runat= "server " CommandName= "modify " CommandArgument= ' <%# DataBinder.Eval(Container.DataItem, "ID ") %> '> 修改 </asp:LinkButton>
[解决办法]
帮顶 !