Learn how to a build a cloud-first strategyRegister Now


dropdown dynamic binding..

Posted on 2008-11-19
Medium Priority
Last Modified: 2013-11-07
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
Question by:1jaws
  • 2
  • 2
LVL 12

Accepted Solution

techExtreme earned 1000 total points
ID: 22997379
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!
LVL 26

Expert Comment

by:Anurag Thakur
ID: 22997481
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

Author Comment

ID: 22999849
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
LVL 26

Assisted Solution

by:Anurag Thakur
Anurag Thakur earned 1000 total points
ID: 23001280
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

Author Comment

ID: 23001577
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?

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Loops Section Overview
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month20 days, 14 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question