HOW: Read dynamic control on postback

  • Thread starter Thread starter JP
  • Start date Start date
J

JP

I created a page that has dynamically created TextBox or DropDownList controls.

However once the text box is filled in and I submit the page, I cannot find
the dynamic control. I have been all over the net looking for an answer. I
read the article from 4GuysFromRolla but it still does not answer my question.

How to I retrieve the values from a dynamically created control on postback?



This was the name of the control I added to a GridView (AJAX call)

TextBox controlTextBox = new TextBox();
controlTextBox.EnableViewState = true;
controlTextBox.ID = "txt" + drControls["controlName"].ToString();
controlTextBox.MaxLength = 6;
controlTextBox.Width = 150;
controlTextBox.Enabled = true;
controlTextBox.Visible = true;
controlTextBox.Attributes.Add("runat", "server"); //just for the heck of it
e.Row.Cells[2].Controls.Add(controlTextBox);

(All is well at this point and the control is visible on the page)

if(IsPostBack)
{
//Control never found
string Answer= ((TextBox)GridView.FindControl("txtTransID")).Text;
}

I understand that the control itself (GUI representation) needs to be
recreated, but should not the control properties be in the viewstate to
access them?

PLEASE HELP! Does anyone have a working example? I could care less that the
controls get re-rendered on the postback as they get redirected to a
different screen after the data saves, but I really need to just access the
value
 
Hello JP,
I created a page that has dynamically created TextBox or DropDownList
controls.

However once the text box is filled in and I submit the page, I cannot
find the dynamic control. I have been all over the net looking for an
answer. I read the article from 4GuysFromRolla but it still does not
answer my question.

How to I retrieve the values from a dynamically created control on
postback?

This was the name of the control I added to a GridView (AJAX call)

TextBox controlTextBox = new TextBox();
controlTextBox.EnableViewState = true;
controlTextBox.ID = "txt" + drControls["controlName"].ToString();
controlTextBox.MaxLength = 6;
controlTextBox.Width = 150;
controlTextBox.Enabled = true;
controlTextBox.Visible = true;
controlTextBox.Attributes.Add("runat", "server"); //just for the
heck of it
e.Row.Cells[2].Controls.Add(controlTextBox);
(All is well at this point and the control is visible on the page)

if(IsPostBack)
{
//Control never found
string Answer=
((TextBox)GridView.FindControl("txtTransID")).Text;
}
I understand that the control itself (GUI representation) needs to be
recreated, but should not the control properties be in the viewstate
to access them?

PLEASE HELP! Does anyone have a working example? I could care less
that the controls get re-rendered on the postback as they get
redirected to a different screen after the data saves, but I really
need to just access the value

The control is added to a naming container (the grid), which makes sure the
control name is unique per row (each row will contain the specified textbox).

So you'll have to query the specific row of the grid control in order to
use FindControl.

eg

foreach (GridRow r in Grid.Rows)
{
r.FindControl("txtTransID");
}

should do the trick
 
You are absolutely correct. I mistakenly typed my example in this message.
This statement is inside a foreach loop that should have read as follows:


foreach (GridViewRow gvr in gridQuestions.Rows)
{
//txtTransID is the dynamically generated control but this always
returns null or error
string a = ((TextBox)gvr.FindControl("txtTransID")).Text;
}


--
JP
..NET Software Developer


Jesse Houwing said:
Hello JP,
I created a page that has dynamically created TextBox or DropDownList
controls.

However once the text box is filled in and I submit the page, I cannot
find the dynamic control. I have been all over the net looking for an
answer. I read the article from 4GuysFromRolla but it still does not
answer my question.

How to I retrieve the values from a dynamically created control on
postback?

This was the name of the control I added to a GridView (AJAX call)

