Passing webcontrols created dynamically on user control placed on a placeholder in a form to another

E

elkay

I am trying to pass Controls dynamically created in UserControl placed
in a placeholder of a page to another page.
How can I get each ID and value of the control in the new page?
Any help will be appreciated.

I have gone through this group and read:
*Creating ASP.NET Server Controls Dynamically....

I am pasting my code for your ref.
<HTML code of Main Form>
<form id="Form1" method="post" runat="server">
<asp:placeHolder id="plh1" runat="server"></asp:placeHolder><br>
<asp:placeHolder id="plh2" runat="server"></asp:placeHolder><br>
<asp:placeHolder id="plh3" runat="server"></asp:placeHolder><br>
<asp:placeHolder id="plh4" runat="server"></asp:placeHolder><br>
<asp:Button ID="btnSubmit" Runat="server" Text="Submit"
OnClick="btnSubmit_Click"></asp:Button>
<asp:Button ID="btnClear" Runat="server" Text="Clear"
OnClick="btnClear_Click"></asp:Button>
</form>
</HTML Code>

<codebehind for Main Form>

string strControlID=null;
private void Page_Load(object sender, System.EventArgs e){
strControlID=Request.Params["ControlID"];
if (!IsPostBack){
plh1Setup(strControlID);
plh2Setup(strControlID);
plh3Setup(strControlID);
plh4Setup(strControlID);}
//Code for Handling Postback here

}
private void plh1Setup (string sControlID) {
oControl1 = LoadControl("Control1.ascx");
plh1.Controls.Clear();
plh1.Controls.Add(oControl1); }

//Similarly for other place holders

protected void btnSubmit_Click(object sender, System.EventArgs e) {
Server.Transfer("ValidationForm.aspx", true); }

</ codebehind for Main Form>


So far everything is being populated as required....
When I click Submit button, how do I take all the values and ID's of
Parent and Children Controls to the "ValidationForm.aspx"

In the "ValidationForm.aspx", I need to validate the control values and
send the response back to user telling him to fix the values of certain
control OR if the values are right with a message saying that values
submitted successfuly (values were right and inserted or updated in
database).

//FOr testing I am trying to read all the ID's of the controls in the
page_Load of "ValidationForm.aspx"
private void Page_Load(object sender, System.EventArgs e) {
foreach (Control c in ????) //How do I get the controlCollection
{
Response.Write("c.Type:"+c.GetType().ToString().ToString() +"<br>");
Response.Write("c.ID:"+c.ID +"<br>");
Response.Write("C.Value:" + ?? +"<br>"); //How do I get the Value
}
}

Any help would be greatly appreciated.
Thank you in advance.
Elkay
 
J

John Saunders

elkay said:
I am trying to pass Controls dynamically created in UserControl placed
in a placeholder of a page to another page.

One doesn't typically pass controls from one page to another. In fact, I've
never seen it done before. What are you trying to accomplish?

John Saunders
 
E

elkay

I have a first page where I have a datagrid. One of the columns is a
hyperlink which contains the params I am passing to the next page.
Based on those params, the next page which has four placeholders and
two buttons gets populated. The four placeholders are added on the
Page_Load event like this:

if (!IsPostBack){
plh1Setup(param1);
plh2Setup(param1);
plh3Setup(param1);
plh4Setup(param1);}
//Code for Handling Postback here
}

private void plh1Setup (string sControlID) {
oControl1 = LoadControl("Control1.ascx");
plh1.Controls.Clear();
plh1.Controls.Add(oControl1); }

Now the code inside the user controls (one of which is Control1.ascx)
which I have created has code for creating dynamic webcontrols like
label, textboxes and radiobuttons the data (Types of control, their
properties etc) for which is coming from the database.

Now I am trying to capture the values entered by the user in these
controls on this page (which has 4 place holders) to the next page,
test those values and either tell the user that submission was ok, or
tell them that there is error in the values they entered and that they
should correct it and resubmit.

Let me know if this makes any sense.
Thank you for your interest.

Elkay
 
J

John Saunders

elkay said:
I have a first page where I have a datagrid. One of the columns is a
hyperlink which contains the params I am passing to the next page.
Based on those params, the next page which has four placeholders and
two buttons gets populated. The four placeholders are added on the
Page_Load event like this:

if (!IsPostBack){
plh1Setup(param1);
plh2Setup(param1);
plh3Setup(param1);
plh4Setup(param1);}
//Code for Handling Postback here
}

private void plh1Setup (string sControlID) {
oControl1 = LoadControl("Control1.ascx");
plh1.Controls.Clear();
plh1.Controls.Add(oControl1); }

Now the code inside the user controls (one of which is Control1.ascx)
which I have created has code for creating dynamic webcontrols like
label, textboxes and radiobuttons the data (Types of control, their
properties etc) for which is coming from the database.

Now I am trying to capture the values entered by the user in these
controls on this page (which has 4 place holders) to the next page,
test those values and either tell the user that submission was ok, or
tell them that there is error in the values they entered and that they
should correct it and resubmit.

Let me know if this makes any sense.

It makes sense to pass the values of those controls, but makes no sense to
pass the controls themselves.


John Saunders
 
E

elkay

You are right John...
I want to get the values of these controls with their ID's, so that I
can do validations.
I am sorry for the confusion.

This is something that I am looking for on the next page (one of my
friends told me to try this):

foreach (Control c in Page.Controls[1].Controls)
{
switch (c.GetType().ToString())
{
case "System.Web.UI.LiteralControl":
string val = ((System.Web.UI.Controls.TextBox)c).Text;
//This line gives error...
break;
//For all the controls....
default:
break;
}
}

Thanks
Elkay
 
E

elkay

You are right John...
I want to get the values of these controls with their ID's, so that I
can do validations.
I am sorry for the confusion.

This is something that I am looking for on the next page (one of my
friends told me to try this):

foreach (Control c in Page.Controls[1].Controls)
{
switch (c.GetType().ToString())
{
case "System.Web.UI.LiteralControl":
string val = ((System.Web.UI.Controls.TextBox)c).Text; //This line
gives error...
break;
//Similarly for all the other controls....
default:
break;
}
}

Thanks
Elkay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top