0

I want to write a custom control involving a DropDownList and a TextBox. Actually, I want to dynamically render DropDownList and TextBox. For example: when a user clicks a Checkbox, the Textbox will change to a DropdownList. On the other hand, when a user deselects the Checkbox, the Dropdownlist will change to a Textbox.

I know this can be done using two controls, which sets the visibility for both control. But can I do it on a custom control?

chridam
  • 100,957
  • 23
  • 236
  • 235
user998405
  • 1,329
  • 5
  • 41
  • 84

2 Answers2

1

If you still want to go with that approach, here is your code.

In Design File:-

 <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True"
 oncheckedchanged="CheckBox1_CheckedChanged" />

 <div id ="control" runat="server">

 </div>

In Code Behind File:-

protected void Page_Load(object sender, EventArgs e)
{
  if (!Page.IsPostBack)
   {
     TextBox txt = new TextBox();
     txt.ID = "txt";
     control.Controls.Add(txt);
   }
}
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
   if (CheckBox1.Checked)
    {
       for (int ix = this.Controls.Count - 1; ix >= 0; ix--)
           if (this.Controls[ix] is TextBox) this.Controls[ix].Dispose();

       DropDownList ddl = new DropDownList();
       ddl.ID = "ddl";

      control.Controls.Add(ddl);
    }
    else
    {
      for (int ix = this.Controls.Count - 1; ix >= 0; ix--)
          if (this.Controls[ix] is DropDownList) this.Controls[ix].Dispose();

       TextBox txt = new TextBox();
       txt.ID = "txt";

       control.Controls.Add(txt);
    }
}

Hope this is what you were looking for.

RL89
  • 1,866
  • 5
  • 22
  • 39
  • The dispose is totally unnecessary, all is diposed at the end of the page life-cycle automatically. Actually you won't find the proviously added control anymore at the next postback. – Tim Schmelter Sep 18 '12 at 09:13
  • You haven't Looked at this Stack thread....http://stackoverflow.com/questions/2014286/removing-dynamically-created-controls-c-sharp – RL89 Sep 18 '12 at 09:15
  • ...and you have overlooked the ASP.NET tag. What Hans suggests on your link is for Winforms only. – Tim Schmelter Sep 18 '12 at 09:16
  • But note that even if you would remove the dispose part, your approach wouldn't work as expected. Especially Events and ViewState wouldn't work since the control must be recreated in `page_load` at the latest(`CheckedChanged` is too late to recreate the old, but ok for the new). – Tim Schmelter Sep 18 '12 at 09:19
0

You could try this code

ASPX

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="dynamicControl.ascx.cs" Inherits="dynamicControl" %>
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" 
    oncheckedchanged="CheckBox1_CheckedChanged" />
<asp:DropDownList ID="DropDownList1" runat="server" visible="false">
</asp:DropDownList>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

CodeBehind

protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
    DropDownList1.Visible = CheckBox1.Checked;
    TextBox1.Visible = !CheckBox1.Checked;
}

This snippet will show a dropDownList if CheckBox is checked and change to TextBox if it's not checked. Despite this is possible I don't think this is the right approach. (eg: AutoPostBack needed, set visibility...)

What do you try to achieve?

Pilgerstorfer Franz
  • 8,303
  • 3
  • 41
  • 54