TextBox controlTextBox = new TextBox();
controlTextBox.EnableViewState = true;
controlTextBox.ID = "txt" + drControls["controlName"].ToString();
controlTextBox.MaxLength = 6;
controlTextBox.Width = 150;
controlTextBox.Enabled = true;
controlTextBox.Visible = true;
controlTextBox.Attributes.Add("runat", "server"); //just for the
heck of it
e.Row.Cells[2].Controls.Add(controlTextBox);
(All is well at this point and the control is visible on the page)

if(IsPostBack)
{
//Control never found
string Answer=
((TextBox)GridView.FindControl("txtTransID")).Text;
}
I understand that the control itself (GUI representation) needs to be
recreated, but should not the control properties be in the viewstate
to access them?

PLEASE HELP! Does anyone have a working example? I could care less
that the controls get re-rendered on the postback as they get
redirected to a different screen after the data saves, but I really
need to just access the value

The control is added to a naming container (the grid), which makes sure the
control name is unique per row (each row will contain the specified textbox).

So you'll have to query the specific row of the grid control in order to
use FindControl.

eg

foreach (GridRow r in Grid.Rows)
{
r.FindControl("txtTransID");
}

should do the trick
 
Hello JP,

Did you re-add the control to the grid before reading it's value? A dynamically
added crontrol will need to be added back to the control tree on every subsequent
request, should you want to access its value...

Jesse
You are absolutely correct. I mistakenly typed my example in this
message. This statement is inside a foreach loop that should have read
as follows:

foreach (GridViewRow gvr in gridQuestions.Rows)
{
//txtTransID is the dynamically generated control but this
always
returns null or error
string a = ((TextBox)gvr.FindControl("txtTransID")).Text;
}
Jesse Houwing said:
Hello JP,
I created a page that has dynamically created TextBox or
DropDownList controls.

However once the text box is filled in and I submit the page, I
cannot find the dynamic control. I have been all over the net
looking for an answer. I read the article from 4GuysFromRolla but it
still does not answer my question.

How to I retrieve the values from a dynamically created control on
postback?

This was the name of the control I added to a GridView (AJAX call)

TextBox controlTextBox = new TextBox();
controlTextBox.EnableViewState = true;
controlTextBox.ID = "txt" + drControls["controlName"].ToString();
controlTextBox.MaxLength = 6;
controlTextBox.Width = 150;
controlTextBox.Enabled = true;
controlTextBox.Visible = true;
controlTextBox.Attributes.Add("runat", "server"); //just for the
heck of it
e.Row.Cells[2].Controls.Add(controlTextBox);
(All is well at this point and the control is visible on the page)
if(IsPostBack)
{
//Control never found
string Answer=
((TextBox)GridView.FindControl("txtTransID")).Text;
}
I understand that the control itself (GUI representation) needs to
be
recreated, but should not the control properties be in the viewstate
to access them?
PLEASE HELP! Does anyone have a working example? I could care less
that the controls get re-rendered on the postback as they get
redirected to a different screen after the data saves, but I really
need to just access the value
The control is added to a naming container (the grid), which makes
sure the control name is unique per row (each row will contain the
specified textbox).

So you'll have to query the specific row of the grid control in order
to use FindControl.

eg

foreach (GridRow r in Grid.Rows)
{
r.FindControl("txtTransID");
}
should do the trick
 
So what you are saying is that after the user clicks submit (postback), I
still have to add the control back to the collection, before I can see what
the user typed? If thats true, how does the view state know that the
properties belong to the dynamic control?

I assume this is where the protected override void OnInit(EventArgs e) comes
in to play? In this particular situation, I do want to maintain the value for
postback solely for the purposes of storing it in the database, but I do not
want to re-render the page after the postback.

--
JP
..NET Software Developer


Jesse Houwing said:
Hello JP,

Did you re-add the control to the grid before reading it's value? A dynamically
added crontrol will need to be added back to the control tree on every subsequent
request, should you want to access its value...

