Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8817161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:46:48+00:00 2026-06-14T04:46:48+00:00

I am trying to create a form. The form controls names are always going

  • 0

I am trying to create a form. The form controls names are always going to be the same. However the type of control will change. For example, I have a control named “first_name”. When the page is initialized, it retrieves data from a database stating what the type of control (TextBox, DropDownList or CheckBox); Then the form is dynamically created on the page. To keep the viewstate, I created the control in the OnInit method.

protected override void OnInit(EventArgs e)
{
    Control first_name;
    string ControlType = GridView1.Rows[0].Cells[1].Text;
    switch (ControlType)
    {
        case("TextBox"):
            first_name = new Control() as TextBox; first_name.ID = "first_name"; this.Controls.Add(first_name);
            break;
        case("DropDownList"):
            first_name = new Control() as DropDownList; first_name.ID = "first_name"; this.Controls.Add(first_name);
            break;
        case("CheckBox"):
            first_name = new Control() as CheckBox; first_name.ID = "first_name"; this.Controls.Add("first_name");
            break;
    }
}

Then I render the control to the page.

protected override void Render(HtmlTextWriter writer)
{
    writer.Write("first_name: ");
    first_name.RenderControl(writer);
}

Next, I try to assign values to the control. This is where I run into problems. I know that when it’s declared globally, it is declared as a control and holds those values.

Is there a way to declare it globally from within a function or to change the type of a control after it’s been declared globally?

protected void Page_Load(object sender, EventArgs e)
{
    switch (first_name.GetType().ToString())
    {
        case ("TextBox"):
            first_name.Text = "Your First Name...";
            break;
        case ("DropDownList"):
            first_name.Items.Add("Jason");
            first_name.Items.Add("Kelly");
            first_name.Items.Add("Keira");
            first_name.Items.Add("Sandi");
            first_name.Items.Add("Gary");
            break;
        case ("CheckBox"):
            first_name.Checked = true;
            break;
    }        
}

Please provide a way that I can accomplish this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T04:46:49+00:00Added an answer on June 14, 2026 at 4:46 am

    In your OnInit you should do new TextBox() and not new Control() as TextBox since the second will just return null.

    Your Page_Load would be something like this:

    if (first_name is TextBox)
        ((TextBox)first_name).Text = "Your First Name...";
    else if (first_name is DropDownList)
    {
        var drp = (DropDownList)first_name;
        drp.Items.Add();
        // ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a form with webbrowser control. I am trying to
I am trying to create dynamic controls (Label and combobox) in a WPF form,
I am trying to create a form in Sharepoint using C#. I don't know
I am trying to create a form with many groups containing many radio buttons.
I am trying to create a form using only JavaScript. I mean create a
I'm trying to create a form that based on the users input would determine
I'm trying to create a form that clients would complete via the web, so
Im a Titanium Beginner who is trying to create a form page with 2
Using C#, I'm trying to create a form with a dataGridView that shows the
I am new to zend. I am trying to create login form using zend

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.