aspx_professional thread: Help with retreiving values from dynamically added controls
maybe you could create a key in Session or reference
a key in ViewState to keep track whether after a
given postback you need to create controls or to
access content that the user put into controls
created after a prior postback.
Lou
-----Original Message-----
From: Montgomery, Aiden [mailto:Aiden.Montgomery@f...]
Sent: Tuesday, November 06, 2001 4:14 AM
To: ASPX_Professional
Subject: [aspx_professional] RE: Help with retreiving values from dyna
mically added controls
On the first postback I get the number of instances of the new objects
from the user input. This means that I cannot create them before
postback as I do not have the required information.
Aiden
-----Original Message-----
From: Louis T Klauder [mailto:lklauder@w...]
Sent: 05 November 2001 18:47
To: ASPX_Professional
Subject: [aspx_professional] RE: Help with retreiving values from dyna
mically added controls
Hi;
I do not have experience adding controls dynamically.
But, why do you not create the control prior to post
back and then access the content after post back?
Lou
-----Original Message-----
From: Montgomery, Aiden [mailto:Aiden.Montgomery@f...]
Sent: Friday, November 02, 2001 11:36 AM
To: ASPX_Professional
Subject: [aspx_professional] RE: Help with retreiving values from dyna
mically added controls
Sorry everyone who is not interested in this.
This is the code that I am using.
It is snippet. I have it in the page load event handler.
if (!Page.IsPostBack)
{
}
else
{
Label rows = new Label();
rows.Text = "How many columns in form section "
+ i.ToString();
int numcells = 1;
TextBox rowText = new TextBox();
rowText.EnableViewState = true;
rowText.ID = "rows" + i.ToString();
myQControl.Controls.Add(rows);
myQControl.Controls.Add(rowText);
myQControl.Controls.Add(new
System.Web.UI.LiteralControl("<br>"));
foreach (Control myQCs in myQControl.Controls)
{
try
{
Type _myControlType
myQCs.GetType();
testLabel.Text +
_myControlType.ToString() + "<br>";
PropertyInfo _myUC_Text
_myControlType.GetProperty("Text");
testLabel.Text +
_myUC_Text.GetValue(myQCs, null).ToString();
}
catch (Exception ex)
{
testLabel.Text += ex.ToString();
}
}
That is what I am using to add the controls and also to try and access
them after the postback. I was told that if I have added something
dynamically to the page then to access it at all I have to add it to the
page again.
This is what that code outputs.
System.Web.UI.WebControls.Label
How many columns in form section 1 System.Web.UI.WebControls.TextBox
System.Web.UI.LiteralControl
So I know that it is managing to retrieve the information from the
Label. Also it is finding the fact that I have a TextBox. I just have no
idea how to get the Value from the text box.
If I change the line :
PropertyInfo _myUC_Text
_myControlType.GetProperty("Text");
to
PropertyInfo _myUC_Text
_myControlType.GetProperty("Value");
I get this.
DetailsSystem.Web.UI.WebControls.Label
System.NullReferenceException: Value null was found where an instance of
an object was required. at ASP.Test_aspx.Page_Load(Object Sender,
EventArgs e) in \Test.aspx:line 107System.Web.UI.WebControls.TextBox
System.NullReferenceException: Value null was found where an instance of
an object was required. at ASP.Test_aspx.Page_Load(Object Sender,
EventArgs e) in \Test.aspx:line 107System.Web.UI.LiteralControl
Which I don't understand as I know that the values that are in the
(Object Sender,EventArgs e) are generated by the .Net framework during
the postback.
Does this help anyone to see any further into the mess that I have?
Or has anyone got any other code that can help me out here.
Thank,
Aiden
-----Original Message-----
From: Louis T Klauder [mailto:lklauder@w...]
Sent: 02 November 2001 15:37
To: ASPX_Professional
Subject: [aspx_professional] RE: Help with retreiving values from dyna
mically added controls
If you put in a statement at the end of the
Page_Load( ... ) routine analogous to
string str_control_text = control.Text ;
, step through it in debug, and then move
your cursor over the word str_control_text
in the code, you should see the value that
was in the control prior to the postback.
Maybe I do not have an adequate grasp of
what you are doing.
Lou
-----Original Message-----
From: Montgomery, Aiden [mailto:Aiden.Montgomery@f...]
Sent: Friday, November 02, 2001 9:38 AM
To: ASPX_Professional
Subject: [aspx_professional] RE: Help with retreiving values from dyna
mically added controls
Sorry if I was not clear but at present there is no user. It is me I am
trying to test my code. How many times I click the button the values are
not retrievable. However as I said the values are still in the
textboxes.
Is there a standard method that I have not found.
Would it be helpful if I posted my entire code sample on here?
Aiden
-----Original Message-----
From: Louis T Klauder [mailto:lklauder@w...]
Sent: 02 November 2001 13:36
To: ASPX_Professional
Subject: [aspx_professional] RE: Help with retreiving values from
dynamically added controls
Did you tell the user to click the button again after
entering the new values ?
Lou
-----Original Message-----
From: Montgomery, Aiden [mailto:Aiden.Montgomery@f...]
Sent: Friday, November 02, 2001 5:47 AM
To: ASPX_Professional
Subject: [aspx_professional] Help with retreiving values from
dynamically added controls
Sorry if this has been covered before but it is frustrating me.
I am going to take a moment to set the scene.
I have a page that has a textbox and a button on it.
You enter a value in the textbox then click the button.
The page reloads with the textbox and the button.
Also now the page has generated a number of placeholders and entered
tables into each one. Also another set of textboxes appear.
The number of placeholders, and therefore tables, and extra text is
determined by the number you entered into the textbox.
The extra text boxes are for you to enter the number of cells you
require in each of the sections.
This is as far as I can get. I simply cannot find a way to get at the
values which are typed into the generated textboxes and therefore cannot
determine how many cells are required in the sections.
What is it that I am over looking?
I have tried accessing the controls through using the findcontrol and
the ID which I have sepcified. Also I have tried to access them by using
a foreach and going through the controls collection.
I have managed to get the actual controls. However not the values which
they hold. Using the stated methods I found my way to the actual control
objects. Once there I cast them to (textbox) and then tried to access
the textbox.value
This returned nothing.
I thought about the fact that because they were created dynamically they
may have to be added to the page again after post back to be able to
access them. This helped in no way at all. I also enabled viewstate and
again nothing.
Someone out there must know where I am going wrong.
Aiden
---
VBug Winter Conference 2001
Make no mistake - there's a great learning curve
for developers moving from COM to .NET. So why
would you want to be a pioneer and cross the bridges
first? Attend the .NET Developer's Conference
(28th to 30th November 2001) In London and realise
the benefits of adopting the technology early. By
attending you'll understand the key challenges and
be left with a thorough understanding of the major
.NET fundamentals. If you're already working
with .NET this is a must-attend event.
http://www.vbug.co.uk/redirect.asp?url=39&id=17
---
You are currently subscribed to
aspx_professional as: aiden.montgomery@f...
$subst('Email.Unsub')
---
VBug Winter Conference 2001
Make no mistake - there's a great learning curve
for developers moving from COM to .NET. So why
would you want to be a pioneer and cross the bridges
first? Attend the .NET Developer's Conference
(28th to 30th November 2001) In London and realise
the benefits of adopting the technology early. By
attending you'll understand the key challenges and
be left with a thorough understanding of the major
.NET fundamentals. If you're already working
with .NET this is a must-attend event.
http://www.vbug.co.uk/redirect.asp?url=39&id=17
---
You are currently subscribed to
aspx_professional as: lklauder@w...
$subst('Email.Unsub')
---
VBug Winter Conference 2001
Make no mistake - there's a great learning curve
for developers moving from COM to .NET. So why
would you want to be a pioneer and cross the bridges
first? Attend the .NET Developer's Conference
(28th to 30th November 2001) In London and realise
the benefits of adopting the technology early. By
attending you'll understand the key challenges and
be left with a thorough understanding of the major
.NET fundamentals. If you're already working
with .NET this is a must-attend event.
http://www.vbug.co.uk/redirect.asp?url=39&id=17
---
You are currently subscribed to
aspx_professional as: aiden.montgomery@f...
$subst('Email.Unsub')
---
VBug Winter Conference 2001
Make no mistake - there's a great learning curve
for developers moving from COM to .NET. So why
would you want to be a pioneer and cross the bridges
first? Attend the .NET Developer's Conference
(28th to 30th November 2001) In London and realise
the benefits of adopting the technology early. By
attending you'll understand the key challenges and
be left with a thorough understanding of the major
.NET fundamentals. If you're already working
with .NET this is a must-attend event.
http://www.vbug.co.uk/redirect.asp?url=39&id=17
---
You are currently subscribed to
aspx_professional as: lklauder@w...
$subst('Email.Unsub')