Jesse
You are absolutely correct. I mistakenly typed my example in this
message. This statement is inside a foreach loop that should have read
as follows:

foreach (GridViewRow gvr in gridQuestions.Rows)
{
//txtTransID is the dynamically generated control but this
always
returns null or error
string a = ((TextBox)gvr.FindControl("txtTransID")).Text;
}
Jesse Houwing said:
Hello JP,

I created a page that has dynamically created TextBox or
DropDownList controls.

However once the text box is filled in and I submit the page, I
cannot find the dynamic control. I have been all over the net
looking for an answer. I read the article from 4GuysFromRolla but it
still does not answer my question.

How to I retrieve the values from a dynamically created control on
postback?

This was the name of the control I added to a GridView (AJAX call)

TextBox controlTextBox = new TextBox();
controlTextBox.EnableViewState = true;
controlTextBox.ID = "txt" + drControls["controlName"].ToString();
controlTextBox.MaxLength = 6;
controlTextBox.Width = 150;
controlTextBox.Enabled = true;
controlTextBox.Visible = true;
controlTextBox.Attributes.Add("runat", "server"); //just for the
heck of it
e.Row.Cells[2].Controls.Add(controlTextBox);
(All is well at this point and the control is visible on the page)
if(IsPostBack)
{
//Control never found
string Answer=
((TextBox)GridView.FindControl("txtTransID")).Text;
}
I understand that the control itself (GUI representation) needs to
be
recreated, but should not the control properties be in the viewstate
to access them?
PLEASE HELP! Does anyone have a working example? I could care less
that the controls get re-rendered on the postback as they get
redirected to a different screen after the data saves, but I really
need to just access the value

The control is added to a naming container (the grid), which makes
sure the control name is unique per row (each row will contain the
specified textbox).

So you'll have to query the specific row of the grid control in order
to use FindControl.

eg

foreach (GridRow r in Grid.Rows)
{
r.FindControl("txtTransID");
}
should do the trick
 
Hello JP,
So what you are saying is that after the user clicks submit
(postback), I still have to add the control back to the collection,
before I can see what the user typed? If thats true, how does the
view state know that the properties belong to the dynamic control?

The value has been added to the viewstate in the same tree structure as the
controls were in. The control will get the right values from the viewstate
as long as you add it as follows:

TextBox tb = new TextBox();
tb.ID = "id";
row.Add(tb);

e.g. It is important to set the ID before adding the control to the tree.

Anything else, you need to set after adding the control to the control tree,
that way anything which was written from ViewState will succesfully be read
and applied, before any other changes are done to the control.
I assume this is where the protected override void OnInit(EventArgs e)
comes in to play? In this particular situation, I do want to maintain
the value for postback solely for the purposes of storing it in the
database, but I do not want to re-render the page after the postback.

The Grid usually has a RowCreated event, which will be fired when the control
tree is being recreated. That is the moment you can safely add the control
and re-apply eventsubscriptions for teh specific control. This will also
allow any event you subscribed to to be fired after Page_Load.

Then later in a databinding event, or in or after the Page_Load, you can
change any value in the control or remove it altogether.

Jesse
Jesse Houwing said:
Hello JP,

Did you re-add the control to the grid before reading it's value? A
dynamically added crontrol will need to be added back to the control
tree on every subsequent request, should you want to access its
value...

Jesse
You are absolutely correct. I mistakenly typed my example in this
message. This statement is inside a foreach loop that should have
read as follows:

foreach (GridViewRow gvr in gridQuestions.Rows)
{
//txtTransID is the dynamically generated control but this
always
returns null or error
string a = ((TextBox)gvr.FindControl("txtTransID")).Text;
}
:
Hello JP,

I created a page that has dynamically created TextBox or
DropDownList controls.

However once the text box is filled in and I submit the page, I
cannot find the dynamic control. I have been all over the net
looking for an answer. I read the article from 4GuysFromRolla but
it still does not answer my question.

