6

I want to set the value of a asp.net textbox using javascript

My JS Code is:

document.getElementById('<%=txtFlag.ClientID %>').value = "Track";

My textbox is:

<asp:TextBox ID="txtFlag" runat="server" Visible="False"></asp:TextBox>

But it gives me an error document.getElementById(...)' is null or not an object

I dont understand what is wrong.

Please help.

4
  • <asp:TextBox ID="txtFlag" runat="server" Visible="False"></asp:TextBox> Commented Jan 10, 2014 at 5:25
  • Why you took Visible="false"??? Commented Jan 10, 2014 at 5:29
  • Because i want that value in my codebehind file. I dont want it displayed on the page Commented Jan 10, 2014 at 5:47
  • 2
    they you have to use hiddenfield instead of textbox Commented Jan 10, 2014 at 5:52

5 Answers 5

8
<asp:TextBox ID="txtFlag" runat="server" Visible="False"></asp:TextBox>

Setting visible=false will cause this textbox to not appear in the rendered page. Remove this, and add display:none;

<asp:TextBox ID="txtFlag" runat="server" style="display:none;"></asp:TextBox>
Sign up to request clarification or add additional context in comments.

Comments

5

Try including the ClientIDMode property in your textbox

<asp:TextBox ID="txtFlag" runat="server" Visible="False" 
                                     ClientIDMode="Static"></asp:TextBox>

Comments

4

You are calling javascript before complete document load. Please write your javascript code on document.ready function like this

 <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script type="text/javascript">

        $(document).ready(function() {
        document.getElementById('<%=txtFlag.ClientID %>').value = "Track";
        });

    </script>

And second thing is that use display none in place of visible false or use hidden field control

<asp:TextBox ID="txtFlag" runat="server" style="display:none;"></asp:TextBox>

Comments

3

Solution 1:

Make that textbox as visible=true and try,

When you make a control as visible false, that control will not be loaded in client side and as you knew javascript will be executed on client side itself.

Solution 2:

Add this javascript at the end of the page.

3 Comments

Buddy he dont want to display that so solution 1 will be wrong
At the time of answering he havent gave that information, as you said hiddenfield can be used instead of textbox..
in my case textbox is visible but still value not assign
2
document.getElementById('txtFlag').value='Track'

try this

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.