2

I am trying to assing value to hiddenfield on indexchange event of dropdownlist ! Actually the problem is when I am trying to update my record i can not find value of that hidden field ! Kindly give me solution or suggest any another option ! Thank You !

My grid view is

<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>

and my indexchange event is

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

}

And my hidden field is

<asp:HiddenField ID="hdloc" runat="server" />
Amar Palsapure
  • 9,590
  • 1
  • 27
  • 46
Chintan
  • 2,768
  • 4
  • 27
  • 43

3 Answers3

0

From the code I can see the HiddenField is not part of your update panel. Hence if you assign any value to it, it will not reflect on client machine. Increase the scope of panel to include the hidden field, and then try.

OR you can try this solution from ASP.net Forum

Here is a small tutorial on update panel (MSDN)

Hope this helps you.

Amar Palsapure
  • 9,590
  • 1
  • 27
  • 46
0
GridViewRow cancel = (GridViewRow)GridView1.Rows[e.RowIndex];
Label lbldeleteID = (Label)cancel.FindControl("lblid");
DaveShaw
  • 52,123
  • 16
  • 112
  • 141
Salih
  • 1
0

If you can't access hdloc from code behind, either is not added by Visual Studio on aspx.designer.cs (try delete it and add it back or change the id and then back to original value) or the hidden field is placed in other template of another binding control, which means you need to use ctrl.FindControl("hdloc") then cast to HiddenField.
Also you need to place this hidden field into an UpdatePanel with UpdateMode="Always".

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

I'm sure that ddlLocation.SelectedItem.Text, like you use it, it gives a compilation error, because ddlLocation is not visible on code behind, since is inside of EditItemTemplate.

Adrian Iftode
  • 15,465
  • 4
  • 48
  • 73