How to I retrieve the values from a dynamically created control on
postback?

This was the name of the control I added to a GridView (AJAX call)

TextBox controlTextBox = new TextBox();
controlTextBox.EnableViewState = true;
controlTextBox.ID = "txt" + drControls["controlName"].ToString();
controlTextBox.MaxLength = 6;
controlTextBox.Width = 150;
controlTextBox.Enabled = true;
controlTextBox.Visible = true;
controlTextBox.Attributes.Add("runat", "server"); //just for the
heck of it
e.Row.Cells[2].Controls.Add(controlTextBox);
(All is well at this point and the control is visible on the page)
if(IsPostBack)
{
//Control never found
string Answer=
((TextBox)GridView.FindControl("txtTransID")).Text;
}
I understand that the control itself (GUI representation) needs to
be
recreated, but should not the control properties be in the
viewstate
to access them?
PLEASE HELP! Does anyone have a working example? I could care less
that the controls get re-rendered on the postback as they get
redirected to a different screen after the data saves, but I
really
need to just access the value
The control is added to a naming container (the grid), which makes
sure the control name is unique per row (each row will contain the
specified textbox).

So you'll have to query the specific row of the grid control in
order to use FindControl.

eg

foreach (GridRow r in Grid.Rows)
{
r.FindControl("txtTransID");
}
should do the trick
 
Ok, this might pose a problem then if the dynamic controls are in a GridView.

In the page life cycle Page_Init occurs before Page_Load with viewsate
becoming available just before Page_Load occurs.

The way I understand it is that on postback ,the dynamic controls have be
added to the back to the collection in the Page_Init method. Since this
method occurs before the Page_Load method, there is no GridView object to add
the controls back to because the postback data wont regenerate the gridview
until it executes the Page_Load Method.






--
JP
..NET Software Developer


Jesse Houwing said:
Hello JP,
So what you are saying is that after the user clicks submit
(postback), I still have to add the control back to the collection,
before I can see what the user typed? If thats true, how does the
view state know that the properties belong to the dynamic control?

The value has been added to the viewstate in the same tree structure as the
controls were in. The control will get the right values from the viewstate
as long as you add it as follows:

TextBox tb = new TextBox();
tb.ID = "id";
row.Add(tb);

e.g. It is important to set the ID before adding the control to the tree.

Anything else, you need to set after adding the control to the control tree,
that way anything which was written from ViewState will succesfully be read
and applied, before any other changes are done to the control.
I assume this is where the protected override void OnInit(EventArgs e)
comes in to play? In this particular situation, I do want to maintain
the value for postback solely for the purposes of storing it in the
database, but I do not want to re-render the page after the postback.

The Grid usually has a RowCreated event, which will be fired when the control
tree is being recreated. That is the moment you can safely add the control
and re-apply eventsubscriptions for teh specific control. This will also
allow any event you subscribed to to be fired after Page_Load.

Then later in a databinding event, or in or after the Page_Load, you can
change any value in the control or remove it altogether.

Jesse
Jesse Houwing said:
Hello JP,

Did you re-add the control to the grid before reading it's value? A
dynamically added crontrol will need to be added back to the control
tree on every subsequent request, should you want to access its
value...

Jesse

You are absolutely correct. I mistakenly typed my example in this
message. This statement is inside a foreach loop that should have
read as follows:

foreach (GridViewRow gvr in gridQuestions.Rows)
{
//txtTransID is the dynamically generated control but this
always
returns null or error
string a = ((TextBox)gvr.FindControl("txtTransID")).Text;
}
:
Hello JP,

I created a page that has dynamically created TextBox or
DropDownList controls.

However once the text box is filled in and I submit the page, I
cannot find the dynamic control. I have been all over the net
looking for an answer. I read the article from 4GuysFromRolla but
it still does not answer my question.

How to I retrieve the values from a dynamically created control on
postback?

