how to display data in DropdownList

Last post 07-02-2009 10:37 AM by vjxp. 11 replies.

Sort Posts:

  • how to display data in DropdownList

    06-30-2009, 1:26 PM
    • Member
      4 point Member
    • vjxp
    • Member since 07-25-2007, 1:05 PM
    • Posts 30

    Hi

    I’ve a table like this in SqlServer

     

    SITE    STATUS

    One      active

    Two     inactive

    Three   process

     

    I’ve a web form, with 2 Dropdown lists & one Save button

     

    If I select “Two” from DropdownList1,

    I want to display “inactive” from DropdownList2 along with other statuses

    If I select “active” from DropdownList2 & click Save button I want to save that

    Modified data

     

    Please advice

  • Re: how to display data in DropdownList

    06-30-2009, 2:05 PM
    • Contributor
      5,624 point Contributor
    • RatheeshC
    • Member since 04-25-2008, 6:05 PM
    • Posts 1,198

     Hi,

    Assuming that you are binding the data from table to the dropdown and they are in the order you written here..


    protected void ddl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (ddl1.SelectedItem.Text)
            {
                case "One":
                    ddl2.SelectedIndex = 0;
                case "Two":
                    ddl2.SelectedIndex = 1;
                case "Three":
                    ddl2.SelectedIndex = 2;
            }
        }
        protected void Btn_Click(object sender, EventArgs e)
        {
            string qry = "Update tablename set Status='" + ddl2.SelectedItem.Text + "' where status ='inactive'";

    //      Write  the code here to update the DB
        }

    Thanks

    Thanks
    Ratheesh

    Please mark it as answer if it resolves your issue.
  • Re: how to display data in DropdownList

    06-30-2009, 6:43 PM
    • Star
      11,863 point Star
    • shahed.kazi
    • Member since 07-09-2008, 2:15 AM
    • Sydney, Australia
    • Posts 2,330
  • Re: how to display data in DropdownList

    06-30-2009, 7:21 PM
    • Star
      9,470 point Star
    • hans_v
    • Member since 01-29-2007, 4:03 PM
    • Posts 1,642

    vjxp:

    I’ve a web form, with 2 Dropdown lists & one Save button

     

    If I select “Two” from DropdownList1,

    I want to display “inactive” from DropdownList2 along with other statuses

    If I select “active” from DropdownList2 & click Save button I want to save that

    Modified data

     

    Well, that's quit simple. The trick is to put the second dropdownlist and save button in a formview, which is bound to the first dropdown. when you select a site from the first dropdown, the formview will select the same record, fill the second dropdownlist and set the status. and when you set the defaultmode of the formview to edit it is exactly what you want, without even one single line of code!!!!

            <asp:DropDownList ID="siteDropDownList" runat="server" DataSourceID="DDL_DataSource" DataTextField="site" DataValueField="siteID" AutoPostBack="true">
            </asp:DropDownList>
            <asp:FormView ID="FormView1" runat="server" DataKeyNames="siteID" DataSourceID="FV_DataSource" DefaultMode="Edit">
                <EditItemTemplate>
                    <asp:DropDownList ID="statusDropDownList" runat="server" SelectedValue='<%# Bind("status") %>'>
                        <asp:ListItem Value="active" Text="active" />
                        <asp:ListItem Value="inactive" Text="inactive" />
                        <asp:ListItem Value="process" Text="process" />
                    </asp:DropDownList>
                    <br />
                    <asp:Button ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Save" />
                </EditItemTemplate>
            </asp:FormView>
    
            <asp:SqlDataSource ID="DDL_DataSource" runat="server" 
                ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
                ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" 
                SelectCommand="SELECT [siteID], [site] FROM [site]"/>
    
            <asp:SqlDataSource ID="FV_DataSource" runat="server" 
                ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
                ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" 
                SelectCommand="SELECT siteID, status FROM site WHERE (siteID = @siteID)" 
                UpdateCommand="UPDATE site SET status = @status WHERE (siteID = @siteID)">
                <SelectParameters>
                    <asp:ControlParameter ControlID="siteDropDownList" PropertyName="SelectedValue" Name="siteID" Type="Int32" />
                </SelectParameters>
                <UpdateParameters>
                    <asp:Parameter Name="status" Type="String" />
                    <asp:Parameter Name="siteID" Type="Int32" />
                </UpdateParameters>
            </asp:SqlDataSource>
    


     

  • Re: how to display data in DropdownList

    06-30-2009, 7:24 PM
    • Star
      9,470 point Star
    • hans_v
    • Member since 01-29-2007, 4:03 PM
    • Posts 1,642

    RatheeshC:
    string qry = "Update tablename set Status='" + ddl2.SelectedItem.Text + "' where status ='inactive'";
     

    This will update all records with status 'inactive', not only the record that was selected....

  • Re: how to display data in DropdownList

    06-30-2009, 7:46 PM
    • Contributor
      5,624 point Contributor
    • RatheeshC
    • Member since 04-25-2008, 6:05 PM
    • Posts 1,198

    Hi,
    i was in assumption that you have only 3 rows..ok
    modify the query

    string qry = "Update tablename set Status='" + ddl2.SelectedItem.Text + "' where Site='" + ddl1.SelectedItem.Text +"'";

     

    Thanks

    Thanks
    Ratheesh

    Please mark it as answer if it resolves your issue.
  • Re: how to display data in DropdownList

    06-30-2009, 7:58 PM
    • Star
      9,470 point Star
    • hans_v
    • Member since 01-29-2007, 4:03 PM
    • Posts 1,642

    RatheeshC:
    i was in assumption that you have only 3 rows..ok
     

    Even with 3 rows more than 1 could have the same status Wink

  • Re: how to display data in DropdownList

    06-30-2009, 10:07 PM
    • Member
      4 point Member
    • vjxp
    • Member since 07-25-2007, 1:05 PM
    • Posts 30

    I've more than 3 rows

  • Re: how to display data in DropdownList

    06-30-2009, 10:21 PM
    • Contributor
      5,624 point Contributor
    • RatheeshC
    • Member since 04-25-2008, 6:05 PM
    • Posts 1,198

    Have you tried the modified query..?

    string qry = "Update tablename set Status='" + ddl2.SelectedItem.Text + "' where Site='" + ddl1.SelectedItem.Text +"'";

    Thanks
    Ratheesh

    Please mark it as answer if it resolves your issue.
  • Re: how to display data in DropdownList

    06-30-2009, 11:10 PM
    • Member
      4 point Member
    • vjxp
    • Member since 07-25-2007, 1:05 PM
    • Posts 30

    i'd like to display data in "statusDropDownList"  from a database

    instead of giving like <asp:ListItem Value="active" Text="active" />

  • Re: how to display data in DropdownList

    07-01-2009, 3:41 AM
    Answer
    • Star
      9,470 point Star
    • hans_v
    • Member since 01-29-2007, 4:03 PM
    • Posts 1,642

    vjxp:
    i'd like to display data in "statusDropDownList"  from a database
     

    I've given you a complete ample, you could have tried this yourself. Anyway, waht about this?

            <asp:DropDownList ID="siteDropDownList" runat="server" DataSourceID="DDL1_DataSource"
                DataTextField="site" DataValueField="siteID" AutoPostBack="true"/>
            <asp:FormView ID="FormView1" runat="server" DataKeyNames="siteID"
                DataSourceID="FV_DataSource" DefaultMode="Edit">
                <EditItemTemplate>
                    <asp:DropDownList ID="statusDropDownList" runat="server" SelectedValue='<%# Bind("status") %>'
                        DataSourceID="DDL2_DataSource" DataTextField="status" DataValueField="status" />
                    <br />
                    <asp:Button ID="UpdateButton" runat="server" CausesValidation="True"
                        CommandName="Update" Text="Save" />
                </EditItemTemplate>
            </asp:FormView>
    
            <asp:SqlDataSource ID="DDL1_DataSource" runat="server" 
                ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
                ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" 
                SelectCommand="SELECT [siteID], [site] FROM [site]"/>
    
            <asp:SqlDataSource ID="DDL2_DataSource" runat="server" 
                ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
                ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" 
                SelectCommand="SELECT [status] FROM [status]"/>
    
            <asp:SqlDataSource ID="FV_DataSource" runat="server" 
                ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
                ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" 
                SelectCommand="SELECT siteID, status FROM site WHERE (siteID = @siteID)" 
                UpdateCommand="UPDATE site SET status = @status WHERE (siteID = @siteID)">
                <SelectParameters>
                    <asp:ControlParameter ControlID="siteDropDownList" PropertyName="SelectedValue"
                        Name="siteID" Type="Int32" />
                </SelectParameters>
                <UpdateParameters>
                    <asp:Parameter Name="status" Type="String" />
                    <asp:Parameter Name="siteID" Type="Int32" />
                </UpdateParameters>
            </asp:SqlDataSource>

    You only need to change the fieldnames according to your table and fieldnames, and specify a connectionstring (n this example it is called Connectionstring) in web.config....

  • Re: how to display data in DropdownList

    07-02-2009, 10:37 AM
    • Member
      4 point Member
    • vjxp
    • Member since 07-25-2007, 1:05 PM
    • Posts 30

    Thanks Hans for your reply

    this is what i am expecting. i'll apply this logic into my project

    once again thanks 

Page 1 of 1 (12 items)