Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends,

I am loading data into dropdownlist from sql as shown below:


C#
if (!IsPostBack)
 
            {
 
               string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
 da =new SqlDataAdapter("sp_LabelNames", connStr);
 
               DataSet ds = new DataSet();
 
                da.Fill(ds);
 
                lblAdmitto.Text = ds.Tables[0].Rows[0][1].ToString();
 
                lblVitalSigns.Text = ds.Tables[0].Rows[1][1].ToString();
 
                lblActivity.Text = ds.Tables[0].Rows[2][1].ToString();
 
                lblDiet.Text = ds.Tables[0].Rows[3][1].ToString();
 
                lblFluids.Text = ds.Tables[0].Rows[4][1].ToString();
 
                lblNursingOrders.Text = ds.Tables[0].Rows[5][1].ToString();
 
                lblMedications.Text = ds.Tables[0].Rows[6][1].ToString();
 
                lblBetaBlockers.Text = ds.Tables[0].Rows[7][1].ToString();
 
                lblAnalgesics.Text = ds.Tables[0].Rows[8][1].ToString();
 
                   
 

 da=new SqlDataAdapter("sp_SelectAdminto",connStr);
 ds =new DataSet();
 da.Fill(ds);
 ddlGeneral.DataSource = ds.Tables[0];
 ddlGeneral.DataTextField ="Text";
 ddlGeneral.DataBind();
 ListItem item = new ListItem("--Select--", "0");
 ddlGeneral.Items.Insert(0, item);


but when I do postback,by performing some operations,drop downlist is empty.

I googled and found we need to make sure that EnableViewState="True" in page directory and also in each controls of page (ie) enabled View State=true. but still issue exists:

steps:

1)pageload> data exists {as it is not postback)

2)selected index> postback,but data exists and going to some say pageB and again coming to pageA

3)again selected index>postback,but data is empty in drop down.

this is the issue.

Drop downlist selected index changed event:
protected void ddlVitalSigns_SelectedIndexChanged(object sender, EventArgs e)

C#
  {
 
       if (ddlVitalSigns.SelectedIndex < 0)
 
        {
 
            lblItemcommentsmsg.Text ="";
 
        }
 
       else
 
        {
 
            lblItemcommentsmsg.Text = ddlVitalSigns.SelectedItem.Value.ToString();
 
           foreach (ListItem li in ddlVitalSigns.Items)
 
            {
 
               if (li.Selected == true)
 
                    li.Attributes.Add("style", "background:red;");
 
 }
 
           
 
DropDownList ddlClicked = sender as DropDownList;
 
           foreach (Control control in pnlMain.Controls)
 
            {
 
               
 
if (control is DropDownList)
 
                {
 
                   
 
DropDownList ddl = control as DropDownList;
 
                   
 
if (ddl.ID != ddlClicked.ID)
 
                    {
 
                        ddl.SelectedIndex = 0;
 
                    }
 
                }
 
            }
 

            Session["X"] = lblItemcommentsmsg.Text;
 
        }
 
    }



Below is .aspx page ddl control code:

ASP.NET
<td class="style25">

     <asp:Label ID="lblVitalSigns" runat="server" Text="Label"></asp:Label>

  </td>

  <td class="style26">

     <asp:DropDownList ID="ddlVitalSigns" runat="server" Height="16px"

   style="margin-left: 0px" Width="356px" AutoPostBack="True"

       onselectedindexchanged="ddlVitalSigns_SelectedIndexChanged">

      </asp:DropDownList>

  </td>


Please help me ASAP.
Posted
Updated 26-Mar-14 18:53pm
v2

 
Share this answer
 
Comments
kdampuru 27-Mar-14 13:46pm    
My problem got solved by itself.Thanks Sir for your article.
Add

ddlGeneral.DataValueField ="Your Table ID";

in your code..
 
Share this answer
 
Comments
kdampuru 27-Mar-14 13:46pm    
My problem got solved by itself.dont know what happend..its strange.
i tried your solution by giving table id as abc,but its saying invalid value field.
Anyways thanks Sir for your reply.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900