Hi Friends,
I am loading data into dropdownlist from sql as shown below:
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)
{
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:
<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.