Link to home
Start Free TrialLog in
Avatar of Je Vught
Je VughtFlag for United States of America

asked on

Why is the asp:UpdatePanel not changing the value of my ASP:Textbox when the textbox is updated from Codebehind?

My scheme:

  • I have a label along with a number of textboxes loaded inside an asp:Updatepanel. These controls are initially loaded with values from an SQL database. These values are based on the initial index passed to the page.
  • Additionally the page contains a gridview which contains a list of "contacts".
  • The idea is that if the user selects a contact from the gridview, the index is used to re-query the SQL db. This function returns a datatable which is used to populate the above mentioned textboxes with the selected contacts data - name, address, emails, etc. The user can then modify the textboxes as necessary.
  • At present, I'm using a asp:timer to trigger the updatepanel when a different contact is selected (though I'd welcome a more elegant trigger solution).


When the page is first loaded, the label and all the textbox controls are populated without issue.


The problem: When subsequent contacts are selected, ONLY the label value changes - NOT the textbox values.

What I've tried:

  • If I disable the timer/trigger, then even the label is not updated when a record is selected in the gridview. This seems to indicate that the timer/trigger is effective (when enabled) as an AsyncPostBackTrigger
  • For the textboxes, I've tried variations of how to assign the textbox values (e.g. Textboxt1.text = value;   TextBox1.Attributes.Add("value", value1);
  • Additionally instead of using an asp:Textbox, I've tried using: <input type="text"  id="TextBox1" runat="server" />

Is what I'm trying to do even possible - what am I missing?


   <asp:Timer runat="server" ID="ContactsTimer" Interval="100" Enabled="false" OnTick="ContactsTimer_Tick" />
   <asp:UpdatePanel ID="upnlContacts" runat="server" UpdateMode="Conditional" >  
      <ContentTemplate>
         <asp:Label ID="lblContactAccountID" runat="server"/>
         <asp:TextBox ID="txtContactFirstName" runat="server"/> 
<%--edit: also tried:
            <input type="text"  id="txtContactFirstName" runat="server" />--%>
      </ContentTemplate>
      <Triggers>
         <asp:AsyncPostBackTrigger ControlID="ContactsTimer" EventName="Tick" />
      </Triggers>
   </asp:UpdatePanel>

Open in new window


private void LoadSelectedContact(int personID)
   {
      DataTable dt = GetProfileContacts(personID, true);
      if (dt.Rows.Count > 0)
      {
         foreach (DataRow row in dt.Rows)
         {
            lblContactAccountID.Text = row["PersonID"].ToString(); 
            txtContactFirstName.Text = row["FirstName"].ToString();

// edit: also tried the below without success
//            string FirstName = row["FirstName"].ToString();
//            txtContactFirstName.value= FirstName;
//            txtContactFirstName.Attributes.Add("value", FirstName);


            // ...
            ContactsTimer.Enabled = true;
         }
      }
   }

   protected void ContactsTimer_Tick(object sender, EventArgs e)
   {
      ContactsTimer.Enabled = false;
      upnlContacts.Update();
   }

Open in new window


Edit: I added (above) an example of the '<input type="text" ...  ' that I've tried without success. Below is the Gridview markup and the associated codebehind:


<asp:GridView ID="gvContacts" runat="server" CssClass="table table-striped solid-top" AutoGenerateColumns="false"
   DataKeyNames="PersonID" OnRowCommand="gvContacts_RowSelected">
   <Columns>
      <asp:BoundField DataField="PersonID" />
      <asp:TemplateField HeaderText="Name" ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Center">
         <ItemTemplate>
            <asp:LinkButton runat="server" CommandName="Selected" CommandArgument="<%# Container.DataItemIndex %>"><%#Eval("LastName")%></asp:LinkButton>
            <asp:LinkButton runat="server" CommandName="Selected" CommandArgument="<%# Container.DataItemIndex %>"><%#Eval("FirstName")%></asp:LinkButton>
         </ItemTemplate>
      </asp:TemplateField>
      <asp:TemplateField HeaderText="Action" ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Center">
         <ItemTemplate>
            <asp:ImageButton runat="server" ImageUrl="~/Images/icon_pencil.png" ToolTip='<%# "Edit Profile for "  + Eval("firstname") + " " + Eval("lastname") %>' CommandName="Edit" CommandArgument="<%# Container.DataItemIndex %>" />
         </ItemTemplate>
      </asp:TemplateField>
   </Columns>
</asp:GridView>

Open in new window


protected void gvContacts_RowSelected(object sender, GridViewCommandEventArgs e)
{
   GridViewRow row;
   if (e.CommandSource.ToString() == "System.Web.UI.WebControls.ImageButton")
   {
      row = (GridViewRow)((ImageButton)e.CommandSource).NamingContainer;
   }
   else
   {
      row = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
   }
   int personID = Int32.Parse(row.Cells[0].Text);
   switch (e.CommandName)
   {
      case "Selected":
         LoadSelectedContact(personID);
         break;
      //case "Edit" ...
   }
}

Open in new window


Avatar of David H.H.Lee
David H.H.Lee
Flag of Malaysia image

Can you share the snippet code where you trigger the function of LoadsElectedContact?

Furthermore, I didn't see the code that you mentioned for this part? Are you able to refer rendered textbox clientID correctly?
For the textboxes, I've tried variations of how to assign the textbox values (e.g. Textboxt1.text = value;   TextBox1.Attributes.Add("value", value1);
Avatar of Je Vught

ASKER

Hello, I updated my original post to include the code snippets you requested.Does that answer your last question? Thanks for your time!
ASKER CERTIFIED SOLUTION
Avatar of David H.H.Lee
David H.H.Lee
Flag of Malaysia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes, this is solution. I verified this on my test project as well as my larger project. So as to be clear for anyone else following this thread:
1. with this solution the goofy asp:timer trigger is not needed.
2. additionally, I moved 'upnlContacts.Update(); ' to the end of the method 'LoadSelectedContact'.  

David - a question: would it surprise you that before step 2 was implemented, each time after the page was loaded for the first time, the additional code worked flawlessly - except for the very first link click? Once I added the .Update call everything worked fine.    

Thanks so much for your time - it's much appreciated.
David - a question: would it surprise you that before step 2 was implemented, each time after the page was loaded for the first time, the additional code worked flawlessly - except for the very first link click? Once I added the .Update call everything worked fine.    
Not really since your initial timer has triggered upnlContacts.Update() under ContactsTimer_Tick() event.
I thought you have to do some other purpose with the timer event initially. You can trigger other code under (!IsPostBack) for the very first link click.

Glad to hear the problem solved now. Cheers.