Share via


How to retrieve textbox values from UpdatePanel

Question

Wednesday, September 3, 2008 4:53 AM

Hi,

I'm doing my first steps with AJAX, and ran into this problem:

I have an updatepanel, inside the contentemplate I have an ASP.NET table control.
This table is filled up with several asp.net textbox controls when the page is loaded

On top of this table (also inside the contenttemplate) is an asp.net checkbox control which has autopostback="true" and has a checkedchanged event handler.

Now, when I check the checkbox the checkedchanged event handler is being called, no problem so far.
In this handler I need to fill the asp.net table control again (with all the textboxes). For this I need to know what values were entered into the textboxes when the checkbox was checked.
Without the whole AJAX thing I could check the Request.Form collection to get the textbox value, but this doesn't seem to work when using the updatepanel approach.

Is there any way I can get my textbox values when filling the table again ?

Thanks in advance!

All replies (9)

Thursday, September 4, 2008 2:32 PM âś…Answered

You can have controls collection approach, which you have been using earlier like Request.Form collection. To do the just put an extra panel control in you table, and add all those dynamic text boxes to the panel control and further, in the onchekcedchanged event, iterate through the panel's controls collection to access those textboxes.

This is a cleaner way to do it comparing your javascript way, since you can avoid javascript string operations as well as those findControl() method calls.

Here is the code:

1.DefaultDynamicControls.aspx

  

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DefaultScript.aspx.cs" Inherits="DefaultScript" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:ScriptManager ID="scriptManager" runat="server"></asp:ScriptManager>
    <asp:UpdatePanel ID="updatePanel" runat="server">
    <ContentTemplate>
    
    </ContentTemplate>
    </asp:UpdatePanel>
    <asp:Button ID="button" runat="server" Text="PostBack" OnClick="OnButtonClick" />
    </div>
    </form>
</body>
</html>

 

2.DefaultDynamicControls.aspx.cs

  

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class DefaultDynamicControls : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        panel1.Controls.Add(new TextBox());
        panel1.Controls.Add(new TextBox());
        panel1.Controls.Add(new TextBox());
        panel1.Controls.Add(new TextBox());
        panel1.Controls.Add(new TextBox());
        panel1.Controls.Add(new TextBox());
        panel1.Controls.Add(new TextBox());
    }
    protected void OnButtonClick(object sender, EventArgs e)
    {
        string sampleText = string.Empty;
        foreach (Control eachTextbox in panel1.Controls)
        {
            if (eachTextbox is TextBox)
            {
                sampleText += ((TextBox)eachTextbox).Text;
            }
        }
        ((Button)(sender)).Text = sampleText;
    }
}

 


Wednesday, September 3, 2008 1:27 PM

You can access the textbox just like you would in a standard .NET application, for example TextBox1.Text.  Let me know if you still have issues

-Damien


Wednesday, September 3, 2008 11:17 PM

Sets the browser focus to the specified control

http://www.asp.net/AJAX/Documentation/Live/mref/O_T_System_Web_UI_ScriptManager_SetFocus.aspx

http://gmamaladze.googlepages.com/maintainfocusbetweenpostbacksalsoinasp.n

You can use the following code to set the focus:
 

ScriptManager sm = ScriptManager.GetCurrent(this);
sm.SetFocus(myTextBox);

To set focus and select text of a you have to use the following:
 

ScriptManager.RegisterStartupScript(this, this.GetType(), "selectAndFocus", "$get('" + myTextBox.ClientID + "').focus();$get('" + myTextBox.ClientID + "').select();", true);
http://forums.asp.net/t/1099345.aspx
http://forums.asp.net/t/1199709.aspx
To get text
ScriptManager.RegisterStartupScript(this, this.GetType(), "getText", "$get('" + txtlname.ClientID + "').Text;", true);

Thursday, September 4, 2008 1:53 AM

I can't access the textbox controls (like 300 of them, depending on the situation) using myTextbox.ClientID, since I add them at runtime in the code behind, so they're not controls that exist in design.

I solved it by adding a hidden textbox on my page and add textboxid,value keypairs in it using javascript. I can read out the value of this hidden textbox on an ajax postback to know the actual values on a postback. Maybe (and probably) not the way it should be done, but I couldn't come up with another solution, and this one works.

If a better solution exists, I'd love to hear it :)


Friday, September 5, 2008 1:46 AM

I only have an ASP table control on my aspx page (design), so can't add a panel first since I'd have to add this panel at runtime as well.
In the code behind I create rows/cells/textboxes and them into the controls (textboxes in tablecells, tablecells in tablerows, tablerows in table).

But I think I see what you're saying.
In my scenario I can iterate through the tablerows, then tablecells, and then controls in each tablecell to get to the textboxes ?
Similar to what you're doing with the panel ?


Friday, September 5, 2008 2:17 AM

 Yes, you are right, since we dont have a panel, we can iterare through the tablerows, then tablecells, and then controls in each tablecell to get to the textboxes.

Let me know if you need any further assistence on this.


Friday, September 5, 2008 2:30 AM

Ok, this looks like a nice solution.
But isn't my approach (using javascript) a faster way of doing things ?

I only keep the changed textboxes in a hidden field (textboxid and value).
In my code behind I create a dictionary based on the values in this hidden field, that contains the changed textboxes only.
This means that when I recreate the table, I'll only iterate through changed textbox values to see if the current textbox has a value entered before postback, in stead of the whole collection (using your approach).
I'm talking about like 300 textboxes, so when I change 1 textbox I'll only check 1 in my approach each time I create a textbox, while I'd check 300 textboxes in your approach every time.

I agree that javascript is a more annoying way of doing things is easy to get bugs in it, but once everything is set up correctly it works as it should work.
The environment where I use it is on an intranet, where we can expect a minimum of capabilities of browsers used to access it (javascript will be enabled), and is less vulnerable to hacking attempts.

Just my feeling on this before I go start changing things.


Friday, September 5, 2008 4:42 AM

Iterating through only the changed/dirty textbox is a nice idea indeed. I was not aware of the fact that you are interested only in the changed/dirty textboxes.

Clint side code is always better performer than server side, however if you need to use those values in the server side, then server side is not a bad idea.

If i undertsand the approach you mentioned, you would be putting the textbox values in the another text box using some delimiter and then parse the same to get individual textbox values, has a basic assumsion that the delimiter that you are using can not be a part of the text box values, and in case they are...it'll break. And next, dont take my word for it, load test both the approaches, access their value at the sever side, you will agree that srting parsing is costly.


Friday, September 5, 2008 4:50 AM

Ok thanks for the reply on this, I'll keep my approach as it's working good now and regarding the performance probably the most interesting solution for this.
I've marked your answer about iterating through the control collection as answer here since it is another possible solution.

Thanks for helping me out on this issue :)