C# 将“控件”转换为文本框并为其赋值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13362904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:19:09  来源:igfitidea点击:

Convert 'Control' to textbox and assign it a value

c#asp.netcontrols

提问by user1663870

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.

我正在尝试创建一个表单。表单控件名称始终相同。然而,控制的类型会改变。例如,我有一个名为“first_name”的控件。页面初始化时,它从数据库中检索数据,说明控件的类型(TextBox、DropDownList 或 CheckBox);然后在页面上动态创建表单。为了保持视图状态,我在 OnInit 方法中创建了控件。

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.

请提供一种方法,我可以做到这一点。

采纳答案by Kna?is

In your OnInityou should do new TextBox()and not new Control() as TextBoxsince the second will just return null.

在你的OnInit你应该做new TextBox()而不是new Control() as TextBox因为第二个只会返回null

Your Page_Loadwould be something like this:

Page_Load会是这样的:

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();
    // ...
}

回答by Tyler Lee

To access the type specific properties, you'll need to cast first_nameto the appropriate type within your switch/case statement.

要访问特定于类型的属性,您需要first_name在 switch/case 语句中强制转换为适当的类型。

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

回答by Matthias Meid

You can use Controlas the static type, and use another variable in each case:

您可以Control用作静态类型,并在每个中使用另一个变量case

Control ctrl;
switch(type)
{
  case "TextBox":
    var box = new TextBox();
    box.Text = "...";
    ctrl = box;
    break;
}
Page.Controls.Add(ctrl);

Apparently - looking at the overridden Rendermethod - you're doing a lot of stuff by hand that ASP.NET Web Forms do for you. You can simply add ctrl(or any control) to the Page's Controlscollection.

显然 - 查看重写的Render方法 - 您正在手动执行 ASP.NET Web 窗体为您所做的很多事情。您可以简单地将ctrl(或任何控件)添加到PageControls集合中。

The Page.Rendermethods renders all elements of the Controlscollection, unless you override it.

这些Page.Render方法呈现Controls集合的所有元素,除非您覆盖它。