This was the name of the control I added to a GridView (AJAX call)

TextBox controlTextBox = new TextBox();
controlTextBox.EnableViewState = true;
controlTextBox.ID = "txt" + drControls["controlName"].ToString();
controlTextBox.MaxLength = 6;
controlTextBox.Width = 150;
controlTextBox.Enabled = true;
controlTextBox.Visible = true;
controlTextBox.Attributes.Add("runat", "server"); //just for the
heck of it
e.Row.Cells[2].Controls.Add(controlTextBox);
(All is well at this point and the control is visible on the page)
if(IsPostBack)
{
//Control never found
string Answer=
((TextBox)GridView.FindControl("txtTransID")).Text;
}
I understand that the control itself (GUI representation) needs to
be
recreated, but should not the control properties be in the
viewstate
to access them?
PLEASE HELP! Does anyone have a working example? I could care less
that the controls get re-rendered on the postback as they get
redirected to a different screen after the data saves, but I
really
need to just access the value
The control is added to a naming container (the grid), which makes
sure the control name is unique per row (each row will contain the
specified textbox).

So you'll have to query the specific row of the grid control in
order to use FindControl.

eg

foreach (GridRow r in Grid.Rows)
{
r.FindControl("txtTransID");
}
should do the trick
 
Hello JP,
Ok, this might pose a problem then if the dynamic controls are in a
GridView.

In the page life cycle Page_Init occurs before Page_Load with viewsate
becoming available just before Page_Load occurs.

The way I understand it is that on postback ,the dynamic controls have
be added to the back to the collection in the Page_Init method. Since
this method occurs before the Page_Load method, there is no GridView
object to add the controls back to because the postback data wont
regenerate the gridview until it executes the Page_Load Method.

It's always tricky to find the right spot... At some point EnsureChildControls();
is called on the page. After that it should be in the right state. You can
override EnsureChildControls to add the controls where needed at the right
time. The grid control will make sure it will have empty rows added, no data
just yet. That's the time to ensure your textboxes get in there as well.

JEsse
Jesse Houwing said:
Hello JP,
So what you are saying is that after the user clicks submit
(postback), I still have to add the control back to the collection,
before I can see what the user typed? If thats true, how does the
view state know that the properties belong to the dynamic control?
The value has been added to the viewstate in the same tree structure
as the controls were in. The control will get the right values from
the viewstate as long as you add it as follows:

TextBox tb = new TextBox();
tb.ID = "id";
row.Add(tb);
e.g. It is important to set the ID before adding the control to the
tree.

Anything else, you need to set after adding the control to the
control tree, that way anything which was written from ViewState will
succesfully be read and applied, before any other changes are done to
the control.
I assume this is where the protected override void OnInit(EventArgs
e) comes in to play? In this particular situation, I do want to
maintain the value for postback solely for the purposes of storing
it in the database, but I do not want to re-render the page after
the postback.
The Grid usually has a RowCreated event, which will be fired when the
control tree is being recreated. That is the moment you can safely
add the control and re-apply eventsubscriptions for teh specific
control. This will also allow any event you subscribed to to be fired
after Page_Load.

Then later in a databinding event, or in or after the Page_Load, you
can change any value in the control or remove it altogether.

Jesse
:

Hello JP,

Did you re-add the control to the grid before reading it's value? A
dynamically added crontrol will need to be added back to the
control tree on every subsequent request, should you want to access
its value...

Jesse

You are absolutely correct. I mistakenly typed my example in this
message. This statement is inside a foreach loop that should have
read as follows:

foreach (GridViewRow gvr in gridQuestions.Rows)
{
//txtTransID is the dynamically generated control but this
always
returns null or error
string a = ((TextBox)gvr.FindControl("txtTransID")).Text;
}
:
Hello JP,

I created a page that has dynamically created TextBox or
DropDownList controls.

