【问题标题】:Value is not found in boundfield in gridview?在gridview的boundfield中找不到值?
【发布时间】:2012-02-16 18:45:10
【问题描述】:

我正在尝试在下拉列表的 indexchange 事件中为 hiddenfield 赋值!实际上问题是当我试图更新我的记录时,我找不到那个隐藏字段的值!请给我解决方案或建议任何其他选择!谢谢!

我的网格视图是

<asp:TemplateField HeaderText="LocCode" SortExpression="LocCode">
   <EditItemTemplate>
       <ajax:UpdatePanel ID="upEditsLocation" runat="server" UpdateMode="Conditional">
           <ContentTemplate>
              <asp:DropDownList ID="ddlLocation" runat="server" 
                 DataSourceID="sdsLocation" 
                 OnDataBound="ddlLocation_DataBound"  
                 DataValueField="LocCode" AppendDataBoundItems="false" 
                 DataTextField="LocCode" 
                 AutoPostBack="true" 
                 onselectedindexchanged="ddlLocation_SelectedIndexChanged">
              </asp:DropDownList>
              <asp:SqlDataSource ID="sdsLocation" runat="server" ConnectionString="<%$ ConnectionStrings:ccConnString %>"
                 ProviderName="<%$ ConnectionStrings:CCConnString.ProviderName %>" SelectCommand="Select LocCode from Location">
              </asp:SqlDataSource>
           </ContentTemplate>
       </ajax:UpdatePanel>
   </EditItemTemplate>
   <ItemTemplate>
       <asp:Label ID="lblLocation" runat="server" Text='<%# Bind("LocCode") %>'>
       </asp:Label>
   </ItemTemplate>
</asp:TemplateField>

我的 indexchange 事件是

protected void  ddlLocation_SelectedIndexChanged(object sender, EventArgs e)
{
    hdloc.Value = ddlLocation.SelectedItem.Text;

}

而我的隐藏字段是

<asp:HiddenField ID="hdloc" runat="server" />

【问题讨论】:

  • 隐藏字段放在哪里?在您的数据绑定控制之外?
  • 是的!但是将值分配给隐藏字段,但我无法将其访问到后面的代码!当我尝试访问它时,我不知道实际的问题它给出了 null !
  • 什么不能访问 hdloc 或 ddlLocation ?

标签: c# asp.net gridview hiddenfield


【解决方案1】:

从代码中我可以看到HiddenField 不是您的更新面板的一部分。因此,如果您为其分配任何值,它将不会反映在客户端计算机上。将面板范围扩大到包含隐藏字段,然后试试。

或者您可以尝试来自 ASP.net 论坛的this 解决方案

Here is a small tutorial on update panel (MSDN)

希望对你有所帮助。

【讨论】:

    【解决方案2】:
    GridViewRow cancel = (GridViewRow)GridView1.Rows[e.RowIndex];
    Label lbldeleteID = (Label)cancel.FindControl("lblid");
    

    【讨论】:

      【解决方案3】:

      如果您无法从后面的代码访问 hdloc,则不是 Visual Studio 在 aspx.designer.cs 上添加的(尝试将其删除并重新添加或更改 id 然后恢复为原始值)或隐藏字段被放置在另一个绑定控件的其他模板中,这意味着您需要使用 ctrl.FindControl("hdloc") 然后转换为 HiddenField。
      您还需要将此隐藏字段放入 UpdateMode="Always" 的 UpdatePanel 中。

      protected void  ddlLocation_SelectedIndexChanged(object sender, EventArgs e)
      {   
          hdloc.Value = (sender as DropDownList).SelectedItem.Text;
      }
      

      我确定 ddlLocation.SelectedItem.Text,就像您使用它一样,它会产生编译错误,因为 ddlLocation 在后面的代码中不可见,因为它位于 EditItemTemplate 内部。

      【讨论】:

        猜你喜欢
        • 2011-03-06
        • 2018-03-11
        • 1970-01-01
        • 2011-06-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-12
        • 1970-01-01
        相关资源
        最近更新 更多