StudentIT
StudentIT

Reputation: 481

Why JQuery is unable to get Textarea value when the visible changed to false?

I thought changing textarea to non visible will disappear from user view but still be hidden with stored information? For my result, as long the textarea remain visible, the Jquery was able to get information. C# server side code is the one who change textarea visible to false.

After I click the first button(BtnIPAM), the C# server side get information from textarea and do the job then change textarea visible to false.

Then I click the second button(BtnISM) to call JQuery to get same textarea, but it get nothing. If I comment out in C# server side to leave textarea visible, the JQuery will be able to get information from textarea.

Anyone know why this happen and a way to fix it?

aspx codes included textarea, first button(BtnIPAM), and second button(BtnISM):

    <asp:TextBox ID="txt" runat="server" visible="False" TextMode="MultiLine" 
                Width=356px Height=200px style="margin-left: 0px"></asp:TextBox>

<asp:Button ID="BtnIPAM" runat="server" onclick="BtnIPAM_Click" 
            Text="Assign It!" />
        <br />
        <asp:Button ID="BtnISM" runat="server" 
            OnClientClick="if (!CreateIsm()) {return false;}"  UseSubmitBehavior="false"
            Text="ISM Easy Button" />

Simple C# button clicked method,

protected void BtnIPAM_Click(object sender, EventArgs e)
{
   //other codes are doing job before change txt to false
   txt.Visible = false;
}

In javascript CreateIsm() function, I use notes = $('#txt').val(); to get textarea info.

Upvotes: 2

Views: 1012

Answers (4)

Fr&#233;d&#233;ric Hamidi
Fr&#233;d&#233;ric Hamidi

Reputation: 263047

In ASP.NET server-side code, setting Visible to false will not actually make the control invisible, but will not render it on the page instead.

If you want to render a hidden control, you can write:

txt.Style.Add(HtmlTextWriterStyle.Display, "none");

Or simply:

txt.Style.Add("display", "none");

Upvotes: 2

Shai Aharoni
Shai Aharoni

Reputation: 1957

The code txt.Visible = false; causes your textarea HTML to not be rendered at all. You can use txt.Attributes["style"] = "display:none";

Upvotes: 1

Kamlesh
Kamlesh

Reputation: 529

In asp.net when you set visible to false control dont render on page, thats why your not able to access it on page

Upvotes: 2

Rob G
Rob G

Reputation: 3526

<asp:TextBox> is probably creating an alternate ID on the page for your control, so searching for '#txt' won't get the actual control. Alternatively you can set ClientIDMode to ClientIDMode.Static (just by specifying Static for that property on the control), then it wont change the ID on you, but you have to ensure all controls on your page have unique IDs.

Like this:

<asp:TextBox ID="txt" runat="server" ClientIDMode="Static">

Additionally, setting Visible='false' on the control will not even write the markup to display it on the page, so the control doesn't exist.

Upvotes: 0

Related Questions