今天准备做一个无页面刷新的控件,原本以为只需要将其代码直接从页面中转过来就行了,没有想到,转过来之后居然只有DropDownList1里有值, DropDownList2空白.
百思不得其解,后来想想,是不是JavaScript里的接收值ID[DropDownList2]没有接收到值或者是DropDownList2无效呢?
运行之后查看源文件,果然,他的ID变成了"Zz1_DropDownList2"了,这可能是与注册控件有关吧.
.NET中直接拖放到页面中的控件,系统默认将其控件名称修改为:"控件名" + "1" + "DropDownList2"
改过来之后,OK.
zz.ascx:
using
System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class zz : System.Web.UI.UserControl
... {
protected void Page_Load(object sender, EventArgs e)
...{
DropDownList2.Attributes.Add("onBlur", "" + this.myHidden.ClientID + ".value=this.options[this.selectedIndex].value");
if (!this.IsPostBack)
...{
this.down1_bind();
this.DropDownList1.Attributes.Add("onchange", "XmlPost2(this);");
}
if (shopClassFatherId != "")
...{
this.down2_bind(shopClassFatherId);
}
}
GetData GD = new GetData();
DataSet ds = new DataSet();
private string shopClassFatherId
...{
get
...{
if (ViewState["shopClassFatherId"] != null && ViewState["shopClassFatherId"].ToString() != "")
...{
return ViewState["shopClassFatherId"].ToString();
}
else
...{
if (Request["shopClassFatherId"] != null && Request["shopClassFatherId"].ToString() != "")
...{
return Request["shopClassFatherId"];
}
else
...{
return "";
}
}
}
set
...{
ViewState["shopClassFatherId"] = value;
}
}
private string shop_classid
...{
get
...{
if (ViewState["shop_classid"] != null && ViewState["shop_classid"].ToString() != "")
...{
return ViewState["shop_classid"].ToString();
}
else
...{
if (Request["shop_classid"] != null && Request["shop_classid"].ToString() != "")
...{
return Request["shop_classid"];
}
else
...{
return "";
}
}
}
set
...{
ViewState["shop_classid"] = value;
}
}
private void down1_bind()
...{
string sql = "select shopClassFatherId,shopClassFatherName from shopClassFather order by shopClassFatherId";
DataSet ds = GD.DataBind(sql);
this.DropDownList1.DataSource = ds;
this.DropDownList1.DataValueField = "shopClassFatherId";
this.DropDownList1.DataTextField = "shopClassFatherName";
this.DropDownList1.DataBind();
}
private void down2_bind(string id)
...{
string mystr = "";
string sql = "select shop_classid,name from shopclass where father = '" + id + "' order by shop_classid";
DataSet ds = GD.DataBind(sql);
if (ds.Tables[0].Rows.Count != 0)
...{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
...{
mystr += "," + ds.Tables[0].Rows[i][0].ToString() + "|" + ds.Tables[0].Rows[i][1].ToString();
}
mystr = mystr.Substring(1);
}
this.Response.Write(mystr);
this.Response.End();
}
int fatherId;
int firstClassId;
string content;
protected void Button1_Click(object sender, EventArgs e)
...{
fatherId = Convert.ToInt32(DropDownList1.Text.ToString());
if (this.myHidden.Value == "" || Convert.ToInt32(this.myHidden.Value.ToString()) == 0)
...{
return;
}
else
...{
firstClassId = Convert.ToInt32(this.myHidden.Value);
}
content = TextBox1.Text.ToString();
if (fatherId == 1)
...{
Response.Redirect("SearchProduct.aspx?firstClassId=" + firstClassId + "&key=" + content);
}
if (fatherId == 2)
...{
Response.Redirect("SearchShop.aspx?firstClassId=" + firstClassId + "&key=" + content);
}
if (fatherId == 3)
...{
Response.Redirect("Searchshopkeeper.aspx?firstClassId=" + firstClassId + "&key=" + content);
}
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class zz : System.Web.UI.UserControl
... {
protected void Page_Load(object sender, EventArgs e)
...{
DropDownList2.Attributes.Add("onBlur", "" + this.myHidden.ClientID + ".value=this.options[this.selectedIndex].value");
if (!this.IsPostBack)
...{
this.down1_bind();
this.DropDownList1.Attributes.Add("onchange", "XmlPost2(this);");
}
if (shopClassFatherId != "")
...{
this.down2_bind(shopClassFatherId);
}
}
GetData GD = new GetData();
DataSet ds = new DataSet();
private string shopClassFatherId
...{
get
...{
if (ViewState["shopClassFatherId"] != null && ViewState["shopClassFatherId"].ToString() != "")
...{
return ViewState["shopClassFatherId"].ToString();
}
else
...{
if (Request["shopClassFatherId"] != null && Request["shopClassFatherId"].ToString() != "")
...{
return Request["shopClassFatherId"];
}
else
...{
return "";
}
}
}
set
...{
ViewState["shopClassFatherId"] = value;
}
}
private string shop_classid
...{
get
...{
if (ViewState["shop_classid"] != null && ViewState["shop_classid"].ToString() != "")
...{
return ViewState["shop_classid"].ToString();
}
else
...{
if (Request["shop_classid"] != null && Request["shop_classid"].ToString() != "")
...{
return Request["shop_classid"];
}
else
...{
return "";
}
}
}
set
...{
ViewState["shop_classid"] = value;
}
}
private void down1_bind()
...{
string sql = "select shopClassFatherId,shopClassFatherName from shopClassFather order by shopClassFatherId";
DataSet ds = GD.DataBind(sql);
this.DropDownList1.DataSource = ds;
this.DropDownList1.DataValueField = "shopClassFatherId";
this.DropDownList1.DataTextField = "shopClassFatherName";
this.DropDownList1.DataBind();
}
private void down2_bind(string id)
...{
string mystr = "";
string sql = "select shop_classid,name from shopclass where father = '" + id + "' order by shop_classid";
DataSet ds = GD.DataBind(sql);
if (ds.Tables[0].Rows.Count != 0)
...{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
...{
mystr += "," + ds.Tables[0].Rows[i][0].ToString() + "|" + ds.Tables[0].Rows[i][1].ToString();
}
mystr = mystr.Substring(1);
}
this.Response.Write(mystr);
this.Response.End();
}
int fatherId;
int firstClassId;
string content;
protected void Button1_Click(object sender, EventArgs e)
...{
fatherId = Convert.ToInt32(DropDownList1.Text.ToString());
if (this.myHidden.Value == "" || Convert.ToInt32(this.myHidden.Value.ToString()) == 0)
...{
return;
}
else
...{
firstClassId = Convert.ToInt32(this.myHidden.Value);
}
content = TextBox1.Text.ToString();
if (fatherId == 1)
...{
Response.Redirect("SearchProduct.aspx?firstClassId=" + firstClassId + "&key=" + content);
}
if (fatherId == 2)
...{
Response.Redirect("SearchShop.aspx?firstClassId=" + firstClassId + "&key=" + content);
}
if (fatherId == 3)
...{
Response.Redirect("Searchshopkeeper.aspx?firstClassId=" + firstClassId + "&key=" + content);
}
}
}