在生成TextBox txtStudentName的时候,同时生成Javascript可以访问的控件TextBox txtStudentID,用了自定义控件的方式,class Student : TextBox
{
protected void CreateChildControls()
{
TextBox txtStudentID = new TextBox();
txtStudentID.ID = "txtStudentID";
this.Page.Form.Controls.Add(txtStudentID);
}
}报错为在Load,Init,UnOnLoad时,不能改变控件集合!
改变this.Page.Form.Controls.Add(txtStudentID);为
this.Controls.Add(txtStudentID);系统不报错。但是我发现控件根本没有建立在相应的页面中!!!
...controlPanel.Controls.Add(txtStudentID);
txtStudentID.Attributes.Add("onKeypress","javascript:if (event.keyCode < 45 || event.keyCode > 57) event.returnValue = false;");
{
public CertificateType()
{
} public string HiddenClientId
{
get
{
return _hidId.ID ;
}
} public string HiddenValue
{
get
{
return _hidId.Value;
}
} private System.Web.UI.WebControls.HiddenField _hidId = new HiddenField (); //protected override void OnPreRender(EventArgs e)
//{
// base.OnPreRender(e); // this.Parent.Controls.Add(_hidId);
//} protected override void OnPagePreLoad(object sender, EventArgs e)
{
base.OnPagePreLoad(sender, e); _hidId.ID = "studentId"; this.Parent.Controls.Add(_hidId);
}
}
txtStudentID.ID = "txtStudentID";这个ID并不一定是客户端的ID
public class Student: TextBox
{
//获取隐藏域在客户端的id
public string HiddenClientId
{
get
{
return _hidId.ID ;
}
}
//获取隐藏域的值
public string HiddenValue
{
get
{
return _hidId.Value;
}
}private System.Web.UI.WebControls.HiddenField _hidId = new HiddenField ();//protected override void OnPreRender(EventArgs e)
//{
// base.OnPreRender(e);// this.Parent.Controls.Add(_hidId);
//}protected override void OnPagePreLoad(object sender, EventArgs e)
{
base.OnPagePreLoad(sender, e);_hidId.ID = "studentId";this.Parent.Controls.Add(_hidId);
}
}
若在1.1中:public class Student: TextBox
{//获取隐藏域在客户端的id
public string HiddenClientId
{
get
{
return this.ClientID + "_studentId";
}
}
//获取隐藏域的值
public string HiddenValue
{
get
{
return Page.Request.Form[HiddenClientId];
}
}
protected override void Render( writer )
{
base.OnRender(e);writer.Write( "<input type='' name='"+HiddenClientId+"' id='"+HiddenClientId+"'>" );
}
}
/// <summary>
/// 选择按纽基类
/// </summary>
public abstract class SelectControl : System.Web.UI.WebControls.WebControl
{
private string _SelectedValue = "" ;
private string _SelectedText = "" ; /// <summary>
/// 选择 函数
/// </summary>
public string SelectFunction ; protected string Param = ""; /// <summary>
/// 获取站点根目录
/// </summary>
protected string SiteRoot
{
get
{
string p = Page.Request.ApplicationPath ;
if( p != "/" )
return p + "/";
else
return p ;
}
} /// <summary>
/// 获取或设置选择的单位ID
/// </summary>
public string SelectedValue
{
set{ _SelectedValue = value ; }
get
{
return _SelectedValue ;
}
} /// <summary>
/// 获取对话框参数
/// </summary>
/// <returns></returns>
protected string GetDialogParam()
{
return "dialogWidth="+this.DialogWidth+"; dialogHeight="+this.DialogHeight+"; help=no;status=no; scroll=no; resizable=yes;" ;
} /// <summary>
/// 获取或设置选择的单位名
/// </summary>
public string SelectedText
{
set{ this._SelectedText = value ; }
get{ return this._SelectedText ; }
} private string _DialogUrl = "";
[Editor( typeof( System.Web.UI.Design.UrlEditor ) , typeof(System.Drawing.Design.UITypeEditor) ) , Category("Setting")]
public string DialogUrl
{
set{ this._DialogUrl = value ; }
get{ return this._DialogUrl ; }
} private Unit _DialogWidth = new Unit( "280px" );
public Unit DialogWidth
{
set{ this._DialogWidth = value ; }
get{ return this._DialogWidth ; }
} private Unit _DialogHeight = new Unit( "500px" );
public Unit DialogHeight
{
set{ this._DialogHeight = value ; }
get{ return this._DialogHeight ; }
} private string _ButtonCssClass = "";
public string ButtonCssClass
{
set{ this._ButtonCssClass = value ; }
get{ return this._ButtonCssClass ; }
} private string _ButtonStyle = "";
public string ButtonStyle
{
set{ this._ButtonStyle = value ; }
get{ return this._ButtonStyle ; }
} private bool _Readonly = false ;
public bool Readonly
{
set{ this._Readonly = value ; }
get{ return this._Readonly ; }
}
private string _ButtonImgageUrl = "";
[Editor( typeof( System.Web.UI.Design.UrlEditor ) , typeof(System.Drawing.Design.UITypeEditor) ) , Category("Setting")]
public string ButtonImgageUrl
{
set{ this._ButtonImgageUrl = value ; }
get{ return this._ButtonImgageUrl ; }
}
private string _ButtonText = "选择...";
public string ButtonText
{
set{ this._ButtonText = value ; }
get{ return this._ButtonText ; }
}
}
/// 选择按纽
/// </summary>
public class SelectButton : SelectControl , System.Web.UI.IPostBackDataHandler
{
private string _TextBoxCssClass = "";
public string TextBoxCssClass
{
set{ _TextBoxCssClass = value ; }
get{ return _TextBoxCssClass ; }
} private string _TexBoxStyle = "";
public string TextBoxStyle
{
set{ _TexBoxStyle = value ; }
get{ return _TexBoxStyle ; }
}
protected override void Render(HtmlTextWriter writer)
{
//Param = "dialogWidth="+this.DialogWidth+"; dialogHeight="+this.DialogHeight+"; help=no;status=no; scroll=no; resizable=yes;" ; base.RenderBeginTag( writer ) ; if( !base.Readonly )
{
writer.WriteLine( "<input title='双击删除' ondblclick=SelectControl_Clear('"+this.ID+"') readonly type=textbox name='"+ this.ID +"' id='"+this.ClientID+"' value='" + this.SelectedText + "' " );
}
else
{
writer.WriteLine( "<input readonly type=textbox name='"+ this.ID +"' id='"+this.ClientID+"' value='" + this.SelectedText + "' " );
} if( this.TextBoxCssClass != "" )
writer.Write( " class='"+this.TextBoxCssClass+"'" ); if( this.TextBoxStyle != "" )
writer.Write( " style='"+this.TextBoxStyle+"'" ); writer.Write( " >" );
writer.Write( "<input type=hidden name='"+ this.ID +"_Value' id='"+ this.ClientID +"_Value' value='" + this.SelectedValue + "'>" ); if( !base.Readonly )
{
if( ButtonImgageUrl == "" )
writer.Write( "<input type=button value='" + ButtonText + "' " );
else
writer.Write( "<img src='"+ButtonImgageUrl+"' align='absmiddle' " ); if( ButtonStyle != "" )
writer.Write( "style='"+ButtonStyle+"'" ); if( ButtonCssClass != "" )
writer.Write( "class='"+ButtonCssClass+"'" ); if(!base.Enabled)
writer.Write( "disabled='disabled' " ) ;
if( DialogUrl == null )
writer.Write( " onclick=\"" + SelectFunction + "('" + this.ID +"','"+ Param +"')\" >" );
else
writer.Write( " onclick=\"_BaseSelect('" + this.ID +"','"+ this.DialogUrl +"','"+ base.GetDialogParam() +"')\" >" );
} base.RenderEndTag( writer ) ;
} #region IPostBackDataHandler 成员 public void RaisePostDataChangedEvent()
{
} public bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
{ this.SelectedText = postCollection[ this.ID ];
this.SelectedValue = postCollection[ this.ID + "_Value" ]; return false;
} #endregion
}
class Student : TextBox
{
protected override void CreateChildControls()
{
}protected override void Render(HtmlTextWriter output)
{
TextBox txtStudentID = new TextBox();
txtStudentID.ID = "txtStudentID"; txtStudentID.RenderControl(output);
Literal l = new Literal();
//js变量
l.Text = "<script type='text/javascript'>function getV(){alert(document.getElementById('" + txtStudentID.ClientID + "').value) }</s" + "cript>";
l.RenderControl(output);
}
}protected void Page_Load(object sender, EventArgs e)
{
Student a = new Student();
Page.Form.Controls.Add(a);}</script><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>DropDownList 补充例子</title>
</head>
<body>
<form id="form2" runat="server">
<asp:Button ID="Button1" runat="server" OnClientClick="getV()" Text="Button" />
</form>
</body>
</html>
TKS!
孟子大哥的这种做法!但是,这种方式在页面PostBack以后,该控件就消失了!服务器端不能再访问到了!jianyi0115(随意)的OnPagePreLoad(object sender, EventArgs e)没有该函数!!
-----------------------
o,我用下拉列表测的~ , Textbox竟然没这个方法。还是直接拼html吧~
{
public TextBox NameBox = new TextBox();
public TextBox IdBox = new TextBox(); protected override void OnInit(EventArgs e)
{
base.OnInit(e); NameBox.ID = "Name";
IdBox.ID = "Id"; this.Controls.Add(NameBox);
this.Controls.Add( IdBox );
}
}