dropdown dynamic binding..

I have a dropdown list that i need to create dynamicly from stor proc and fill it. Then depending on user selects from the dropdown i need to fill the gridview.. Can anyone give me detail example how to do that please? Thanks
Who is Participating?
techExtremeConnect With a Mentor Commented:
Dim myddl as new DropDownList()

myddl.ID=ddlDay" & i
'give it datasource and call databind event after setting text and value fields.

'Now add this dropdown to some collection  like if you have a table, add this control to one particular table cell etc...

'attach event handler
AddHandler myddl.SelectedIndexChanged, AddressOf DropDownLists_SelectedIndexChanged

'Now create a sub with the name DropDownLists_SelectedIndexChanged  which has arguments same as any dropdown list's selectedindexchanged event needs.

Then  write code in there to fill your gridview based on selected value of the dropdown
It should work perfectly.

Note: Create the dropdown in preinit event of your page/user control as it will resolve any issue you have with your dropdown not being created, also the code for creating dropdown must run on every postback i.e. you must not write it in 'if not ispostback' type of condition.

Hope it helped!
Anurag ThakurTechnical ManagerCommented:
DropDownList in data binding -

add the following to the aspx page in the dropdown
OnSelectedIndexChanged="DDL_SelectedIndexChanged" AutoPostBack="True"

and in the code behind add the follwoing
protected void DDL_SelectedIndexChanged(object sender, EventArgs e)
      //check here and then do the database call for getting the data and then bind it to the gridview
1jawsAuthor Commented:
ok i want to see my grid to be filled when user see the page , because dropdown gets filled with data and i want to see the gird to be filled accordinly to first value from the dripdown, when i see the page at the first time
how to do that
i have tried preinit, prerender events but couldnt get them fired before page load for some reason
Anurag ThakurConnect With a Mentor Technical ManagerCommented:
you do that in page load event
when you fill the dropdown on the page load and pre select the value after that you call the database with that value, get the datatable/dataset and bind the gridview with that data

-- suggestion
create a function in which you pass the value (as selected in the dropdown) and in that function you do the db call and data binding with the gridview
this will put all you db call logic and databinding logic at one place which will be more maintainable
1jawsAuthor Commented:
I am a newbie on this but i managed to fill the dropdown from database by selecting datasource and datatextfield at the aspx page, and i can see dropdown is getting filled fine at the page loads but i need now is the fill the gridview with the selected value to pass the gridview at the page load, but i cant do this, for some reason it doesnt get filled at the page load first time page loads dropdown gets filled only not grid, i have tried the do it on the different events like preinit event but i cannot get the preinit fired with the master page , it is not happening for some reason. So, what should i do?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.