自定义控件如果包含
this.Page.IsClientScriptBlockRegistered
等
就不能被其他控件所嵌套
请问这个怎么处理啊。
----------------------------------------------------------------
例如:
using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Collections;
using System.Collections.Specialized;
using System.Xml;
namespace WebCtr
{
/// <summary>
/// PandC 的摘要说明。
/// 省市联动选择控件
/// </summary>
[DefaultProperty("Text"),ToolboxData("<{0}:PandC runat=server></{0}:PandC>")]
public class PandC : System.Web.UI.WebControls.WebControl, IPostBackDataHandler
{
#region 属性
[Bindable(true),
Category("Appearance"),
DefaultValue("")]
public string Text
{
get
{
if (ViewState["Text"] != null)
{
return ViewState["Text"].ToString();
}
else
{
return null;
}
}
set
{
ViewState["Text"] = value;
}
}
#endregion
//省份
private ArrayList parr = new ArrayList();
//城市
private ArrayList carr = new ArrayList();
/// <summary>
/// 将此控件呈现给指定的输出参数
/// </summary>
/// <param name="writer"> 要写出到的 HTML 编写器</param>
protected override void Render(HtmlTextWriter output)
{
GetPandC();
#region 省市连选所需的javascript函数
if(!this.Page.IsClientScriptBlockRegistered("PandC_clientScript"))
{
output.WriteLine("<script language=\"JavaScript\">");
output.WriteLine("var groups="+(parr.Count+1)+";");
output.WriteLine("var group=new Array(groups);");
output.WriteLine("for (i=0; i<groups; i++)");
output.WriteLine("group[i]=new Array();");
//选项0对应二级下拉框
output.WriteLine("group[0][0]=new Option(\"请选择\",\"\");");
for(int i=0;i<carr.Count;i++)
{
ArrayList arr = (ArrayList)carr[i];
output.WriteLine("group["+(i+1)+"][0]=new Option(\"请选择\",\""+parr[i].ToString()+"\");");
for(int j=0;j<arr.Count;j++)
{
output.WriteLine("group["+(i+1)+"]["+(j+1)+"]=new Option(\""+arr[j].ToString()+"\",\""+parr[i].ToString()+arr[j].ToString()+"\");");
}
}
output.WriteLine("function redirectff(x,obj){");
output.WriteLine("for (m=obj.options.length-1;m>0;m--)");
output.WriteLine("obj.options[m]=null;");
output.WriteLine("for (i=0;i<group[x].length;i++){");
output.WriteLine("obj.options[i]=new Option(group[x][i].text,group[x][i].value);");
output.WriteLine("obj.options[0].selected=true;");
output.WriteLine("}}");
output.WriteLine("</script>");
this.Page.RegisterClientScriptBlock("PandC_clientScript", "");
}
#endregion
#region 写select部分
//初始化
output.Write("<select id=\"" + this.ID + "_p\" name=\""+this.ID+"_p\" onChange=\"redirectff(this.options.selectedIndex,document.all."+this.ID+")\" ");
//select的其他属性(如何将WebControl的属性很方便的全部应用到控件中呢)
if (this.CssClass != "")
{
output.Write(" class=\""+this.CssClass+"\"");
}
output.Write(">");
//option
output.Write("<option>请选择</option>");
//找出初始选定的省
int pindex = 0;
for (int i=0;i<parr.Count;i++)
{
if(this.Text != null)
{
//根据选定值给出默认项
if (this.Text.IndexOf(parr[i].ToString())>-1)
{
output.Write("<option selected>"+parr[i].ToString()+"</option>");
pindex = i+1;
}
else
{
output.Write("<option>"+parr[i].ToString()+"</option>");
}
}
else
{
output.Write("<option>"+parr[i].ToString()+"</option>");
}
}
output.Write("</select>");
output.Write("省");
output.Write("<select id=\""+this.ID+"\" name=\""+this.ID+"\"");
if (this.CssClass != "")
{
output.Write(" class=\""+this.CssClass+"\"");
}
output.Write(">");
output.Write("<option value=\"\">请选择</option>");
//根据初始化选定的省来选定市
if (pindex > 0)
{
ArrayList arr = (ArrayList)carr[pindex];
for(int i=0;i<arr.Count;i++)
{
}
}
output.Write("</select>");
output.Write("市");
#endregion
}
/// <summary>
/// 从XML文件中读取省份城市信息
/// </summary>
/// <returns></returns>
private void GetPandC()
{
ArrayList arr = new ArrayList();
//从资源中获取
System.Reflection.Assembly thisExe;
thisExe = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.Stream file = thisExe.GetManifestResourceStream("WebControlLibrary.PandC.xml");
XmlTextReader xr = new XmlTextReader(file);
while(xr.Read())
{
if(xr.NodeType == XmlNodeType.Element && xr.Name == "province")
{
parr.Add(xr.GetAttribute("name"));
arr = new ArrayList();
}
if(xr.NodeType == XmlNodeType.Element && xr.Name == "city")
{
arr.Add(xr.GetAttribute("name"));
}
if(xr.NodeType == XmlNodeType.EndElement && xr.Name == "province")
{
carr.Add(arr);
}
}
xr.Close();
}
#region IPostBackDataHandler 成员
public event EventHandler TextChanged;
/// <summary>
/// 当由类实现时,用信号要求服务器控件对象通知 ASP.NET 应用程序该控件的状态已更改。
/// </summary>
public virtual void RaisePostDataChangedEvent()
{
OnTextChanged(EventArgs.Empty);
}
protected virtual void OnTextChanged(EventArgs e)
{
if (TextChanged != null)
TextChanged(this,e);
}
/// <summary>
/// 当由类实现时,为 ASP.NET 服务器控件处理回发数据。
/// </summary>
/// <param name="postDataKey">控件的主要标识符</param>
/// <param name="postCollection">所有传入名称值的集合</param>
/// <returns>如果服务器控件的状态在回发发生后更改,则为 true;否则为 false。</returns>
public virtual bool LoadPostData(string postDataKey, NameValueCollection postCollection)
{
String presentValue = Text;
String postedValue = postCollection[postDataKey];
if (presentValue == null || !presentValue.Equals(postedValue))
{
Text = postedValue;
return true;
}
return false;
}
#endregion
}
}
这个控件就不能被其他控件嵌套