本人超笨,所以向各位求教了。
两个Dropdownlist:dl1和dl2,其中dl1直接在页面中,dl2在DataGrid1的footer中。
我的设想是,在aspx中不做任何绑定,当Page_Load时绑定dl1到“省份”字段;当dl1发生OnSelectedIndexChanged时在后台的dl1_SelectedIndexChanged事件中绑定dl2到“地区”字段,并实现联动。
问题出在后面的dl1_SelectedIndexChanged事件中——当dl1的值changed时,dl2没任何反应。下面是我的代码片段,请不吝赐教,争取下班之前散分完毕。注意两个dropdownlist控件的位置! string strmysql2 = "select * from area where arProv = '" + dl1.Text + "'";
SqlDataAdapter da2 = new SqlDataAdapter(strmysql2, cnn);
DataSet ds2 = new DataSet();
foreach (DataGridItem item in dgIn.Controls[0].Controls)
{
if (item.ItemType == ListItemType.Footer)
{
Dropdownlist t2 = (Dropdownlist)item.FindControl("dl2");
da2.Fill(ds2, "area");
t2.DataSource = ds2;
t2.DataTextField = "arName";
t2.DataValueField = "arName";
t2.DataBind();
DataGrid1.DataBind();
}
}
两个Dropdownlist:dl1和dl2,其中dl1直接在页面中,dl2在DataGrid1的footer中。
我的设想是,在aspx中不做任何绑定,当Page_Load时绑定dl1到“省份”字段;当dl1发生OnSelectedIndexChanged时在后台的dl1_SelectedIndexChanged事件中绑定dl2到“地区”字段,并实现联动。
问题出在后面的dl1_SelectedIndexChanged事件中——当dl1的值changed时,dl2没任何反应。下面是我的代码片段,请不吝赐教,争取下班之前散分完毕。注意两个dropdownlist控件的位置! string strmysql2 = "select * from area where arProv = '" + dl1.Text + "'";
SqlDataAdapter da2 = new SqlDataAdapter(strmysql2, cnn);
DataSet ds2 = new DataSet();
foreach (DataGridItem item in dgIn.Controls[0].Controls)
{
if (item.ItemType == ListItemType.Footer)
{
Dropdownlist t2 = (Dropdownlist)item.FindControl("dl2");
da2.Fill(ds2, "area");
t2.DataSource = ds2;
t2.DataTextField = "arName";
t2.DataValueField = "arName";
t2.DataBind();
DataGrid1.DataBind();
}
}
解决方案 »
- 急急急~~求邮件找回密码的代码~
- c#从winform转webform遇到的问题,求解
- 在回调的方法中生成的控件事件不响应
- asp.net 2.0的网站更新需要重新发布整个网站?
- 在 FreeTextBox 添加上QQ和MSN表情,就差一段JavaScript脚本了。
- request怎么获取变量参数的值?
- 怎样才能防止它回退到已登录的界面?
- 调查一下各位用asp.net工资收入情况?
- asp.net中简单的SQL语句哪里有错?
- 文忠是什么格式的字符啊?
- 我的。NET网站用动网的论坛,怎么进行用户登录的交互呀。。
- 怎样判断一个byte()是gif还是ppt还是rar还是exe文件呢?
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.Xml;namespace WebApplication2
{
/**//// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DropDownList DropDownListProvince;
protected System.Web.UI.WebControls.DropDownList DropDownListCity;
protected System.Web.UI.WebControls.Label LabelMessage; public string City = "广州";
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
string CurrentPath = this.Server.MapPath("."); if (!Page.IsPostBack)
{
if(System.IO.File.Exists(CurrentPath + "\\Province.xml"))
{
this.DropDownListProvince.Items.Clear(); System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(CurrentPath + "\\Province.xml"); XmlNodeList nodes = doc.DocumentElement.ChildNodes; XmlNode node1 = doc.DocumentElement.SelectSingleNode(@"Province/City[@Name='" + this.City + "']"); foreach(XmlNode node in nodes)
{
this.DropDownListProvince.Items.Add(node.Attributes["Name"].Value);
int n = this.DropDownListProvince.Items.Count - 1;
if(node1 != null && node == node1.ParentNode)
this.DropDownListProvince.SelectedIndex = n;
}
DropDownListProvince_SelectedIndexChanged(sender,e);
}
else
{
this.LabelMessage.Text = "地市信息文件丢失!";
}
}
} Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.DropDownListProvince.SelectedIndexChanged += new System.EventHandler(this.DropDownListProvince_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void DropDownListProvince_SelectedIndexChanged(object sender, System.EventArgs e)
{
string CurrentPath = this.Server.MapPath("."); if(System.IO.File.Exists(CurrentPath + "\\Province.xml"))
{
this.DropDownListCity.Items.Clear(); System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(CurrentPath + "\\Province.xml"); XmlNodeList nodes = doc.DocumentElement.ChildNodes[this.DropDownListProvince.SelectedIndex].ChildNodes; foreach(XmlNode node in nodes)
{
this.DropDownListCity.Items.Add(node.Attributes["Name"].Value);
int n = this.DropDownListCity.Items.Count - 1;
if(node.Attributes["Name"].Value == this.City)
{
this.DropDownListCity.SelectedIndex = n;
}
} if(this.DropDownListCity.SelectedIndex == -1)
this.DropDownListCity.SelectedIndex = 0;
}
else
{
this.LabelMessage.Text = "地市信息文件丢失!";
}
} }
}
已经设为TRUE了。Re:pontus(pontus)
触发了事件,但dl2里面是空的。Re: q_po_o(两个人)
你的代码好像很复杂耶,是不是XML?我看不懂。好像跟我的问题不大一致,有空研究研究吧,感谢!!
t2.DataSource = ds2;
改成:t2.DataSource = ds2.Tables["area"];
是不是将我上面的那段代码复制到DataGrid1_ItemDataBound事件中??
试过了,好象不行哦。Re:bitliuyang(昊天)
不行!
要这样子的话,是不是要把t2的Item集再传回dl2???怎么样传呢???
不知道这样理解是否合理???
下班了,回家继续等待高手们的帮助哦!!!
我的程序是要绑定到后台数据库的,用客户端与服务器端有什么不同?况且我不会JavaScript。Re:singlepine(小山)
中间那段ddlpovince_SelectedIndexChanged事件代码跟我的代码应该是一样的吧?不知道为什么我的不行呢?
另外我想请教:btndelete.Attributes.Add("onclick","Delete();");这条语句后而的双引号中引用的事件是不是只能为客户端事件?如果要引用服务器端事件(即后台cs或vb文件中定义的事件)要怎么做?Re:bitliuyang(昊天)
为何要去掉那句?DataGrid中的控件绑定了之后不用对DataGrid进行重新绑定吗?我去掉了之后并没有影响程序执行,只是还是不行呢。
关于btndelete.Attributes.Add("onclick","Delete();");对服务器端事件的调用的问题希望你知道的话能帮忙解答一下,谢谢哦。/
是后台调用前台方法,主要实现删除前判断和无刷新删除,直接在后台删除也是可以的,如
foreach(DataGridItem thisitem in DataGrid1.Items)
{
if(((CheckBox)thisitem.Cells[0].Controls[1]).Checked)
{
string strloginid= DataGrid1.DataKeys[thisitem.ItemIndex].ToString();
Del (strloginid); //删除函数
}
}