I am trying to populate a TextBox within an ASPX page (C#) with a dropdownlist value using JQuery. I tested the code and it works very well outside of an ASP.Net FormView control. It populates the textbox as advertised. But, as some of you know, you have
to do something different to make it work when the DropDownList control and TextBox control are embedded inside the FormView control. The solution I need is how to use FindControl to make this work. Or, provide the syntax to make this work inside the FormView.
My JavaScript is horrible at best! I gave this a try but it was wrong! Again, I am not professing to be an expert. I don't want to change the javascript that is working! I just want it to work with the FormView.
<script type="text/javascript">
function sethref() {
var Textb = document.getElementById('<%= (FormView1.FindControl("FormView1").FindControl("TextBox1")).ClientID%>');
var parLab = document.getElementById('<%= (FormView1.FindControl("FormView1").FindControl("Dropdown1")).ClientID%>').options[document.getElementById('<%= (FormView1.FindControl("FormView1").FindControl("Dropdown1")).ClientID%>').selectedIndex].text;
if (parLab != '--select deptname--') {
Textb.value = parLab
}
else {
Textb.value = '';
}
}
</script>
But I get this error:
Object reference not set to an instance of an object.
Description:
An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
<script type="text/javascript"> function sethref() { var Textb = document.getElementById(''); var parLab = document.getElementById('').options[document.getElementById('').selectedIndex].text; if (parLab != '--select deptname--') { Textb.value = parLab }
else { Textb.value = ''; } } </script>
Member
6 Points
47 Posts
Populate ASP.Net TextBox with value from DropDownList using javascript
Oct 10, 2020 02:46 PM|uid541112|LINK
I am trying to populate a TextBox within an ASPX page (C#) with a dropdownlist value using JQuery. I tested the code and it works very well outside of an ASP.Net FormView control. It populates the textbox as advertised. But, as some of you know, you have to do something different to make it work when the DropDownList control and TextBox control are embedded inside the FormView control. The solution I need is how to use FindControl to make this work. Or, provide the syntax to make this work inside the FormView. My JavaScript is horrible at best! I gave this a try but it was wrong! Again, I am not professing to be an expert. I don't want to change the javascript that is working! I just want it to work with the FormView.
Member
6 Points
47 Posts
Re: Populate ASP.Net TextBox with value from DropDownList using javascript
Oct 10, 2020 02:51 PM|uid541112|LINK
The errors:
The name TextBox1 does not exist in the currrent context
The name Dropdown1 does not exist in the currrent context
Member
6 Points
47 Posts
Re: Populate ASP.Net TextBox with value from DropDownList using javascript
Oct 10, 2020 02:54 PM|uid541112|LINK
I tried my hand at changing the code to this:
<script type="text/javascript">
function sethref() {
var Textb = document.getElementById('<%= (FormView1.FindControl("FormView1").FindControl("TextBox1")).ClientID%>');
var parLab = document.getElementById('<%= (FormView1.FindControl("FormView1").FindControl("Dropdown1")).ClientID%>').options[document.getElementById('<%= (FormView1.FindControl("FormView1").FindControl("Dropdown1")).ClientID%>').selectedIndex].text;
if (parLab != '--select deptname--') {
Textb.value = parLab
}
else {
Textb.value = '';
}
}
</script>
But I get this error:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
<script type="text/javascript"> function sethref() { var Textb = document.getElementById(''); var parLab = document.getElementById('').options[document.getElementById('').selectedIndex].text; if (parLab != '--select deptname--') { Textb.value = parLab } else { Textb.value = ''; } } </script>