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

Discussion in 'ASP .Net Web Controls' started by elkay, Dec 22, 2004.

  1. elkay

    elkay Guest

    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
    elkay, Dec 22, 2004
    #1
    1. Advertising

  2. "elkay" <> wrote in message
    news:...
    >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
    John Saunders, Dec 22, 2004
    #2
    1. Advertising

  3. elkay

    elkay Guest

    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
    elkay, Dec 22, 2004
    #3
  4. "elkay" <> wrote in message
    news:...
    >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
    John Saunders, Dec 22, 2004
    #4
  5. elkay

    elkay Guest

    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
    elkay, Dec 22, 2004
    #5
  6. elkay

    elkay Guest

    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
    elkay, Dec 22, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Sm9l?=
    Replies:
    1
    Views:
    3,272
    Peter Rilling
    Dec 13, 2005
  2. Replies:
    0
    Views:
    2,041
  3. msimmons
    Replies:
    0
    Views:
    454
    msimmons
    Jul 16, 2009
  4. nail

    Build control in a placeholder or like a placeholder

    nail, Sep 10, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    321
  5. Guest
    Replies:
    1
    Views:
    102
    Steven Cheng[MSFT]
    Dec 25, 2006
Loading...

Share This Page