0

I found an issue in some of my code and can't figure out the reason why. I'm using .Net 4.5. Can anyone please tell me the difference between these two cases? I tried a few different things such javascript to disable via Page.ClientScript or on the body onload event but I'm not getting what I want (TextBox2 is "" and TextBox1 is "Hello, TextBox1"). When I comment out tmp.Enable = false everything is fine. I'd like to be able to disable both controls but still access the Text value. Works fine for "TextBox1" but not "tmp" aka "TextBox2".

The reason for !IsPostBack and TextBox2 being created during the Page_Load is because I'm dynamically creating X number of controls and setting their value from a datareader. they can then be modified by the user and saved to the table. There must be a way!

This post sounds like my problem but I'm getting different results than them.
ASP.Net ViewState doesn't work when Control become Enable=False

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
    function t() {
        document.getElementById("TextBox1").disabled = true;
        document.getElementById("TextBox2").disabled = true;
    }
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
    <asp:Panel runat="server" ID="Panel1">
        <asp:TextBox runat="server" ID="TextBox1"></asp:TextBox>
    <asp:Button runat="server" ID="button1" OnClick="button1_Click" />
    </asp:Panel>
</div>
</form>
</body>
</html>

public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) { TextBox1.Text = "Hello, TextBox1"; }
        TextBox1.Enabled = false; 
        TextBox tmp = new TextBox();
        tmp.ID = "TextBox2";
        if (!IsPostBack) { tmp.Text = "Hello, TextBox2"; }
        tmp.Enabled = false;
        Panel1.Controls.Add(tmp); 
    }

    protected void button1_Click(object sender, EventArgs e)
    {
        TextBox tmp = ((TextBox)Page.FindControl("TextBox2"));
        if(tmp != null)
        {
            tmp.Text.ToString();
        }
        TextBox1.Text.ToString(); 
    }

}

UPDATE: Per haraman's suggestion I was able to get it working by making the following changes:

protected void Page_PreInit(object sender, EventArgs e)
    {
        TextBox tmp = new TextBox();
        tmp.ID = "TextBox2";
        Panel1.Controls.Add(tmp); 
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) { TextBox1.Text = "Hello, TextBox1"; }
        TextBox1.Enabled = false;

        if (!IsPostBack) { ((TextBox)Page.FindControl("TextBox2")).Text = "Hello, TextBox2"; }
        ((TextBox)Page.FindControl("TextBox2")).Enabled = false;


    }
    protected void button1_Click(object sender, EventArgs e)
    {
        TextBox tmp = ((TextBox)Page.FindControl("TextBox2"));
        if (tmp != null)
        {
            tmp.Text.ToString();
        }
        TextBox1.Text.ToString();
    }
Community
  • 1
  • 1
hackingchemist
  • 156
  • 1
  • 11
  • review your code in button1_click, you are using `tmp.Text.ToString();` and `TextBox1.Text.ToString();`. Where are you using these values? These must be in a statement format i.e. either assign them to a variable or control value. – haraman Oct 09 '15 at 05:27
  • This was just for demonstration purposes. In the button click tmp.Text.ToString() returns "" and TextBox1.Text.ToString() returns "Hello, TextBox1". In my example I'm not actually "using" the values, it was just to show the results via the debugger. – hackingchemist Oct 09 '15 at 14:23

2 Answers2

2

You should consider using ReadOnly = true instead of Enabled = false.

Values for disabled form elements are NOT passed to the processor method. For more specific details refer disabled-vs-readonly-form-fields/

EDIT: Addition with regard to your code
Created a test case with your code and found that I just misread your code. Here is what is happening in your code:

  1. You create a new TextBox (tmp) on every PostBack.
    tmp is recreated (But TextBox1 is already there and NOT recreated)

  2. You do NOT assign value to tmp on every PostBack
    This means there is no text in tmp (TextBox1 NOT being recreated, retains its text)

More specific details can be found in the answer given by R.C in this SO post dynamically-created-controls-losing-data-after-postback

A practical approach to the same can be found in this post ASPNet-Dynamic-Controls-ViewState-Retain-state-for-dynamically-created-controls-on-PostBack

Community
  • 1
  • 1
haraman
  • 2,744
  • 2
  • 27
  • 50
0

try using Read Only Property????

Read Only

In the context of a TextBox, readonly allows the user to set focus to and select and copy the text but not modify it. A disabled TextBox does not allow any interaction whatsoever.

Use ReadOnly when you have data that you want the user to see and copy, but not modify. Use a disabled textbox, when the data you are displaying is not applicable in for the current state of a dialog or window.

Enabled:

Gets or sets a value indicating whether the control can respond to user interaction.

A_Sk
  • 4,532
  • 3
  • 27
  • 51
  • I think read only will achieve what I'm going for.. However, I was wondering why TextBox1's value is returned when enable=false but TextBox2's value is "" – hackingchemist Oct 09 '15 at 14:30
  • Read Only was going to be a little more effort just because in my actual code I have a jQuery datepicker attached to the textbox. So the contents are "editable" by the user because of that. Enable = false was just easier and achieves what I wanted. Only when the textbox isn't created/added during the page load however. – hackingchemist Oct 09 '15 at 14:33