我是新手怎么加是这样吗?
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();
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();
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();
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();}
否则每次加载都回执行的!
传过去的页面代码:
if(this.cnat_21.SelectedValue != "小二")
{
p.cnat_21 = this.cnat_21.SelectedItem.Text;
}
......
......
......
......
接收值的时候这样写对吗?我试验好了几次但是都接收到的是第一个值也就是数据库中的第一行的数据”请选择“
就是selectedindexchange事件之后吗?
那你直接在事件里写p.cnat_21 = this.cnat_21.SelectedItem.Text;不就行了吗?
何必加个if判断呢?不理解。
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个字????
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("请选择",""));
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();
但是不显示呀!请选择!!
这样比较好吧!
啊缺少命名空间???
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;
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();
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();
且这个加入的代码没有放在 not ispostback中
dropdownlist需要引用????
不需要
if(Page.IsPostback==false)
{
你的绑定代码
}
<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>");
}
http://topic.csdn.net/u/20070606/22/419b7894-731d-42e2-b98d-5c8a1b6608de.html?seed=822352797
也没解决
1.!Ispostback
2.AutoPostBack="true"
还有这句,this.cnat_21.DataValueField= "jfxh ";你绑定的ID编号,可是你却这样写,这2个一辈子都不会相等if(this.cnat_21.SelectedValue != "小二 ")
{
p.cnat_21 = this.cnat_21.SelectedItem.Text;
}
if(!Ispostback)
{
...
}然后那你的下拉框的AutoPostBack设为true
然后在绑定后加this.cnat_21.Items.Insert(0,new ListItem( "--请选择-- ", " "));