0

I am looking for this over the internet but I am not getting any suitable answers. Here is my problem:

I have a web user control which has the following three controls

<asp:RadioButtonList ID="RadioButtonList1" runat="server"     AutoPostBack="true" 
CssClass="radioButton" RepeatDirection="Horizontal" RepeatLayout="Flow" 
onselectedindexchanged="RadioButtonList1_SelectedIndexChanged" >
    <asp:ListItem Value="UET111">Use Existing</asp:ListItem>
    <asp:ListItem Value="CNT111">Create New</asp:ListItem>
</asp:RadioButtonList>

<asp:DropDownList ID="DropDownList1" runat="server" >
</asp:DropDownList>
<asp:TextBox ID="TextBox1" runat="server" C></asp:TextBox>

It's code behind

 protected void Page_Load(object sender, EventArgs e)
    {
        DropDownList1.Visible = false;
        TextBox1.Visible = false;
    }

    protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (RadioButtonList1.SelectedItem.Value == "UET")
        {
            DropDownList1.Visible = true;
            TextBox1.Visible = false;
        }
        else if (RadioButtonList1.SelectedItem.Value == "CNT")
        {
            DropDownList1.Visible = false;
            TextBox1.Visible = true;
        }
    }

Now my question- Actually I am using this web control 5 times on the same aspx page. The problem is I want to fill the dropdown list from aspx page and each dropdown will have different data(as I have 5 user control).

Is it possible to do that? Please help me with these and let me know if anyone knows a better way of doing this.

Rebecca
  • 159
  • 1
  • 4
  • 13

1 Answers1

1

Create a public function in your user control that lets you send in a DataSource for the dropdown list to populate from, and if you want bind as well.

public BindDropdownDataSource(DataSet dataSource)
{
    DropDownList1.DataSource = dataSource;
    DropDownList1.DataBind();
}
Camilo
  • 345
  • 1
  • 6
  • Thank you sir for your response:) So is it possible to populate it for all 5 user controls? – Rebecca Sep 02 '15 at 20:37
  • Yeah, you'd just do something like: userControl1.BindDropdownDataSource(dataSource1); userControl2.BindDropdonwDataSource(dataSource2); etc – Camilo Sep 02 '15 at 20:39
  • NO, this won't work. The moment you databind ... dropdown will have item changes. – Rahul Sep 02 '15 at 20:39
  • What I can do if I do not want to query database? I mean I want to populate them without database for now? – Rebecca Sep 02 '15 at 20:41
  • Just populate the datasource with any sort of enumerable list. Such as a dictionary. Check out this post: http://stackoverflow.com/questions/805595/c-sharp-dropdownlist-with-a-dictionary-as-datasource?rq=1 – Camilo Sep 02 '15 at 20:46
  • ohh ... that's great I will try that.:) Thank you so much – Rebecca Sep 02 '15 at 20:50
  • @Camilo: I have one more question, can I ask? – Rebecca Sep 02 '15 at 21:29
  • One problem is that when I am clicking on radio button of other user control the DropDownList of previously selected radio button disappears? Why it's happening? – Rebecca Sep 02 '15 at 21:49
  • Is there autopostbacks involved? – Camilo Sep 03 '15 at 21:38