为什么我不能访问WEB页中的DropDownList控件中所选定的内容?

lgqTiger 2004-08-05 11:11:50
我尝试这样访问,但是没有返回值;

private void lbModel_SelectedIndexChanged(object sender, System.EventArgs e)
{
TextBox1.Text = lbModel.Items[lbModel.SelectedIndex].Text;
TextBox1.DataBind();
//TextBox1.Text = lbModel.SelectedValue.ToString();
}

后来我将该控件的AutoPostBack属性设置为true后有了返回值;
但是每次都只能够返回列表框中第一个位置的值。
请问,这是为什么呢?
我尝试过,该控件的Items属性在设计期间就给赋值的话,就正常,
但是我必须动态帮定该控件到我指定的字段中,数据帮定都没有问题
能正常显示但是选择后要访问我的选择的值时就不行了。
(只能够返回第一个记录的数据)

...全文
93 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
lgqTiger 2004-08-07
  • 打赏
  • 举报
回复
啊!谢谢楼上的两位师傅。
都是你们这些热心人,
让我这个刚丛Delphi转过来学习C#的初哥
找到了光明和温暖。
谢谢!
[老虎]顿首。
csq0516 2004-08-06
  • 打赏
  • 举报
回复
up
marvelstack 2004-08-06
  • 打赏
  • 举报
回复
楼主犯了重复绑定的错误,下面是我给你写的完整代码:
前台:
<%@ Page language="c#" Codebehind="WebForm3.aspx.cs" AutoEventWireup="false" Inherits="WebApplication1.WebForm3" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm3</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:DropDownList id="DropDownList1" runat="server"></asp:DropDownList>
<asp:CheckBox id="CheckBox1" runat="server" Text="启用AutoPostBack属性" AutoPostBack="True"></asp:CheckBox>
<asp:Button id="ButtonGet" runat="server" Text="获取数据"></asp:Button><FONT face="宋体"><BR>
</FONT>
<asp:Label id="Label1" runat="server">Label</asp:Label>
</form>
</body>
</HTML>

后台:
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;

namespace WebApplication1
{
/// <summary>
/// WebForm3 的摘要说明。
/// </summary>
public class WebForm3 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DropDownList DropDownList1;
protected System.Web.UI.WebControls.CheckBox CheckBox1;
protected System.Web.UI.WebControls.Button ButtonGet;
protected System.Web.UI.WebControls.Label Label1;

private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)//只在第一次时绑定数据源。
{
DropDownList1.DataSource = GetBindData();
DropDownList1.DataBind();
}
}
//创建数据源
protected ArrayList GetBindData()
{
ArrayList al = new ArrayList();
al.Add("星期一");
al.Add("星期二");
al.Add("星期三");
al.Add("星期四");
al.Add("星期五");
al.Add("星期六");
al.Add("星期日");
return al;
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.DropDownList1.SelectedIndexChanged += new System.EventHandler(this.DropDownList1_SelectedIndexChanged);
this.CheckBox1.CheckedChanged += new System.EventHandler(this.CheckBox1_CheckedChanged);
this.ButtonGet.Click += new System.EventHandler(this.ButtonGet_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
Label1.Text = String.Format("你选择了{0}",(string)DropDownList1.Items[DropDownList1.SelectedIndex].Value);
}

private void CheckBox1_CheckedChanged(object sender, System.EventArgs e)
{
DropDownList1.AutoPostBack = CheckBox1.Checked;
}

private void ButtonGet_Click(object sender, System.EventArgs e)
{
Label1.Text = String.Format("你选择了{0}",(string)DropDownList1.Items[DropDownList1.SelectedIndex].Value);
}
}
}
有什么问题再发短信给我。
saucer 2004-08-05
  • 打赏
  • 举报
回复
dropdownlist uses viewstate to remember what items it has, so you only need to bind once. If you re-bind the control upon postback in Page_Load, the selected item will be always the first item
windinwing 2004-08-05
  • 打赏
  • 举报
回复
if (!IsPostBack)
判断网页是否加载,然后在绑定,不然在点确定的时候DropDownList会重新绑定一次
lgqTiger 2004-08-05
  • 打赏
  • 举报
回复
阿阿!请问楼上的高人,
为什么一定要这样做?
小弟,不明白!
请指教。 ^_^
saucer 2004-08-05
  • 打赏
  • 举报
回复

1. then set AutoPostBack=true

2. do the data binding inside if block

if (!IsPostBack)
{
//lbModel.DataSource = ...;
//lbModel.DataTextField = ...;
//lbModel.DataBind();
}

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