一个页面上定义了一个字符串 A 拖放一个DropDownList 到页面上,然后将 datasource 属性定义为 A 字符串; 再拖放一个Lable 用来显示选定的文本,和一个按钮实现操作; 按钮的 button_click 事件 private void Button_Click(object sender, System.EventArgs e)
{ Label1.Text="你选择的是: " + DropDownList1.SelectedItem.Text; } 按照书上的意思,这时候 lable 反映的文本应该是我的 DropDownList 的当前选项,可是不管我选什么 lable 上的文本都是 DropDownList 的第一项(索引最小项).
下面是整段的代码,麻烦看一下:
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;
namespace MIS_SY
{
/// <summary>
/// zengjia 的摘要说明。
/// </summary>
public class zengjia : System.Web.UI.Page
{
SqlConnection ConnectZJ; //声明一个类级别的连接对象
SqlDataAdapter AdpZJ; //声明一个类级别的适配器对象
protected DataSet1 DsZJ=new DataSet1(); ////声明一个类级别的数据集实例 public string[] sex={"男","女"}; //定义页面上几个DropDownList控件绑定的字符串 public string[] xueli={"小学","初中","高中","中专","大专","本科","研究生","博士生"};
public string[] leixin={"正式","内部"};
public string[] bumen={"董事会","集团办公室","财务中心","器材公司","公共安全公司","安防公司","消防事务所","贸易部","资产管理中心","物流中心","售后服务部"}; protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.DropDownList DropDownList1;
protected System.Web.UI.WebControls.Label Label4;
protected System.Web.UI.WebControls.TextBox TextBox3;
protected System.Web.UI.WebControls.Label Label5;
protected System.Web.UI.WebControls.TextBox TextBox4;
protected System.Web.UI.WebControls.Label Label6;
protected System.Web.UI.WebControls.DropDownList DropDownList2;
protected System.Web.UI.WebControls.Label Label7;
protected System.Web.UI.WebControls.TextBox TextBox5;
protected System.Web.UI.WebControls.Label Label8;
protected System.Web.UI.WebControls.TextBox TextBox6;
protected System.Web.UI.WebControls.Label Label9;
protected System.Web.UI.WebControls.TextBox TextBox7;
protected System.Web.UI.WebControls.Label Label10;
protected System.Web.UI.WebControls.TextBox TextBox8;
protected System.Web.UI.WebControls.Label Label11;
protected System.Web.UI.WebControls.DropDownList DropDownList3;
protected System.Web.UI.WebControls.Label Label12;
protected System.Web.UI.WebControls.TextBox TextBox9;
protected System.Web.UI.WebControls.Label Label13;
protected System.Web.UI.WebControls.TextBox TextBox10;
protected System.Web.UI.WebControls.Label Label14;
protected System.Web.UI.WebControls.Label Label15;
protected System.Web.UI.WebControls.TextBox TextBox11;
protected System.Web.UI.WebControls.Label Label16;
protected System.Web.UI.WebControls.TextBox TextBox12;
protected System.Web.UI.WebControls.Label Label17;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Label Label18;
protected System.Web.UI.WebControls.Button Button3;
protected System.Web.UI.WebControls.DropDownList DropDownList4; int GetMaxID() //自编的得到"最大序号"的"GetMaxID"函数
{
SqlCommand cmdNewID=new SqlCommand("SELECT+MAX(序号)"+"FROM 圣鹰员工花名册",ConnectZJ);
ConnectZJ.Open();
int intID;
if(cmdNewID.ExecuteScalar()!=DBNull.Value)
intID=(int)cmdNewID.ExecuteScalar();
else
intID=0;
ConnectZJ.Close();
return intID+1;
} //自定义函数结束 private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
ConnectZJ=(SqlConnection)Session["Connect1"]; //从强制转换的状态变量获得连接
AdpZJ=(SqlDataAdapter)Session["Adp1"]; //从强制转换的状态变量获得适配器
DsZJ=(DataSet1)Session["Ds1"]; //从强制转换的状态变量获得数据集 DropDownList1.DataBind(); //自定义字符串的数据绑定
DropDownList2.DataBind(); //自定义字符串的数据绑定
DropDownList3.DataBind(); //自定义字符串的数据绑定
DropDownList4.DataBind(); //自定义字符串的数据绑定 int MaxID=GetMaxID(); //通过自定义的方法得到并在控件上显示“当前的序号"
TextBox1.Text=MaxID.ToString();
} #region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
} /// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Button2.Click += new System.EventHandler(this.Button2_Click);
this.Button3.Click += new System.EventHandler(this.Button3_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void TextBox1_TextChanged(object sender, System.EventArgs e)
{ } private void Button2_Click(object sender, System.EventArgs e) //“重新填写"按钮
{
Server.Transfer("zengjia.aspx");
} private void Button1_Click(object sender, System.EventArgs e)
{ DataSet1.圣鹰员工花名册Row rowNew;
rowNew=DsZJ.圣鹰员工花名册.New圣鹰员工花名册Row();
rowNew.序号=GetMaxID();
rowNew.姓名=TextBox2.Text;
rowNew.性别=DropDownList1.SelectedItem.Text; //注意文本框输入---〉DateTime的转换
rowNew.生日=DateTime.Parse(TextBox3.Text); rowNew.证件号码=TextBox4.Text;
rowNew.学历=DropDownList2.SelectedItem.Text;
rowNew.专业=TextBox5.Text;
rowNew.职称=TextBox6.Text; //注意文本框输入---〉DateTime的转换
rowNew.到岗日期=DateTime.Parse(TextBox7.Text); rowNew.岗位=TextBox8.Text;
rowNew.合同类型=DropDownList3.SelectedItem.Text;
rowNew.特殊状况=TextBox9.Text; rowNew.合同期限=int.Parse(TextBox10.Text); //注意文本框输入---> int 的 转换 rowNew.档案编号=TextBox11.Text; rowNew.预收违约金=int.Parse(TextBox12.Text); //注意文本框输入---> int 的 转换
rowNew.所在部门=DropDownList4.SelectedItem.Text; DsZJ.圣鹰员工花名册.Add圣鹰员工花名册Row(rowNew); //数据集中增加新行
AdpZJ.Update(DsZJ);
Server.Transfer("zengjia.aspx"); } 后来看MSDN知道,如果对DropDownList控件绑定了字符串来显示下拉条目不能将选定的条目读出,那么,DropDownList的条目只有手动在控件里面一个个加? 如果我用字符串绑定的方法,怎么实现读出选定条目的 Text 属性???谢谢
{ Label1.Text="你选择的是: " + DropDownList1.SelectedItem.Text; } 按照书上的意思,这时候 lable 反映的文本应该是我的 DropDownList 的当前选项,可是不管我选什么 lable 上的文本都是 DropDownList 的第一项(索引最小项).
下面是整段的代码,麻烦看一下:
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;
namespace MIS_SY
{
/// <summary>
/// zengjia 的摘要说明。
/// </summary>
public class zengjia : System.Web.UI.Page
{
SqlConnection ConnectZJ; //声明一个类级别的连接对象
SqlDataAdapter AdpZJ; //声明一个类级别的适配器对象
protected DataSet1 DsZJ=new DataSet1(); ////声明一个类级别的数据集实例 public string[] sex={"男","女"}; //定义页面上几个DropDownList控件绑定的字符串 public string[] xueli={"小学","初中","高中","中专","大专","本科","研究生","博士生"};
public string[] leixin={"正式","内部"};
public string[] bumen={"董事会","集团办公室","财务中心","器材公司","公共安全公司","安防公司","消防事务所","贸易部","资产管理中心","物流中心","售后服务部"}; protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.DropDownList DropDownList1;
protected System.Web.UI.WebControls.Label Label4;
protected System.Web.UI.WebControls.TextBox TextBox3;
protected System.Web.UI.WebControls.Label Label5;
protected System.Web.UI.WebControls.TextBox TextBox4;
protected System.Web.UI.WebControls.Label Label6;
protected System.Web.UI.WebControls.DropDownList DropDownList2;
protected System.Web.UI.WebControls.Label Label7;
protected System.Web.UI.WebControls.TextBox TextBox5;
protected System.Web.UI.WebControls.Label Label8;
protected System.Web.UI.WebControls.TextBox TextBox6;
protected System.Web.UI.WebControls.Label Label9;
protected System.Web.UI.WebControls.TextBox TextBox7;
protected System.Web.UI.WebControls.Label Label10;
protected System.Web.UI.WebControls.TextBox TextBox8;
protected System.Web.UI.WebControls.Label Label11;
protected System.Web.UI.WebControls.DropDownList DropDownList3;
protected System.Web.UI.WebControls.Label Label12;
protected System.Web.UI.WebControls.TextBox TextBox9;
protected System.Web.UI.WebControls.Label Label13;
protected System.Web.UI.WebControls.TextBox TextBox10;
protected System.Web.UI.WebControls.Label Label14;
protected System.Web.UI.WebControls.Label Label15;
protected System.Web.UI.WebControls.TextBox TextBox11;
protected System.Web.UI.WebControls.Label Label16;
protected System.Web.UI.WebControls.TextBox TextBox12;
protected System.Web.UI.WebControls.Label Label17;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Label Label18;
protected System.Web.UI.WebControls.Button Button3;
protected System.Web.UI.WebControls.DropDownList DropDownList4; int GetMaxID() //自编的得到"最大序号"的"GetMaxID"函数
{
SqlCommand cmdNewID=new SqlCommand("SELECT+MAX(序号)"+"FROM 圣鹰员工花名册",ConnectZJ);
ConnectZJ.Open();
int intID;
if(cmdNewID.ExecuteScalar()!=DBNull.Value)
intID=(int)cmdNewID.ExecuteScalar();
else
intID=0;
ConnectZJ.Close();
return intID+1;
} //自定义函数结束 private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
ConnectZJ=(SqlConnection)Session["Connect1"]; //从强制转换的状态变量获得连接
AdpZJ=(SqlDataAdapter)Session["Adp1"]; //从强制转换的状态变量获得适配器
DsZJ=(DataSet1)Session["Ds1"]; //从强制转换的状态变量获得数据集 DropDownList1.DataBind(); //自定义字符串的数据绑定
DropDownList2.DataBind(); //自定义字符串的数据绑定
DropDownList3.DataBind(); //自定义字符串的数据绑定
DropDownList4.DataBind(); //自定义字符串的数据绑定 int MaxID=GetMaxID(); //通过自定义的方法得到并在控件上显示“当前的序号"
TextBox1.Text=MaxID.ToString();
} #region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
} /// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Button2.Click += new System.EventHandler(this.Button2_Click);
this.Button3.Click += new System.EventHandler(this.Button3_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void TextBox1_TextChanged(object sender, System.EventArgs e)
{ } private void Button2_Click(object sender, System.EventArgs e) //“重新填写"按钮
{
Server.Transfer("zengjia.aspx");
} private void Button1_Click(object sender, System.EventArgs e)
{ DataSet1.圣鹰员工花名册Row rowNew;
rowNew=DsZJ.圣鹰员工花名册.New圣鹰员工花名册Row();
rowNew.序号=GetMaxID();
rowNew.姓名=TextBox2.Text;
rowNew.性别=DropDownList1.SelectedItem.Text; //注意文本框输入---〉DateTime的转换
rowNew.生日=DateTime.Parse(TextBox3.Text); rowNew.证件号码=TextBox4.Text;
rowNew.学历=DropDownList2.SelectedItem.Text;
rowNew.专业=TextBox5.Text;
rowNew.职称=TextBox6.Text; //注意文本框输入---〉DateTime的转换
rowNew.到岗日期=DateTime.Parse(TextBox7.Text); rowNew.岗位=TextBox8.Text;
rowNew.合同类型=DropDownList3.SelectedItem.Text;
rowNew.特殊状况=TextBox9.Text; rowNew.合同期限=int.Parse(TextBox10.Text); //注意文本框输入---> int 的 转换 rowNew.档案编号=TextBox11.Text; rowNew.预收违约金=int.Parse(TextBox12.Text); //注意文本框输入---> int 的 转换
rowNew.所在部门=DropDownList4.SelectedItem.Text; DsZJ.圣鹰员工花名册.Add圣鹰员工花名册Row(rowNew); //数据集中增加新行
AdpZJ.Update(DsZJ);
Server.Transfer("zengjia.aspx"); } 后来看MSDN知道,如果对DropDownList控件绑定了字符串来显示下拉条目不能将选定的条目读出,那么,DropDownList的条目只有手动在控件里面一个个加? 如果我用字符串绑定的方法,怎么实现读出选定条目的 Text 属性???谢谢
解决方案 »
- [求助]请求HTTPS页面时加载证书问题Request.ClientCertificates.Add(X509Certificate2)
- 求修改JS:gridview中某行中第二列checkbox控制第三列checkboxlist是否可用
- 一个数据库表有100多个字段,怎么快速的插入数据库?
- 请教,关于FormsAuthentication
- ASP.NET的数据库连接问题
- 此网站需要运行以下加载项:"Beijing Baofeng Inc."中的"暴风影音播放组件"。
- 怎么解决登陆一个网站后保存他的信息.而在以后不用登陆了??
- 点击GridView1中的删除,怎么才能删除物理文件???
- 代码转换问题
- 这个判断错哪里了?
- 哪能提供免费的发手机短信的webserviecs或者有其它办法也行(最好是不太花钱的)?
- 为什么insert into 会同时插入两条一样的记录?
//TaskName 显示项
//Status 值
public class Task
{
private String _TaskName, _Status; public Task(String TaskName, String Status)
{
_TaskName = TaskName;
_Status = Status;
} public String TaskName
{
get { return _TaskName; }
} public String Status
{
get { return _Status; }
}
}
// dropdownlist绑定数据
// 传入参数 fldnm 基础数据分类名
// 传入参数 drpd 下拉列表控件
public static void f_bnd(string fldnm, DropDownList drpd)
{
ArrayList arr = new ArrayList();
drpd.Items.Clear();
drpd.DataValueField = "Status";
drpd.DataTextField = "TaskName";
string sSql = "SELECT * from xxx";
DataSet ds = db.fillDataSet(sSql);
foreach (DataRow myRow in ds.Tables[0].Rows)
{
arr.Add(new Task(myRow[0].ToString(), myRow[0].ToString()));
}
drpd.DataSource = arr;
drpd.DataBind();
}希望对你有点帮助
建议在Page_Load中加:
if(!this.IsPostBack)
{
//你的与绑定有关有代码
}
这样绑定只在第一次调用
试试如下方法!
加一个<ASP:LABEL id="lblHid" visible="False"></ASP:LABEL>private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
lblHid.Text= DropDownList1.SelectedIndex.ToString();
}private void Button_Click(object sender, System.EventArgs e)
{
int intTmp=int.Parse(lblHid.Text);
TextBox1.Text="你选择的是: " +DropDownList1.Items[intTmp].Text;
}