Click here to Skip to main content
16,016,306 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Everyone,

I am new in asp.net and facing a strange issue. I have textBox inside gridView.

I want to get the textBox value using JavaScript. I have gone through the solution available in internet and tried below line.

var value = document.getElementById("<% = txtNewQuizID.ClientID %>").value;

For now leave about getting the value, after this line, the page itself is not opening. its showing below message.

Uh-oh, something went wrong! Error Code: 500

I tried to find out whats wrong in this line, so I commented the line but still same issue. Then in commented line, i have removed "<%" and "%>" then page opens properly. So the issue is because of the <% and %>. Please tell solution how to get value of textBox. and whats wrong in above line which i tried.
Posted
Updated 29-Jan-15 8:31am
v2
Comments
Sergey Alexandrovich Kryukov 29-Jan-15 14:48pm    
No, it is not because of that. This is because you are using trial-and-error approach instead of understanding.
This line (with %%) looks find, but the behavior also depends on other things.

So far, you did not provide enough information to solve the problem. Read in ClientID and — important! — ClientIDMode.

One of the reasons why you observe what you observe is that you hard coded in HTML:
id="txtNewQuizID.ClientID"
Could it be possible? Please check up. If this is so, I guess you know how to fix it.

—SA

1 solution

Please see my comment to the question: so far, you did not provide enough information. You need to show that element, to do the least.

In the meanwhile, read this thoroughly:
https://msdn.microsoft.com/en-us/library/system.web.ui.control.clientid%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.web.ui.clientidmode(v=vs.110).aspx[^].

But at this moment, I'm not sure you understand what happens on server side and on client site in such cases. Make sure you understand it.

—SA
 
Share this answer
 
Comments
Upadhyay Praveen 29-Jan-15 15:12pm    
My requirement is :- I have a gridview, where based on dropdown value i need to disable/enable the next control that is textBox.
I tried below code.

<script language = "javascript" type = "text/javascript">

function disableQuizID(txtNewQuizID, ddlNewCategory) {

if (ddlNewCategory.value == "4") {
alert("Testing1"); //Testing purpose.
document.getElementById(txtNewQuizID).disabled = true;
alert("Testing2");

}
else {
document.getElementById(txtNewQuizID).disabled = false;
}
}
</script >

while creating GridView in aspx page, I have given OnRowCreated="grdQuestions_RowCreated"

Now aspx.cs

protected void grdQuestions_RowCreated(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer)
{
DropDownList ddlNewCategory = (DropDownList)e.Row.FindControl("ddlNewCategory");
TextBox txtNewQuizID = (TextBox)e.Row.FindControl("txtNewQuizID");

ddlNewCategory.Attributes.Add("onchange", "disableQuizID('" + txtNewQuizID.ClientID + "', this);");

}
}

The dropdown has 4 values, If the selected value is 4 then the Textbox should be disabled/readonly. I tested above code, when i select value 4 in dropdown, the alert("testing1") comes, but alert("testing2") dont come. Please help. I am trying to find out from long time, did not get the solution. Please let me know if you want more info.
Sergey Alexandrovich Kryukov 29-Jan-15 15:47pm    
Please, move this code to the question using "Improve question" and format propertly.
Where is the sample of HTML?
What txtNewQuizID in Javascript equals to? It it equal to real HTML id?

Look, the problem is simple, but you are making rounds instead of addressing the issue. Did you see "view/source" in browser? Did you use the debugger? Do you handle Javascript exceptions? Do something real. You are not even answering my questions.

It's all just around having some id attribute of some HTML element and addressing this id in Javascript code, and some possible mismatch.

—SA

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