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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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!

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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 ThakurTechnical 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?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.