Click here to Skip to main content
14,384,421 members
Rate this:
Please Sign up or sign in to vote.
See more:
Control added dynamically on a button click (for example Details button) is not accessible in another button click (Submit button) in VB.net

I added a textbox control in one button click (say btnDetails)

It is working perfectly.

When I access the same textbox in another button click (say btnSubmit)

It is showing that Object reference is not set.

How to access the TextBox created?
Kindly help to resolve this.

What I have tried:

I added a textbox control in one button click (say btnDetails).

Dim mytextbox as New TextBox
mytextbox.ID = "textbox1"
mytextbox.Text = "Hello"
MyPanel.Controls.Add(mytextbox)

It is working perfectly.

When I access the same textbox in another button click (say btnSubmit)

TryCast(MyPanel.FindControl("textbox1"), TextBox).Text = "Welcome"

It is showing that Object reference is not set.

How to access the TextBox created?
Kindly help to resolve this.
Posted
Updated 6-Jan-19 22:45pm
Comments
F-ES Sitecore 7-Jan-19 5:27am
   
Controls you create yourself are not persisted across postbacks so when you click btnSubmit the postback happens and your dynamic controls no longer exist. You need to re-add your dynamic controls on every postback.
Member 14102257 7-Jan-19 7:09am
   
Yes Sir. This is the issue. Thanks. Is there any other solution to access the controls without re-adding them?

To fix this, I changed the dynamic controls creation part from button click event to page load event(added new page) and removed (if not ispostback then) condition.

But I just want to know if any other possibilities to solve this in button click itself without re-adding the controls on every postback.
F-ES Sitecore 7-Jan-19 7:27am
   
Not really, that's just how webforms works. When you put a control on an aspx page Visual Studio adds code to the designer file to create that control which is why the controls are available in the first place. If you're adding your own controls that .net doesn't know about then you have to replicate what .net does for controls on the aspx page and that is to create them all with each postback.
Member 14102257 7-Jan-19 7:41am
   
Ok. Thank you Sir
Rate this:
Please Sign up or sign in to vote.

Solution 1

This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterdays shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, VS will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, VS will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

Quote:
Control added dynamically on a button click (for example details button) is not accessible in another button click (submit button) in VB.NET

Without seeing a consistent piece of code, it is impossible to even guess what you really do.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

Visual Basic / Visual Studio Video Tutorial - Basic Debugging - YouTube[^]
Visual Basic .NET programming for Beginners - Breakpoints and Debugging Tools[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100