1

I create textbox by this code:

<div style="clear:left;">
    <asp:TextBox TextMode="MultiLine" runat="server" ID="selectText" ReadOnly="true" Width="560px" Height="50px"></asp:TextBox>
</div>

I fill it by this code:

elSelText.value = elSelText.value.substr(0, position) + chosenoption2.value + " ";

And then i try to send value in textbox to server, but it's empty!

protected void btnUseSelectClick(object sender, EventArgs e)
    {
        sourceDetails.SelectCommand += " and " + selectText.Text;
        Session["FilterSelectCommand"] = sourceDetails.SelectCommand;
        tableResults.DataBind();
    }

On the advice I added AutoPostBack="true":

<div style="clear:left;">
    <asp:TextBox TextMode="MultiLine" runat="server" AutoPostBack="true" ID="selectText" ReadOnly="true" Width="560px" Height="50px"></asp:TextBox>
</div>

but it didn't help

5
  • 2
    Just a guess, but does removing ReadOnly="true" make a difference? Commented Jan 17, 2013 at 14:49
  • The value of the textbox is empty because the server doesn't know it has changed. The server needs to be notified somehow. Typically this is done with either a full or partial postback. Commented Jan 17, 2013 at 14:51
  • 1
    @Doc I think that's what protected void btnUseSelectClick() { } is for Commented Jan 17, 2013 at 15:00
  • @DStanley I don't think it would. The value wouldn't be submitted if it were disabled, but readonly shouldn't make a difference. I could be wrong though Commented Jan 17, 2013 at 15:01
  • @DStanley, you advice helped me but what if it important to stay textbox readonly? Commented Jan 17, 2013 at 15:02

3 Answers 3

2

Although it's news to me, it seems that the ReadOnly property doesn't keep track of changes from the client. If you want the "readonly" functionality but still get the value on the server, put the following in your Page_Load method:

selectText.Attributes.Add("readonly", "readonly");

And remove the ReadOnly (and AutoPostBack) property in the <asp:TextBox> tag.

( From: http://aspadvice.com/blogs/joteke/archive/2006/04/12/16409.aspx and http://forums.asp.net/t/1467081.aspx - it was a fairly quick find with Google)

Sign up to request clarification or add additional context in comments.

Comments

0

Maybe a problems of ViewState. Try add the check of Page.IsPostBack in the page load event like this:

If(!Page.IsPostBack)
{
    // Data binding for the first call
}

Comments

0

I believe this is due to ReadOnly: ASP.Net registers which controls are readonly when sending you the page.

The value of these controls is discarded when posting back, and it is regotten (from ViewState I believe).

A workaround for this would be not setting readonly="true" on the aspx page, but setting it with $(document).ready(your_function_here();) (if you're using jQuery) or with the body onLoad event.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.