I am using javascript to assign values to a textbox. I love the javascript code because it does not require a postback. I want to continue to use it. Through some research I have gotten rid of the following errors:
The name TextBox1 does not exist in the currrent context
The name Dropdown1 does not exist in the currrent context
The error I get now is:
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.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
I'm hoping someone can help me clean this up and make this work within the FormView control. I'm including my code.
Your code shows a lack of fundamental knowledge that you'll need to fix. Data bound controls have templates. If the data bound control is in edit mode then the edit template is displayed. Update mode -> update template. Default -> Item template.
Your JavaScript code is looking for edit template controls that do not exist in the item template. This is the reason for the null reference error. Drop the JavaScript approach it is not a good design in Web Forms for many reasons.
There are two ways to deal with data bound controls. Programmatically in the code behind or through wizards and SQL Data Source controls within the markup. You are using the wizard approach. That's fine but you have to understand the syntax and terms
to get everything working correctly otherwise it seems like magic. It is very difficult to troubleshoot magic.
The other big design issue with your code is the FormView expects a singe record not collection. You can view the record, update the record, delete the record or create a new record. Your code is tries to select may records. You should have noticed that
only the first record in the list is shown.
I think you'll be interested in the following doc section(s). There's a lot of subject in the left navigation pane that you'll need to read...
I put together an example that might help. It's users and colors. You can edit a user's color, add a user color, or delete a user color. This is not production code. The purpose of this example is to illustrate how to handle dropdowns in a data bound
control using CRUD operations.
CREATE TABLE Colors (
ColorId INT PRIMARY KEY IDENTITY(1,1),
[Name] VARCHAR(64),
)
INSERT INTO Colors([Name])
VALUES ('Red'), ('Yellow'), ('Blue'), ('Green')
CREATE TABLE Users (
UserId INT PRIMARY KEY IDENTITY(1,1),
Username VARCHAR(64)
)
INSERT INTO Users(Username)
VALUES('Bob'), ('Sally'), ('Jane')
CREATE TABLE UserColor(
ColorId INT NOT NULL,
UserId INT NOT NULL
)
INSERT INTO UserColor(ColorId, UserId)
VALUES (1,1)
The formview has absolutely no issues running running CRUD operations. The CRUD operations were automatically generated by the Visual Studio Wizard and, they work. I paid a developer to solve the javascript and it's working fine now. Closing this out.
Member
6 Points
47 Posts
JavaScript to populate TextBox from DropDownList ***(IMPORTANT) Inside a FormView Control
Oct 10, 2020 03:43 PM|uid541112|LINK
I am using javascript to assign values to a textbox. I love the javascript code because it does not require a postback. I want to continue to use it. Through some research I have gotten rid of the following errors:
The name TextBox1 does not exist in the currrent context
The name Dropdown1 does not exist in the currrent context
The error I get now is:
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.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
I'm hoping someone can help me clean this up and make this work within the FormView control. I'm including my code.
All-Star
53131 Points
23681 Posts
Re: JavaScript to populate TextBox from DropDownList ***(IMPORTANT) Inside a FormView Control
Oct 10, 2020 09:25 PM|mgebhard|LINK
Your code shows a lack of fundamental knowledge that you'll need to fix. Data bound controls have templates. If the data bound control is in edit mode then the edit template is displayed. Update mode -> update template. Default -> Item template.
Your JavaScript code is looking for edit template controls that do not exist in the item template. This is the reason for the null reference error. Drop the JavaScript approach it is not a good design in Web Forms for many reasons.
There are two ways to deal with data bound controls. Programmatically in the code behind or through wizards and SQL Data Source controls within the markup. You are using the wizard approach. That's fine but you have to understand the syntax and terms to get everything working correctly otherwise it seems like magic. It is very difficult to troubleshoot magic.
The other big design issue with your code is the FormView expects a singe record not collection. You can view the record, update the record, delete the record or create a new record. Your code is tries to select may records. You should have noticed that only the first record in the list is shown.
I think you'll be interested in the following doc section(s). There's a lot of subject in the left navigation pane that you'll need to read...
https://docs.microsoft.com/en-us/aspnet/web-forms/overview/data-access/accessing-the-database-directly-from-an-aspnet-page/inserting-updating-and-deleting-data-with-the-sqldatasource-vb
Main section
https://docs.microsoft.com/en-us/aspnet/web-forms/overview/data-access/
I put together an example that might help. It's users and colors. You can edit a user's color, add a user color, or delete a user color. This is not production code. The purpose of this example is to illustrate how to handle dropdowns in a data bound control using CRUD operations.
Member
6 Points
47 Posts
Re: JavaScript to populate TextBox from DropDownList ***(IMPORTANT) Inside a FormView Control
Oct 11, 2020 12:00 PM|uid541112|LINK
I am very aware I don't have the fundamental knowledge. I don't think I would be here if I had the FUNDAMENTAL KNOWLEDGE @MGEBHARD!
Member
6 Points
47 Posts
Re: JavaScript to populate TextBox from DropDownList ***(IMPORTANT) Inside a FormView Control
Oct 11, 2020 12:08 PM|uid541112|LINK
The formview has absolutely no issues running running CRUD operations. The CRUD operations were automatically generated by the Visual Studio Wizard and, they work. I paid a developer to solve the javascript and it's working fine now. Closing this out.