Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET text box loses text on partial postback

I have a user control,

In user control I have a button, which when is clicked, shows a message box ,

message box has a textbox and another button,

when I click on another button, I need to get textbox value in code behind, but it's not happening at all, as button is doing partial postback and textbox just loses it's state.

I can't get textbox value in page_load method and save it to session state as textbox is populating dynamically

this is what i tried so far,

<asp:TextBox ID="textbox1" TextMode="MultiLine" runat="server" />

and

protected void Button1_Click(object sender, EventArgs e)
        {
string button1text = TextBox1Text;

and

public partial class myUserControl : UserControl
{
    public string TextBox1Text
    {
        get 
        {
            return Page.Session["TextBox1Text"] as string;  
        }
        set
        {
            Page.Session["TextBox1Text"] = TextBox1.Text;
        }
    }

No Gain but Only Pain.

like image 460
Mathematics Avatar asked Oct 22 '22 10:10

Mathematics


2 Answers

It's been a while since I used UpdatePanels, but I believe that on partial postback they only send updated values for controls inside them. So move the TextBox inside the UpdatePanel, or perhaps use Javascript to populate a hidden control inside the UpdatePanel with the contexts of the TextBox whenever it is updated.

like image 83
David Cummins Avatar answered Oct 29 '22 21:10

David Cummins


in ASP page


    <input type="hidden" id="hidtext" runat="server" value="">

in C#


    hidtext.Value=textbox1.text;

or VB



     hidtext.Value=textbox1.text

after post back


    textbox1.text=hidtext.value;

like image 30
pradeep Avatar answered Oct 29 '22 20:10

pradeep