I am creating a large form which re-uses the same dropdown-list of employees multiple times. it is a dynamic dropdown-list which uses a sqldatasource to get the employee list from a sql server db. Is there any way of creating a control or something else which will help me in this situation. I need to be able to change the ID of the control every time I use the dropdown-list. I dont want to keep creating a separate dropdown-list with sqldatasource 15 times in the same form. Any Ideas???
1 Answer
A user control (.ascx) sounds like exactly what you need.
Check out Walkthrough: Creating Reusable Elements with ASP.NET User Controls
2 Comments
Damien Knox
thanks. I have created a control which is a dynamic dropdown list. how do I access the selected value of the control to insert into db. if it was a normal dropdown list, I would use - 'cmd.Parameters.AddWithValue("@LearnSkills", Dropdownlist1.Text);' but I cant use Dropdownlist1.Text when using a user control.
Karl Anderson
You need to create a read-only (
getter only) property in your user control that will expose the string value of the value selected in the drop down list, like this: public string SelectedValue { get { return dropdownlist1.Text; } }