在生成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);系统不报错。但是我发现控件根本没有建立在相应的页面中!!!

解决方案 »

  1.   

    protected System.Web.UI.WebControls.Panel controlPanel;
    ...controlPanel.Controls.Add(txtStudentID);
      

  2.   

    controlPanel.Controls.Add(txtStudentID);
    txtStudentID.Attributes.Add("onKeypress","javascript:if (event.keyCode < 45 || event.keyCode > 57) event.returnValue = false;");
      

  3.   

    tks,不能使用Attributes的方式,因为我的 javascript要访问StudentID,Attributes的方式是不能访问的?我也不能用隐藏控件,因为界面太多,所以只能用生成控件的时候自动生成另外一个保存他的ID值!
      

  4.   

    或者你就直接在override Render里固定ID!例子MSDN上有!
      

  5.   

    楼主是要生成一个包含一个文本框,一个隐藏域的复合控件吧?既然 CreateChildControls() 方法中不能修改控件集合,且 Load,Init,UnOnLoad时,也不能改变控件集合!那么试一下其它方法: public class Student: TextBox
        {
            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);
            }
        }
      

  6.   

    以上代码测试通过。要用js控制动态生成的textbox,需要从其ClientID中取。
    txtStudentID.ID = "txtStudentID";这个ID并不一定是客户端的ID
      

  7.   

    有点错误:
    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);
    }
    }
      

  8.   

    楼主是1.1还是2.0??以上是2.0的解决方法。
    若在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+"'>" );

    }
      

  9.   

    或者,无论1.1还是2.0你可以从头写一个提供两个域的控件,参考一下:
    /// <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 ; }
    }
     
    }
      

  10.   

    continue:/// <summary>
    /// 选择按纽
    /// </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
    }
      

  11.   

    以上两个类生成html需要做一些修改,把js去掉。game over ~
      

  12.   

    <%@ Page Language="C#" AutoEventWireup="true" Debug="true" %><%@ Import Namespace="System.Data" %><script runat="server"> 
    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>
      

  13.   

    看不懂楼主的CreateChildControl在干什么,你为什么想把你控件内的控件添加到Page上面去?作为一个控件,管好自己内部的事情就是了。改为this.Controls.Add就更加没意义了,本来你就继承TextBox控件,你还在自己里面再添加一个TextBox控件,那就是什么?
      

  14.   

    http://community.csdn.net/Expert/topic/5073/5073214.xml?temp=.3755915
    TKS! 
    孟子大哥的这种做法!但是,这种方式在页面PostBack以后,该控件就消失了!服务器端不能再访问到了!jianyi0115(随意)的OnPagePreLoad(object sender, EventArgs e)没有该函数!!
      

  15.   

    jianyi0115(随意)的OnPagePreLoad(object sender, EventArgs e)没有该函数!!
    -----------------------
    o,我用下拉列表测的~ , Textbox竟然没这个方法。还是直接拼html吧~
      

  16.   

    or try :public class CertificateType : System.Web.UI.WebControls.WebControl
        {
             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 );
            }
        
                }