However once the text box is filled in and I submit the page, I
cannot find the dynamic control. I have been all over the net
looking for an answer. I read the article from 4GuysFromRolla
but it still does not answer my question.

How to I retrieve the values from a dynamically created control
on postback?

This was the name of the control I added to a GridView (AJAX
call)

TextBox controlTextBox = new TextBox();
controlTextBox.EnableViewState = true;
controlTextBox.ID = "txt" +
drControls["controlName"].ToString();
controlTextBox.MaxLength = 6;
controlTextBox.Width = 150;
controlTextBox.Enabled = true;
controlTextBox.Visible = true;
controlTextBox.Attributes.Add("runat", "server"); //just for
the
heck of it
e.Row.Cells[2].Controls.Add(controlTextBox);
(All is well at this point and the control is visible on the
page)
if(IsPostBack)
{
//Control never found
string Answer=
((TextBox)GridView.FindControl("txtTransID")).Text;
}
I understand that the control itself (GUI representation) needs
to
be
recreated, but should not the control properties be in the
viewstate
to access them?
PLEASE HELP! Does anyone have a working example? I could care
less
that the controls get re-rendered on the postback as they get
redirected to a different screen after the data saves, but I
really
need to just access the value
The control is added to a naming container (the grid), which
makes sure the control name is unique per row (each row will
contain the specified textbox).

So you'll have to query the specific row of the grid control in
order to use FindControl.

eg

foreach (GridRow r in Grid.Rows)
{
r.FindControl("txtTransID");
}
should do the trick
 
I think I finally got this thing figured out. Granted it takes a bit of code.

