Click here to Skip to main content
15,867,305 members
Please Sign up or sign in to vote.
3.60/5 (5 votes)
See more:
I am using following code to set the value of textbox in asp.net using JQuery. But still its not setting the value.

JavaScript
$('<%=txtSStreet.ClientID %>').val("Street");
$('<%=txtSState.ClientID %>').val("City");
$('<%=txtSDistrict.ClientID %>').val("State");
// $('<%=ddlSCountry.ClientID %>').attr("value","Country");
$('<%=txtSZipcode.ClientID %>').val("ZIPCode");
$('<%=txtSPostalAddress.ClientID %>').val("PostalAddress");
Posted
Updated 19-Oct-11 3:40am
v2
Comments
kiran dangar 19-Oct-11 9:41am    
Have you placed your code inside $(document).ready() ??

You can try this way..

JavaScript
$(document).ready(function()
    {
        $('#<%=txtSStreet.ClientID %>').val("Street");
        $('#<%=txtSState.ClientID %>').val("City");
        $('#<%=txtSDistrict.ClientID %>').val("State");
        // $('#<%=ddlSCountry.ClientID %>').attr("value","Country");
        $('#<%=txtSZipcode.ClientID %>').val("ZIPCode");
        $('#<%=txtSPostalAddress.ClientID %>').val("PostalAddress");
    });



Hope this will help...

[Please click on "Accept Answer" if this helps you]
 
Share this answer
 
v3
Comments
JQuery Geeks 24-Oct-11 1:37am    
right way
preet88 23-May-12 7:13am    
how we can access all text boxes can you tell me??
for eg $(.textbox) refers to class of textbox
similarly if i want to refer all textboxes how to do that???
Killzone DeathMan 23-Nov-12 4:49am    
I thing its the same way, just add the class="textbox" and in jquery $('.textbox').val('My Text');
Am I right?
deepak.m.shrma 5-Nov-12 22:41pm    
@preet
$("input[type*='text']") gives you array of text, using 'each' method u can manipulate all textboxes.
ex.
$("input[type*='text']").each(function(count){
$(this).val('count = '+count);
});
Sampath Sridhar 10-Mar-13 23:39pm    
The answer is correct.
Instead of

$('<%=txtSStreet.ClientID %>').val("Street");


try this :

$('#' +'<%=txtSStreet.ClientID %>').val("Street");


Thanks,
Jyotish
 
Share this answer
 
Comments
Member 11031891 23-Aug-14 22:13pm    
This is the correct solution since, in the question, those are IDs you are trying to set. The # indicates an ID, so this must prepend the <%=xxxxxxxx.ClientID %>.
set txtSStreet textbox's clientidmode property to static in aspx page then using jquery
$('#txtSStreet').val('value you want to set');
 
Share this answer
 
Comments
GeraldTrost 16-Nov-14 0:30am    
THIS is the right answer!
other answers came from people who don't deal with ASP.NET
and so they dont have experienced this issue!
JavaScript
$(‘:submit’).click(function () {
$(‘div’).text(‘This is new text.’) ;


This will set the Text value of 'div' element on submit button click.
You can similarly set the textbox value, replace 'div' with '#textbox_id'

Regards,
Abhisheik
 
Share this answer
 
v3
Put $(document).ready function on the top OR u can put $(function(){}); and into the curly braces u can write down your code. Your problem will be resolved.
 
Share this answer
 
hey buddy . u can use below code to find textbox and set its value.

$('#ControlName').val('value');



Please Vote for Answer
 
Share this answer
 
Comments
Shining Legend 4-Oct-10 5:05am    
I already tried this way but it is not working.
Sandip.Nascar 22-Aug-12 13:56pm    
This is a server side control. So, definitely this will not work.
refer an example:

XML
<head runat="server">
    <title></title>
    <script src="scripts/jquery.js" type="text/javascript"></script>
    <script language="javascript" type="text/javascript">
        $(document).ready(function() {
            $('#btn1').click(function() {
                $('#Abc').val('Clicked')
                return false;
            });
            $('#btn2').click(function() {
                $('#Abc').val('Hit')
                return false;
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <input name="Abc" id="Abc" runat="server" />
     <input type="button" runat="server" value="click" id="btn1" />
     <asp:Button ID="btn2" runat="server" Text="Hit" />
    </div>
    </form>
</body>
 
Share this answer
 
Comments
Saiseva 22-Jun-11 16:24pm    
Your code-example is very simple and easy.

This is ami, I am new in JQERY world.

I am learning and I show so many examples, tuorial etc. online.....But all are almost same start with $(document).ready()...WITHOUT IN-DEPT EXPLANATION OF $ and how to use basic syntax...i feeel sometime it is confusing...

More...Mostly Javascript codes are more to do with form's element like getting values..data validations...data manipulation..Less on CSS-HTML

Most of tutorial talks about CSS Element manipulation....Less on HTML ELEMENT DATA-VALUE Manipulation.

Do u have/know any tutorials/LINK/BOOK.......that clears begginners on HTML element side

For example : How to get value of text box, how to compare value of two password control, How to get date value, How do i know which radio button is selected, change text based on selected radio button...

Or tutorial tells..major functions used in data manipulation...

see if i see u r example i got what val function does.....but I can't just go by each function and read sytax...It doesn't provide any dudeligence..

Please let me know if anyone know..Better tutorial/link/book.

my id is ami_sur@yahoo.com

thanks
Ajith K Gatty 7-Apr-14 8:17am    
Try this link http://try.jquery.com

You can also read Javascript and Jquery, The missing manual 2nd edition.

Good Luck
put it in a $(document).ready
 
Share this answer
 
v2
Use this...
C#
$('#ControlName').val('value');
 
Share this answer
 
Comments
Member 10042358 23-Jun-20 13:40pm    
$('#ControlName').val('value'); its ok
but in asp.net code view..cannot get value of textbox
textbox.text

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900