user1848942
user1848942

Reputation: 355

Textbox is empty after filling with javascript

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

Upvotes: 1

Views: 913

Answers (3)

Ian
Ian

Reputation: 50905

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)

Upvotes: 2

Destrictor
Destrictor

Reputation: 752

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.

Upvotes: 0

Hieu Le
Hieu Le

Reputation: 8415

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
}

Upvotes: 0

Related Questions