在PostBack之后,ASP.NET控制类型更改

时间:2015-04-12 15:37:29

标签: c# asp.net textbox postback autopostback

当用户从另一个DropDownList(ddlTypeDemande)中选择一个特定值时,我需要将DropDownList(ddlGouvPick)更改为TextBox(txtGouvPick)。 有一部分代码描述了我的案例:

ASPX页面 DropDownList调用操作

<td>
<asp:DropDownList 
    ID="ddlTypeDemande" 
    runat="server" 
    CssClass="DDDropDown" 
   AutoPostBack="True" OnSelectedIndexChanged="ddlTypeDemande_SelectedIndexChanged"></asp:DropDownList>
</td>

TextBox将放在这里:

<td id="tdGouvLiv" 
runat="server" >
<asp:DropDownList 
ID="ddlGouv_Liv" 
runat="server" 
AutoPostBack="True" 
OnSelectedIndexChanged="ddlGouv_Liv_SelectedIndexChanged">
</asp:DropDownList>
</td>

CodeBehind:

    protected void ddlTypeDemande_SelectedIndexChanged(object sender, EventArgs e)
    {
    switch (ddlTypeDemande.SelectedValue)
      {
        case "I": 

          TextBox vartxtGouv_Liv = new TextBox();
          tdGouvLiv.Controls.Remove(ddlGouv_Colct);
          vartxtGouv_Liv.ID = "txtGouv_Liv";
          vartxtGouv_Liv.CssClass = "DDTextBox";
          vartxtGouv_Liv.Width = 200;
          tdGouvLiv.Controls.Add(vartxtGouv_Liv);
          break;

    case "O": 

         Export(); /*Change the type of another control*/
         break;
     }
  }

问题是当我从页面中的另一个控件调用回发时。我失去了变化。

1 个答案:

答案 0 :(得分:1)

最简单的方法是将文本框和下拉列表控件添加到表单并使用其Visible属性,如下所示:

switch (ddlTypeDemande.SelectedValue)
  {
    case "I": 

      vartxtGouv_Liv = true;
      ddlGouv_Colct.Visible = false;
      break;

case "O": 
      vartxtGouv_Liv = false;
      ddlGouv_Colct.Visible = true;

     Export(); /*Change the type of another control*/
     break;
 }