【问题标题】:Convert 'Control' to textbox and assign it a value将“控件”转换为文本框并为其赋值
【发布时间】:2012-11-02 00:13:38
【问题描述】:

我正在尝试创建一个表单。表单控件名称总是相同的。但是控制类型会改变。例如,我有一个名为“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;
    }
}

然后我将控件渲染到页面。

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

接下来,我尝试为控件赋值。这是我遇到问题的地方。我知道当它被全局声明时,它被声明为一个控件并保存这些值。

有没有办法在函数内全局声明它或在全局声明后更改控件的类型?

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;
    }        
}

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

【问题讨论】:

  • -1 代表问题,因为代码远不能编译

标签: c# asp.net controls


【解决方案1】:

在您的OnInit 中,您应该使用new TextBox() 而不是new Control() as TextBox,因为第二个只会返回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();
    // ...
}

【讨论】:

    【解决方案2】:

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

    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;
        }        
    }
    

    【讨论】:

    • 不起作用,因为 GetType().ToString() 返回完整的类型名称。
    【解决方案3】:

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

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

    显然 - 查看被覆盖的 Render 方法 - 您正在手动完成许多 ASP.NET Web 窗体为您完成的工作。您可以简单地将ctrl(或任何控件)添加到PageControls 集合中。

    Page.Render 方法会渲染 Controls 集合的所有元素,除非您覆盖它。

    【讨论】:

    • 引用问题:“初始化页面时,它从数据库中检索数据,说明控件的类型(TextBox、DropDownList 或 CheckBox);” - 他正在创建动态控件,而不是静态的
    • @Knaģis 哦,我明白了,我更正了我的答案。过多地研究代码而不是问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-02
    • 2012-12-22
    相关资源
    最近更新 更多