DropDownList 控件的使⽤
关于下拉菜单(dropdownlist)⼀些⽤法: DropDownListWeb 控件创建⼀个包含多个选项的下拉列表,⽤户可以从中选择⼀个选项。 1,动态绑定数据(与datatable 绑定):
若要指定绑定后的数据选定项可使⽤DropDownList.Items.FindByText("name").Selected=true 或者DropDownList.Items.FindByValue("code").Selected=true
另外也可从数据库中获得选定项(⽐如修改信息的时候)
可使⽤先⽤数据库填充⼀个DateSet--ds,然后使⽤DropDownList.Items.FindByValue/*或者FindByText*/(ds.Tables[0].Rows[0].ItemArray[0].ToString()).Selected=true; 实际应⽤:
1,从查询页⾯中点击编辑跳转到编辑页⾯2,编辑完成,提⽰修改成功后,跳转到查询页⾯的跳转的状态 查询页⾯:
1
#region 绑定设置dropdownlist 的值2
/// <summary>3
/// 绑定设置dropdownlist 的值4
/// </summary>5
/// <param name="ddl"></param>6
控件的使用/// <param name="dt"></param>7
private void SetDropDownList(DropDownList ddl, DataTable dt)8
{9
//清空原有数据10
ddl.Items.Clear();11
//判断是否有需要填充的数据,有则填充12
if (dt.Rows.Count > 0)13
{14
ddl.DataSource = dt;//绑定数据源15
ddl.DataValueField = dt.Columns[0].ToString();//绑定数据项Value 16
ddl.DataTextField = dt.Columns[2].ToString();//绑定数据项Text 17
ddl.DataBind();//绑定18
}19
20
ddl.Items.Add(new ListItem("请选择", ""));//填充请【选择项】21
ddl.SelectedIndex = ddl.Items.Count - 1;//选中【请选择】项22
}23 24 #endregion
1 /// <summary>
2 /// 点击编辑,获取编辑的id, 即须知id
3 /// </summary>
4 /// <param name="sender"></param>
5 /// <param name="e"></param>
6 protected void lbtnEdit_Click(object sender, EventArgs e)
7 {
8 LinkButton editbtn = (LinkButton)sender;
9 string ExamID = DropExam.SelectedItem.Value;
10 //获取被编辑字典的ID
11 string NoticeID = editbtn.CommandArgument.ToString();
12 Response.Redirect("ExamNotice.aspx?NoticeID=" + NoticeID + "&ExamID=" + ExamID);
13 //"ModifyExamStudentInformation.aspx?StudentNo=" + parmStr[0] + "&ExamMainID=" + parmStr[1]
14 }
修改页⾯:
1 /// <summary>
2 /// 点击修改
3 /// </summary>
4 /// <param name="sender"></param>
5 /// <param name="e"></param>
6 protected void btnOK_Click(object sender, EventArgs e)
7 {
8 try
9 {
10
11 TeaExamInformationBLL teaExamInformationBLL = new TeaExamInformationBLL();
12 //定义⼀个考试申请实体类
13 string NoticeID = Request.QueryString["NoticeID"];
14 string ExamID = Request.QueryString["ExamID"];
15 ExamNoticeEntity enExamNotice = new ExamNoticeEntity();
16 enExamNotice.NoticeID = NoticeID;
17 enExamNotice.Notice = txtContent.Text;
18 enExamNotice.IsNoUse = IsUse.SelectedItem.Text;
19 enExamNotice.NoticeName = txtNoticeName.Text;
20 //enExamNotice.AddUser = "何静媛";
21 enExamNotice.AddUser = Session["UserName"].ToString();
22 //enExamNotice.IsNoDefault = IsDefault.SelectedItem.Text;
23 //声明错误枚举
24 ConfirmInfo flagUpdateExamNotice;
25 //判断如果想要设置默认须知
26 flagUpdateExamNotice = teaExamInformationBLL.UpdateExamInformation(enExamNotice);
27 if (ConfirmInfo.Success == flagUpdateExamNotice)
28 {
29 Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('修改考试须知信息成功!');</scr
30 //修改成功
31 //Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "location.href='SearchInformation.aspx?ExamID='" + ExamID, true);
32 Response.Redirect("SearchInformation.aspx");
33
34
35 }
36 //代码已经存在
37 else if (ConfirmInfo.Exists == flagUpdateExamNotice)
38 {
39 Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('修改为默认须知失败!');</script
40 }
41 else
42 {
43 Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('修改考试须知信息失败!');</scr
44 }
45 }
46 catch (Exception ex)
47 {
48 MessageBox.Show(this.Page, ex.Message);
49 }
50
51 }
点击修改,修改成功后,跳转回查询页⾯:
1 protected void Page_Load(object sender, EventArgs e)
2 {
3 if (Session["UserNO"] != null)
4 {
5
6 if (!IsPostBack)
7 {
8 //页⾯⾸次加载时给表格绑定数据
9
10 BindAllCollege();
11 //判断学院的下拉框是否为空,即判断session中的学院是否为空
12 if (Session["SelectedCollegeID"] == null)
13 {
14 BindAllCollege();
15
16 }
17 else
18 {
19 //如果学院的session中不为空,则将下拉框加载上数据,并附上选中项
20 BindAllCollege();
21 //先清空选中项
22 DropDep.ClearSelection();
23 //给下拉框赋上选中项
24 DropDep.Items.FindByValue(Session["SelectedCollegeID"].ToString()).Selected = true;
25 //DropExamRoom.Items.FindByText(ExamRoom).Selected = true;
26
27 BindCourseByID();
28 DropCourse.ClearSelection();
29 DropCourse.Items.FindByValue(Session["SelectedCourseID"].ToString()).Selected = true;
30
31 BindExamByCourseID();
32 DropExam.ClearSelection();
33 DropExam.Items.FindByValue(Session["SelectedExamID"].ToString()).Selected = true;
34
35 //定义B层类
36 TeaExamInformationBLL teaExamInformationBLL = new TeaExamInformationBLL();
37 //定义⼀个考试须知实体类
38 TR_ExamNoticeEntity enExamNoticeRelative = new TR_ExamNoticeEntity();
39 enExamNoticeRelative.ExamID = DropExam.SelectedItem.Value;
40 enExamNoticeRelative.AddUser = Session["UserName"].ToString();
41 //enExamNoticeRelative.AddUser = "何静媛";
42
43 DataTable dtExamNotice = teaExamInformationBLL.QueryExamInformation(enExamNoticeRelative);
44 if (dtExamNotice.Rows.Count == 0)
45 {
46 //MessageBox.Show(this, "此考试下还未添加考试须知!系统默认应⽤默认须知");
47 lblMessage.Text = "此考试下还未添加考试须知!系统默认应⽤默认须知";
48 }
49 repExamNotic.DataSource = dtExamNotice;
50 repExamNotic.DataBind();
51
52 }
53 }
54 }
55 else
56 {
57 Response.Redirect("../../Student/StuLogin.aspx");
58 }
59
60 }
2.查询⽤法
对于单个下拉菜单查询我⼀般直接使⽤SelectedIndexChanged 事件,使⽤这个事件千万别忘记把下拉菜单的AutoPostBack 属性设为true!
3,添加⽤法 添加DorpDownList 控件的Item ⼦项的三种⽅法 DropDownList 控件的SelectedItem 对象表⽰在执⾏过程中被选中的Item 项Item 项拥有三个属性。其⼀为Text,其⼆为Value 它们分别表⽰Item 项所显⽰的⽂字与所表⽰的值,如果Value 属性没有设置,则默认Value 属性值等Text 属性,第三个属性Selected 则⽤于判断某Item 项是否被选中添加DorpDownList 控件的Item ⼦项。
1
/// <summary>2
/// 学院变化时加载课程3
/// </summary>4
/// <param name="sender"></param>5
/// <param name="e"></param>6
protected void dropCollege_SelectedIndexChanged(object sender, EventArgs e)7
{8
/
/绑定课程9
BindCourseByID();10
}11
12
#region 绑定学院13
14
/// <summary>15
/// 绑定学院16
/// </summary>17
private void BindAllCollege()18
{19
DataTable dtCollege = new DataTable();20
BasicSystemBLL BasicSystemBll = new BasicSystemBLL();21
dtCollege = BasicSystemBll.GetAllCollege();22
23
//判断是否有学院24
if (dtCollege.Rows.Count == 0)25
{26
MessageBox.Show(this, "没有学院,请联系管理员!");27
DropDep.Items.Clear();28
//dropDepartment.Items.Clear();29
DropCourse.Items.Clear();30
DropExam.Items.Clear();31
//DropExamRoom.Items.Clear();32
}33
else 34
{35
SetDropDownList(DropDep, dtCollege);36
DropExam.Items.Clear();37
//DropExamRoom.Items.Clear();38
}39
}40 41 #endregion
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论