我是新手怎么加是这样吗?
SqlCommand cmc=new SqlCommand("select * from cnatjf",con);
SqlDataReader sdf=cmc.ExecuteReader();
this.cnat_21.DataSource=sdf;
this.cnat_21.DataTextField="mz";
this.cnat_21.DataValueField="jfxh";
this.cnat_21.Items.Insert(0,new ListItem("请选择",""));
this.cnat_21.DataBind();
sdf.Close();

解决方案 »

  1.   

    dropdownlist.Items.Insert(0,"请选择");
      

  2.   

    这样不用在数据库中加就又了吗?
    SqlCommand cmc=new SqlCommand("select * from cnatjf",con);
    SqlDataReader sdf=cmc.ExecuteReader();
    this.cnat_21.DataSource=sdf;
    this.cnat_21.DataTextField="mz";
    this.cnat_21.DataValueField="jfxh";
    dropdownlist.Items.Insert(0,"请选择");
    this.cnat_21.DataBind();
    sdf.Close();
      

  3.   

    if(!IsPostBack){
       SqlCommand cmc=new SqlCommand("select * from cnatjf",con);
       SqlDataReader sdf=cmc.ExecuteReader();
       this.cnat_21.DataSource=sdf;
       this.cnat_21.DataTextField="mz";
       this.cnat_21.DataValueField="jfxh";
       this.cnat_21.DataBind();
       sdf.Close();}
      

  4.   

    Page_Load中有代码吗?如果有帮定代码等,请写在if(!IsPostBack)中
    否则每次加载都回执行的!
      

  5.   

    绑定现在没上面问题!
    传过去的页面代码:
    if(this.cnat_21.SelectedValue != "小二")
    {
    p.cnat_21 = this.cnat_21.SelectedItem.Text;
    }
    ......
    ......
    ......
    ......
    接收值的时候这样写对吗?我试验好了几次但是都接收到的是第一个值也就是数据库中的第一行的数据”请选择“
      

  6.   

    不知道你传过去是什么意思?
    就是selectedindexchange事件之后吗?
    那你直接在事件里写p.cnat_21 = this.cnat_21.SelectedItem.Text;不就行了吗?
    何必加个if判断呢?不理解。
      

  7.   

    dropdownlist本身不支持更新操作,简单的方法是在postback后得到传递的值直接写入数据库,你总是得到"请选择"应该是在页面load之后的值,你应该取得request["cnat_21"]的值
      

  8.   

    请选择哪三个自不存入数据库的话在dropdownlist中怎么才能加进去?
      

  9.   

    SqlCommand cmc=new SqlCommand("select * from cnatjf",con);
       SqlDataReader sdf=cmc.ExecuteReader();
       this.cnat_21.DataSource=sdf;
       this.cnat_21.DataTextField="mz";
       this.cnat_21.DataValueField="jfxh";
       this.cnat_21.DataBind();
       sdf.Close();
    在绑定的时候怎么让他自动加入请选择3个字????
      

  10.   

    this.cnat_21.Items.Insert(0,new ListItem("请选择",""));这样是错误的吧
      

  11.   

    SqlCommand cmc=new SqlCommand("select * from cnatjf",con);
       SqlDataReader sdf=cmc.ExecuteReader();
       this.cnat_21.DataSource=sdf;
       this.cnat_21.DataTextField="mz";
       this.cnat_21.DataValueField="jfxh";
       this.cnat_21.DataBind();
       sdf.Close();
    this.cnat_21.Items.Insert(0,new ListItem("请选择",""));
      

  12.   

    这样我试验了
    SqlCommand cmc=new SqlCommand("select * from cnatjf",con);
       SqlDataReader sdf=cmc.ExecuteReader();
       this.cnat_21.DataSource=sdf;
       this.cnat_21.DataTextField="mz";
       this.cnat_21.DataValueField="jfxh";
    this.cnat_21.Items.Insert(0,new ListItem("请选择",""));   我加在这行了!
       this.cnat_21.DataBind();
       sdf.Close();
    但是不显示呀!请选择!!
      

  13.   

    dropdownlist.Items.Insert(0,"请选择");
    这样比较好吧!
      

  14.   

    都一样。你放在databind之后试试。
      

  15.   

    C:\Inetpub\wwwroot\cnat_work\index.aspx.cs(123): 找不到类型或命名空间名称“DropdownList”(是否缺少 using 指令或程序集引用?)
    啊缺少命名空间???
      

  16.   

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;
      

  17.   

    if(!IsPostBack){
       SqlCommand cmc=new SqlCommand("select * from cnatjf",con);
       SqlDataReader sdf=cmc.ExecuteReader();
       this.cnat_21.DataSource=sdf;
       this.cnat_21.DataTextField="mz";
       this.cnat_21.DataValueField="jfxh";
       this.cnat_21.Item.Insert(0,new ListItem("--请选择--",""));
       this.cnat_21.DataBind();
       sdf.Close();
      

  18.   

    这个不是把“请选择”放在哪的问题,是不是读出选择值之后dropdownlist又加载了一遍数据
      

  19.   

    加不上呀!!!!
    SqlCommand cmc=new SqlCommand("select * from cnatjf",con);
    SqlDataReader sdf=cmc.ExecuteReader();
    this.cnat_21.DataSource=sdf;
    this.cnat_21.DataTextField="mz";
    this.cnat_21.DataValueField="jfxh";this.cnat_21.Items.Insert(0,new ListItem("--请选择--",""));
    this.cnat_21.DataBind();
    sdf.Close();
      

  20.   

    估计一下,可能是你的请选择不是静态声明的.而是程序加入的.
    且这个加入的代码没有放在 not ispostback中
      

  21.   

    DropDownList  的回传   属性  选择 true
      

  22.   

    LASPQQ() ( ) 信誉:100    Blog  2007-1-18 13:59:55  得分: 0  
     
     
       
    dropdownlist需要引用????
    不需要  
     
      

  23.   

    估计你忘了。。
    if(Page.IsPostback==false)
    {
    你的绑定代码
    }
      

  24.   

    我也是这样啊,每次获取的选择项数据都是第一个(默认值),并且一点击按钮,数据就丢失
     <asp:dropdownlist  EnableViewState=“true” ID="stationData1"   runat="server"></asp:dropdownlist>
    <asp:button ID="btnAddNewStation" OnClick="addNewstation_Click" runat="server" text="Button" />
    后台:protected void Page_Load(object sender, EventArgs e)
     {  
          if(!Ispostback)
         {
                int i=0;
                int j = 0;
                ArrayList time = new ArrayList();
                for (i = 0; i < 24; i++)
                    for (j = 0; j < 2; j++)
                        if (j == 0)
                            time.Add(i + ":00:00");
                        else
                            time.Add(i + ":30:00");
     stationDataTime1.DataSource = time;
                stationDataTime1.DataBind();
     }
    }protected void addNewstation_Click(object sender, EventArgs e)
        {
     Response.Write(@"<script language='javascript'>alert('station=" + stationData1.SelectedValue + " date=" + stationDate.Text + " time=" + stationDataTime1.SelectedValue + "');</script>");
          
        }
      

  25.   

    上面有两个引号、和括弧写错了。这里也有一个相同的帖子
    http://topic.csdn.net/u/20070606/22/419b7894-731d-42e2-b98d-5c8a1b6608de.html?seed=822352797
    也没解决
      

  26.   

    错在这一句:this.cnat_21.SelectedItem.Text
      

  27.   

    兄弟,我的问题解决了
    1.!Ispostback
    2.AutoPostBack="true" 
      

  28.   


    还有这句,this.cnat_21.DataValueField= "jfxh ";你绑定的ID编号,可是你却这样写,这2个一辈子都不会相等if(this.cnat_21.SelectedValue   !=   "小二 ") 

    p.cnat_21   =   this.cnat_21.SelectedItem.Text; 
      

  29.   

    把那写代码放在.!Ispostback里面,然后把
    if(!Ispostback)
    {
        ...
    }然后那你的下拉框的AutoPostBack设为true
    然后在绑定后加this.cnat_21.Items.Insert(0,new   ListItem( "--请选择-- ", " "));