C#中的自定义Web控件

时间:2014-06-21 06:46:00

标签: c# asp.net .net web user-controls

我创建了一个自定义Web控件,它由DropDownList和TextBox组成。自定义控件正在网页中正确呈现,但在DropDownList中选择的值或在TextBox中输入的值不会进入后面的代码。

这是我的代码:

public class DropDownListWithTextBox : WebControl
{
  private DropDownList dropDownList;    
  private TextBox textBox;
  protected override void CreateChildControls()
  {
        base.CreateChildControls();

        this.dropDownList = new DropDownList();
        this.dropDownList.ID = this.ID; 
        this.dropDownList.Width = 205;
        foreach (ChoiceInfo choice in this.Choices)
        {
            string ctext = string.Format("{0}: {1}", choice.Text, choice.Value);
            ListItem item = new ListItem(ctext, choice.Value);
            item.Selected = choice.Selected;
            this.dropDownList.Items.Add(item);
        }

        this.dropDownList.Items.Add(new ListItem("Other", "Other"));

        this.textBox = new TextBox();
        this.textBox.ID = this.ID + "_ddlTextBox";
        this.textBox.Width = 200;

        this.Controls.Add(this.dropDownList);
        this.Controls.Add(this.textBox);
    }
}

控件正在网页中正确呈现,但在ddl或文本框中选择的值不能在后面的代码中进行访问。 你可以帮我在后面的代码中访问这些值。

case FieldType.DropDownListWithTextBox:

    DropDownListWithTextBox ddl1 = (DropDownListWithTextBox)table.FindControl(field.Name);

    properties.Add(new FeatureProperty(field.Name, ddl1.controlValue()));
 break;

0 个答案:

没有答案