2

我正在尝试在下拉列表的 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" />
4

3 回答 3

0

如果您无法从后面的代码访问 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 内部。

于 2012-01-23T12:50:52.220 回答
0

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

或者您可以从 ASP.net 论坛尝试此解决方案

这是一个关于更新面板的小教程(MSDN)

希望这对您有所帮助。

于 2012-01-23T12:16:12.230 回答
0
GridViewRow cancel = (GridViewRow)GridView1.Rows[e.RowIndex];
Label lbldeleteID = (Label)cancel.FindControl("lblid");
于 2012-01-23T12:18:45.163 回答