【问题标题】:Link button inside nested gridview invoking the wrong row嵌套gridview中的链接按钮调用错误的行
【发布时间】:2014-02-26 15:01:45
【问题描述】:

我有一个如下所示的展开折叠嵌套网格视图。

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>

【问题讨论】:

  • 请出示您的标记。

标签: c# asp.net gridview nested


【解决方案1】:

我假设 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);
  }  
}  

【讨论】:

  • 谢谢。这会起作用,但是我需要一个额外的数据库调用来获取该城市 ID 的详细信息。我在 RowCommand 中的代码还需要从其他列中获取值并将其分配给隐藏变量(如 frm classID、UOM)等。 x.Value = row.Cells[4].Text; hidClassID.Value = row.Cells[0].Text; hidSomething.Value = row.Cells[3].Text; lblUOM.Text = row.Cells[5].Text;
  • 通过 Cells[x] 检索数据非常脆弱。由于您有 cityId,您可以轻松地从数据库中检索城市信息。
  • 感谢 Win 抽出宝贵时间回复。从数据库获取是唯一的途径
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-29
  • 1970-01-01
  • 2013-08-02
  • 1970-01-01
相关资源
最近更新 更多