回发后Dropdownlist为空 [英] Dropdownlist is Empty after Postback

查看:123
本文介绍了回发后Dropdownlist为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,



我将数据从sql加载到dropdownlist中,如下所示:





  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 ]。行[ 0 ] [ 1 ]。ToString();

lblVitalSigns.Text = ds.Tables [ 0 ]。行[ 1 ] [ 1 ]。ToString();

lblActivity.Text = ds.Tables [ 0 ]。行[ 2 ] [ 1 ]。ToString();

lblDiet.Text = ds.Tables [ 0 ]。行[ 3 ] [ 1 ]。ToString();

lblFluids.Text = ds.Tables [ 0 ]。行[ 4 ] [ 1 ]。ToString();

lblNursingOrders.Text = ds.Tables [ 0 ]。行[ 5 ] [ 1 ]。ToString();

lblMedications.Text = ds.Tables [ 0 ]。行[ 6 ] [ 1 ]。ToString();

lblBetaBlockers.Text = ds.Tables [ 0 ]。行[ 7 ] [ 1 ]。ToString();

lblAnalgesics.Text = ds.Tables [ 0 ]。行[ 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( - - 选择 - 0);
ddlGeneral.Items.Insert( 0 ,item);





但是当我做回发时,通过执行一些操作,下拉列表是空的。



我用Google搜索并发现我们需要确保EnableViewState =Truein页面目录以及页面的每个控件(即)启用View State = true。但仍存在问题:



步骤:



1)pageload>数据存在{因为它不是回发)



2)所选索引>回发,但数据存在并转到某些说pageB并再次来到pageA



3)再次选择索引>回发,但数据在下拉列表中为空。 />


这是问题。



下拉列表选择索引更改事件:

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 (控制控件 in pnlMain.Controls)

{



如果(control DropDownList)

{



DropDownList ddl = control as DropDownList;



if (ddl.ID!= ddlClicked.ID)

{

ddl.SelectedIndex = 0 ;

}

}

}


会话[ X] = lblItemcommentsmsg.Text;

}

}







< u> 下面是.aspx页面ddl控制代码:



 <   td     class   =  style25 >  

< asp:Label ID = lblVitalSigns runat = server 文本 = 标签 > < / asp:标签 >

< < span class =code-leadattribute> / td >

< ; td class < span class =code-keyword> = style26 >

< asp:DropDownList ID = ddlVitalSigns < span class =code-attribute> runat = server 高度 = 16px



style = margin-left:0px 宽度 = 356px < span class =code-attribute> AutoPostBack = True



onselectedindexchanged = ddlVitalSigns_SelectedIndexChanged >

< / asp:DropDownList >

< / td >





请尽快帮助我。

解决方案

< blockquote> Plz参考此链接。它可能对你有帮助。

http:/ /stackoverflow.com/questions/17463937/restore-a-drop-down-selected-item-after-postback [ ^ ]


Add



ddlGeneral.DataValueField =你的桌子ID;


代码中的
..


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.

解决方案

Plz refer this link. It may help you.
http://stackoverflow.com/questions/17463937/restore-a-drop-down-selected-item-after-postback[^]


Add

ddlGeneral.DataValueField ="Your Table ID";

in your code..


这篇关于回发后Dropdownlist为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