dropdown dynamic binding..

Posted on 2008-11-19
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
    LVL 12

    Accepted Solution

    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
    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

    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
    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

    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?

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now