You must Sign In to post a response.
  • Category: ASP.NET

    Dropdown list selection

    Hi Friends
    I have one drop down list. in that drop down box i have to show only one value at a time.
    for Eg:
    I have one dropdown and one button. in my database i have 38 values. now i can populate those 38 values at a time to dropdown box. but my requirement is i have to show one by one to dropdown box. when i will click button 2nd value should be display in dropdown box, as well as 1st should be removed from dropdowm.


    can any one guide me friends
  • #763146
    Hi,

    for this you need to do more code logic, instead of do in above manner, you want to show one item at a time right? then keep the dropdown index based on button click and disable the dropdown.


    int index=0;
    protected void btnClick(object sender, EventArgs e)
    {
    if(ViewState["index"]==null)
    ViewState["index"]=index;
    else
    index=Convert.ToInt32(ViewState["index"].ToString());

    //now, assign this index to dropdownlist
    //bind your dropdown
    ddl1.SelectedIndex=index;
    index=index+1;
    ViewState["index"]=index;
    }


    I didn't text this, if you got any errors please let me know...

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #763154
    Hello,

    Refer the below code i have made it as per my understanding :

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title></title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:DropDownList ID="DropDownList1" runat="server">
    </asp:DropDownList><br />
    <asp:Button ID="ButtonChangeVal" runat="server" Text="Change Value"
    onclick="ButtonChangeVal_Click" />
    </div>
    </form>
    </body>
    </html>


    Code - behind :

    SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|Database1.mdf;Integrated Security=True;User Instance=True");
    SqlCommand cmd;
    SqlDataReader dr;
    static int rownum = 1;

    protected void ButtonChangeVal_Click(object sender, EventArgs e)
    {
    try
    {
    con.Open();
    cmd = new SqlCommand("SELECT EmpName FROM(SELECT EmpName, ROW_NUMBER() OVER (ORDER BY EmpName) AS RowNum FROM Employees) AS MyDerivedTable WHERE MyDerivedTable.RowNum BETWEEN " + rownum + " AND " + rownum + " ", con);
    dr = cmd.ExecuteReader();
    if (dr.HasRows)
    {
    dr.Read();
    DropDownList1.Items.Clear();
    DropDownList1.Items.Add(dr[0].ToString());
    dr.Dispose();
    rownum += 1;
    }
    else
    {
    con.Close();
    rownum = 1;
    ButtonChangeVal_Click(ButtonChangeVal, null);
    }
    }
    catch (Exception ex)
    {
    Response.Write(ex.Message);
    }
    finally
    {
    con.Close();
    }
    }

    It will fetch one record at a time and add into the dropdown list. When it finds there is no more record. It will restart the reading from 1st row. So it will not cause any issue.

    Hope this is what you are looking for.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"


Sign In to post your comments