Basically I’m generating the dynamic controls in the gridview.RowDataBound()
Then in GridView.DataBound() if EVENTTARGET that generates the GridView is
what fired the AJAX post back, I’m cycling though all the controls for each
row in the given cell and adding them to a Control[row#,control object] type
array so I can maintain all the properties that were set. I then store this
array in a session.

Then on post back, I check the page EVENTTARGET to ensure that the object
that generated the full post back was the submit button otherwise I don’t
want to waste time cycling through the array.

I make sure the session value is not null and convert it back to the
Control[,] array. I cycling though the array OnPage_Load, I simply cycle
though GridViewRows and do Controls.Add to the cell using the object stored
in the array.

So far seems to work. I have some other tweaks I need to make though but
I’ve gotten over a major hurdle.

You have been a huge help.




--
JP
..NET Software Developer


Jesse Houwing said:
Hello JP,
Ok, this might pose a problem then if the dynamic controls are in a
GridView.

In the page life cycle Page_Init occurs before Page_Load with viewsate
becoming available just before Page_Load occurs.

The way I understand it is that on postback ,the dynamic controls have
be added to the back to the collection in the Page_Init method. Since
this method occurs before the Page_Load method, there is no GridView
object to add the controls back to because the postback data wont
regenerate the gridview until it executes the Page_Load Method.

It's always tricky to find the right spot... At some point EnsureChildControls();
is called on the page. After that it should be in the right state. You can
override EnsureChildControls to add the controls where needed at the right
time. The grid control will make sure it will have empty rows added, no data
just yet. That's the time to ensure your textboxes get in there as well.

JEsse
Jesse Houwing said:
Hello JP,

So what you are saying is that after the user clicks submit
(postback), I still have to add the control back to the collection,
before I can see what the user typed? If thats true, how does the
view state know that the properties belong to the dynamic control?

The value has been added to the viewstate in the same tree structure
as the controls were in. The control will get the right values from
the viewstate as long as you add it as follows:

TextBox tb = new TextBox();
tb.ID = "id";
row.Add(tb);
e.g. It is important to set the ID before adding the control to the
tree.

Anything else, you need to set after adding the control to the
control tree, that way anything which was written from ViewState will
succesfully be read and applied, before any other changes are done to
the control.

I assume this is where the protected override void OnInit(EventArgs
e) comes in to play? In this particular situation, I do want to
maintain the value for postback solely for the purposes of storing
it in the database, but I do not want to re-render the page after
the postback.

The Grid usually has a RowCreated event, which will be fired when the
control tree is being recreated. That is the moment you can safely
add the control and re-apply eventsubscriptions for teh specific
control. This will also allow any event you subscribed to to be fired
after Page_Load.

Then later in a databinding event, or in or after the Page_Load, you
can change any value in the control or remove it altogether.

Jesse

:

Hello JP,

Did you re-add the control to the grid before reading it's value? A
dynamically added crontrol will need to be added back to the
control tree on every subsequent request, should you want to access
its value...

Jesse

You are absolutely correct. I mistakenly typed my example in this
message. This statement is inside a foreach loop that should have
read as follows:

foreach (GridViewRow gvr in gridQuestions.Rows)
{
//txtTransID is the dynamically generated control but this
always
returns null or error
string a = ((TextBox)gvr.FindControl("txtTransID")).Text;
}
:
Hello JP,

I created a page that has dynamically created TextBox or
DropDownList controls.

However once the text box is filled in and I submit the page, I
cannot find the dynamic control. I have been all over the net
looking for an answer. I read the article from 4GuysFromRolla
but it still does not answer my question.

How to I retrieve the values from a dynamically created control
on postback?

This was the name of the control I added to a GridView (AJAX
call)

TextBox controlTextBox = new TextBox();
controlTextBox.EnableViewState = true;
controlTextBox.ID = "txt" +
drControls["controlName"].ToString();
controlTextBox.MaxLength = 6;
controlTextBox.Width = 150;
controlTextBox.Enabled = true;
controlTextBox.Visible = true;
controlTextBox.Attributes.Add("runat", "server"); //just for
the
heck of it
e.Row.Cells[2].Controls.Add(controlTextBox);
(All is well at this point and the control is visible on the
page)
if(IsPostBack)
{
//Control never found
string Answer=
((TextBox)GridView.FindControl("txtTransID")).Text;
}
I understand that the control itself (GUI representation) needs
to
be
recreated, but should not the control properties be in the
viewstate
to access them?
PLEASE HELP! Does anyone have a working example? I could care
less
that the controls get re-rendered on the postback as they get
redirected to a different screen after the data saves, but I
really
need to just access the value
The control is added to a naming container (the grid), which
makes sure the control name is unique per row (each row will
contain the specified textbox).

So you'll have to query the specific row of the grid control in
order to use FindControl.

eg

foreach (GridRow r in Grid.Rows)
{
r.FindControl("txtTransID");
}
should do the trick
 
Hello JP,
I think I finally got this thing figured out. Granted it takes a bit
of code.

Basically I'm generating the dynamic controls in the
gridview.RowDataBound() Then in GridView.DataBound() if EVENTTARGET
that generates the GridView is what fired the AJAX post back, I'm
cycling though all the controls for each row in the given cell and
adding them to a Control[row#,control object] type array so I can
maintain all the properties that were set. I then store this array in
a session.

Then on post back, I check the page EVENTTARGET to ensure that the
object that generated the full post back was the submit button
otherwise I don't want to waste time cycling through the array.

I make sure the session value is not null and convert it back to the
Control[,] array. I cycling though the array OnPage_Load, I simply
cycle though GridViewRows and do Controls.Add to the cell using the
object stored in the array.

So far seems to work. I have some other tweaks I need to make though
but I've gotten over a major hurdle.

You have been a huge help.

Instead of adding them to the session, I'd put it in the viewstate, as the
data is page specific.

It was a pleasure helping :).

Jesse
Jesse Houwing said:
Hello JP,
Ok, this might pose a problem then if the dynamic controls are in a
GridView.

In the page life cycle Page_Init occurs before Page_Load with
viewsate becoming available just before Page_Load occurs.

The way I understand it is that on postback ,the dynamic controls
have be added to the back to the collection in the Page_Init method.
Since this method occurs before the Page_Load method, there is no
GridView object to add the controls back to because the postback
data wont regenerate the gridview until it executes the Page_Load
Method.
It's always tricky to find the right spot... At some point
EnsureChildControls(); is called on the page. After that it should be
in the right state. You can override EnsureChildControls to add the
controls where needed at the right time. The grid control will make
sure it will have empty rows added, no data just yet. That's the time
to ensure your textboxes get in there as well.

JEsse
:

Hello JP,

So what you are saying is that after the user clicks submit
(postback), I still have to add the control back to the
collection, before I can see what the user typed? If thats true,
how does the view state know that the properties belong to the
dynamic control?

The value has been added to the viewstate in the same tree
structure as the controls were in. The control will get the right
values from the viewstate as long as you add it as follows:

TextBox tb = new TextBox();
tb.ID = "id";
row.Add(tb);
e.g. It is important to set the ID before adding the control to the
tree.
Anything else, you need to set after adding the control to the
control tree, that way anything which was written from ViewState
will succesfully be read and applied, before any other changes are
done to the control.

I assume this is where the protected override void
OnInit(EventArgs e) comes in to play? In this particular
situation, I do want to maintain the value for postback solely for
the purposes of storing it in the database, but I do not want to
re-render the page after the postback.

The Grid usually has a RowCreated event, which will be fired when
the control tree is being recreated. That is the moment you can
safely add the control and re-apply eventsubscriptions for teh
specific control. This will also allow any event you subscribed to
to be fired after Page_Load.

Then later in a databinding event, or in or after the Page_Load,
you can change any value in the control or remove it altogether.

Jesse

:

Hello JP,

Did you re-add the control to the grid before reading it's value?
A dynamically added crontrol will need to be added back to the
control tree on every subsequent request, should you want to
access its value...

Jesse

You are absolutely correct. I mistakenly typed my example in
this message. This statement is inside a foreach loop that
should have read as follows:

foreach (GridViewRow gvr in gridQuestions.Rows)
{
//txtTransID is the dynamically generated control but this
always
returns null or error
string a = ((TextBox)gvr.FindControl("txtTransID")).Text;
}
:
Hello JP,

I created a page that has dynamically created TextBox or
DropDownList controls.

However once the text box is filled in and I submit the page,
I cannot find the dynamic control. I have been all over the
net looking for an answer. I read the article from
4GuysFromRolla but it still does not answer my question.

How to I retrieve the values from a dynamically created
control on postback?

This was the name of the control I added to a GridView (AJAX
call)

TextBox controlTextBox = new TextBox();
controlTextBox.EnableViewState = true;
controlTextBox.ID = "txt" +
drControls["controlName"].ToString();
controlTextBox.MaxLength = 6;
controlTextBox.Width = 150;
controlTextBox.Enabled = true;
controlTextBox.Visible = true;
controlTextBox.Attributes.Add("runat", "server"); //just for
the
heck of it
e.Row.Cells[2].Controls.Add(controlTextBox);
(All is well at this point and the control is visible on the
page)
if(IsPostBack)
{
//Control never found
string Answer=
((TextBox)GridView.FindControl("txtTransID")).Text;
}
I understand that the control itself (GUI representation)
needs
to
be
recreated, but should not the control properties be in the
viewstate
to access them?
PLEASE HELP! Does anyone have a working example? I could care
less
that the controls get re-rendered on the postback as they get
redirected to a different screen after the data saves, but I
really
need to just access the value
The control is added to a naming container (the grid), which
makes sure the control name is unique per row (each row will
contain the specified textbox).

So you'll have to query the specific row of the grid control in
order to use FindControl.

eg

foreach (GridRow r in Grid.Rows)
{
r.FindControl("txtTransID");
}
should do the trick
 
Back
Top