转换“控制”到文本框,并为其分配一个值 [英] Convert 'Control' to textbox and assign it a value

查看:89
本文介绍了转换“控制”到文本框,并为其分配一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个表单。表单控件的名字总是会是相同的。然而控制的类型将发生变化。例如,我有一个名为FIRST_NAME控制。当页面被初始化,它从数据库中陈述检索数据什么的控制(文本框,DropDownList的或复选框)的类型;然后形式是动态页面上创建的。为了保持视图状态,我创造了OnInit方法控制。

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.

推荐答案

在你的的OnInit 你应该做的新文本框(),而不是新的控制()作为文本框,因为第二个将只返回

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

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

这篇关于转换“控制”到文本框,并为其分配一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