Gives a error: "Microsoft JScript runtime error: 'document.getElementById(...)' is null or not an object". The field text field, txtTest DOES exist. can someone please tell me what going on?
Also.. how can I use sqlName variable in asp? For example, using it to build the Delete, Select, Update, and Insert Commands in a gridview:
One of the most common causes of something like this is the Client ID issue. If you have a Master Page (or this is part of a User Control), the elements will be rendered to the browser with a modified id. ASP.NET prefixes elements that have runat="server"
to sort of protect us from ourselves. You can verify if this is, infact, the case by doing a view source of the page after you load it to see if the textbox has the id you expect.
One thing you might consider is using the ClientID property like so:
var sqlName = '<%= Session["tmp_SQLTableNames"] %>';
window.document.getElementById('<%= txtTest.ClientID%>').value = sqlName;
Another consideration is to control the ID. In .NET 4.0, they added the ability to control ClientIDs a little more. Check here for further details:
You'll have to get an understanding of when javascript executes versus when the server executes. Basically all server code executes first to create html that is then delievered over the pipes to the client machine. Once that is done, the server essentially
forgets everything about what it just did because it needs to standby for other requests. Once the browser gets the html document, it begins loading it in and executing javascript as it comes across it. Any time there is a postback, form submittal or ajax
request, the sessionID is sent up, the server takes it, grabs any session information associated with that ID and the cycle continues.
I explain this to illustrate that everything happens sequentially. Javascript and Server aren't running concurrently. I may need to get a larger view of what you are trying to do, but one thing to consider is to have a hidden input that can be set by javascript.
Then, have the user submit the form through some kind of postback. The SQL command can then extract the information from the hidden input to complete its query.
WARNING: Because you are concatenating a string variable into your SQL query, it is vulnerable to SQL Injection Attacks. Though that is a different discussion entirely.
"Dream as if you'll live forever, live as if you'll die today." --James Dean
bflagg
Member
19 Points
73 Posts
how to use javascript variable is a aspx page?
May 15, 2012 05:04 PM|LINK
Subject is main question..
Something simple like this:
<script type="text/javascript"> var sqlName = '<%= Session["tmp_SQLTableNames"] %>'; window.document.getElementById('txtTest').value = sqlName; </script>Gives a error: "Microsoft JScript runtime error: 'document.getElementById(...)' is null or not an object".
The field text field, txtTest DOES exist. can someone please tell me what going on?
Also.. how can I use sqlName variable in asp?
For example, using it to build the Delete, Select, Update, and Insert Commands in a gridview:
any ideas?
AceCorban
Star
12358 Points
2274 Posts
Re: how to use javascript variable is a aspx page?
May 15, 2012 05:15 PM|LINK
One of the most common causes of something like this is the Client ID issue. If you have a Master Page (or this is part of a User Control), the elements will be rendered to the browser with a modified id. ASP.NET prefixes elements that have runat="server" to sort of protect us from ourselves. You can verify if this is, infact, the case by doing a view source of the page after you load it to see if the textbox has the id you expect.
One thing you might consider is using the ClientID property like so:
var sqlName = '<%= Session["tmp_SQLTableNames"] %>'; window.document.getElementById('<%= txtTest.ClientID%>').value = sqlName;Another consideration is to control the ID. In .NET 4.0, they added the ability to control ClientIDs a little more. Check here for further details:
http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientidmode.aspx
Of course, this all assumes that the client ID is the issue. You'll need to first verify this.
bflagg
Member
19 Points
73 Posts
Re: how to use javascript variable is a aspx page?
May 16, 2012 01:44 AM|LINK
I verified the field id is correct.
The var sqlName is holding the correct value I want, but I don't know how to use it.
ie... (from grid view stuff) selectCommad="SELECT * FROM "; + sqlName
This fails because I can't use the js var..... Can it be done?
AceCorban
Star
12358 Points
2274 Posts
Re: how to use javascript variable is a aspx page?
May 16, 2012 04:42 PM|LINK
You'll have to get an understanding of when javascript executes versus when the server executes. Basically all server code executes first to create html that is then delievered over the pipes to the client machine. Once that is done, the server essentially forgets everything about what it just did because it needs to standby for other requests. Once the browser gets the html document, it begins loading it in and executing javascript as it comes across it. Any time there is a postback, form submittal or ajax request, the sessionID is sent up, the server takes it, grabs any session information associated with that ID and the cycle continues.
I explain this to illustrate that everything happens sequentially. Javascript and Server aren't running concurrently. I may need to get a larger view of what you are trying to do, but one thing to consider is to have a hidden input that can be set by javascript. Then, have the user submit the form through some kind of postback. The SQL command can then extract the information from the hidden input to complete its query.
WARNING: Because you are concatenating a string variable into your SQL query, it is vulnerable to SQL Injection Attacks. Though that is a different discussion entirely.
roopeshreddy
All-Star
20155 Points
3328 Posts
Re: how to use javascript variable is a aspx page?
May 17, 2012 09:29 AM|LINK
Hi,
You can use RegisterClientScriptBlock() or RegisterStartupScript() to achieve your goal!
You can pass the server side values to client side by using the above options!
Check the following link for more information!
http://www.codeproject.com/Articles/11098/Use-Call-RegisterStartUpScript-RegisterClientScrip
Hope it helps u...
Roopesh Reddy C
Roopesh's Space