我有一个展开折叠嵌套网格视图,如下所示。
childgrid(gvChild)列是点击链接按钮列,我需要获取所点击的城市名称。我在子gridview上捕获它的rowcommand事件是这样的:
protected void gvChild_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "ShowDetails")
{
int index = Convert.ToInt32(e.CommandArgument);
GridView gridChild = (GridView)gvwParent.Rows[index].FindControl("gvChild");
foreach (GridViewRow row in gridChild .Rows)
{
string aCity= ((LinkButton)row.Cells[1].FindControl("lnkSelectedCity")).Text;
}
}
}
我理解findcontrol行是def错误的,但是我怎样才能对子网格进行findcontrol以获得被点击的正确链接按钮?现在,如果我选择"伦敦",则索引值为1,字符串" aCity"将它作为"犹他州"。将欣赏这方面的意见。谢谢。
我的aspx页面上的代码:
<asp:GridView ID="gvParent" runat="server"
AllowPaging="True" AutoGenerateColumns="False"
DataKeyNames="CountryID"
OnRowDataBound="gvParent_RowDataBound" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<a href="javaScript:divexpandcollapse('div<%# Eval("CountryID") %>');">
<img id='imgdiv<%# Eval("CountryID") %>' width="9px" border="0" src="expand_white.gif"
alt="" /></a>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CountryDesc">
<ItemTemplate>
<asp:Label ID="lblEmpID" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "CountryDesc") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<tr>
<td colspan="100%">
<div id='div<%# Eval("CountryID") %>' style="display: none; position: relative; left: 15px; overflow: auto">
<asp:GridView ID="gvChild" runat="server" AutoGenerateColumns="False" PageSize="5"
Width="100%" Height="270px" ShowHeader="False" OnRowDataBound="gvChild_RowDataBound">
<Columns>
<asp:BoundField DataField="CityID" HeaderText="CityID">
<HeaderStyle CssClass="Invisible" />
<ItemStyle CssClass="Invisible" HorizontalAlign="Left" />
</asp:BoundField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkSelectedCity" runat="server" Text='<% # Eval("CityName") %>'
CommandName="ShowDetails" CommandArgument='<%# Container.DataItemIndex %>' ToolTip="Select x"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Assigned" HeaderText="Assigned">
<HeaderStyle CssClass="Invisible" />
<ItemStyle CssClass="Invisible" HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="CountryID" HeaderText="Assigned">
<HeaderStyle CssClass="Invisible" />
<ItemStyle CssClass="Invisible" HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="ClassID" HeaderText="ClassID">
<HeaderStyle CssClass="Invisible" />
<ItemStyle CssClass="Invisible" HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="UOM" HeaderText="UOM">
<HeaderStyle CssClass="Invisible" />
<ItemStyle CssClass="Invisible" HorizontalAlign="Left" />
</asp:BoundField>
</Columns>
</asp:GridView>
</div>
</td></tr>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
答案 0 :(得分:0)
我认为 CityID 是一个唯一的密钥。如果是这样,您可以将 CityID 分配给 CommandArgument 。
然后您可以在回发后从 CommandArgument 中检索 CityID 。
<asp:GridView ID="gvChild" ... DataKeyNames="CityID">
<Columns>
...
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkSelectedCity"
...
CommandArgument='<%# Eval("CityID") %>'
ToolTip="Select x" />
</ItemTemplate>
</asp:TemplateField>
protected void gvChild_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "ShowDetails")
{
int cityId = Convert.ToInt32(e.CommandArgument);
}
}