public class TripalCheckList : System.Web.UI.WebControls.WebControl
{
private TextBox txtListContent;
                  //构造函数
public TripalCheckList()
{
txtListContent = new TextBox();
}
                  public string SelectedListText
{
set
{
this.txtListContent.Text = value;
}
get
{
return this.txtListContent.Text;
}
}
        }
点击button时将该控件的值赋给另一个文本框,可该控件在执行构造函数时new了,结果值被清空了,怎么处理啊,急死

解决方案 »

  1.   

    如果你只需要用它来保存值的话,可用viewsate,如果要显示的话.可以将它添加到某个宣传控件中.即使下次再new 再添加到控件,它的值依然会存在.
      

  2.   

    该控件要显示,并且还要获取该控件中的值,相当于下拉框的文本框,但是SaveViewState方法在构造函数以后执行,值已经清空了
      

  3.   

    protected override void CreateChildControls()
    {
    txtListContent.ID = this.ClientID + "_Text";
    this.Controls.Add(txtListContent);
    base.CreateChildControls ();
    }
    我后面写了这段,主要是因为构造函数new了子控件,new控件的代码放哪里都不好使,如果不放的话调用属性又报未实例化的错
      

  4.   

    protected override void CreateChildControls()
    {
                               txtListContent = new TextBox();
    txtListContent.ID = this.ClientID + "_Text";
    this.Controls.Add(txtListContent);
    base.CreateChildControls ();
    }
    设计时呈现不了
    取值报错未实例化该控件
      

  5.   

    this.Controls.Add(txtListContent);这样有问题.
    你要把这个textbox显示在哪里?
      

  6.   

    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.ComponentModel;namespace Tripal.Web.UI.Controls
    {
    /// <summary>
    /// WebCustomControl1 的摘要说明。
    /// </summary>
    [DefaultProperty("Text"), 
    ToolboxData("<{0}:TripalCheckList runat=server></{0}:TripalCheckList>")]
    public class TripalCheckList : System.Web.UI.WebControls.WebControl
    {
    private TextBox txtListContent;
    private TextBox txtListID;
    private DataTable _DataSource = null;
    private string _DataValueField;
    private string _DataTextField;
    private string _DropDownHeight=string.Empty; public TripalCheckList()
    {


    public override Unit Width
    {
    get
    {
    return txtListContent.Width;
    }
    set
    {
    txtListContent.Width = value;
    base.Width = value;
    }
    } public string DropDownHeight
    {
    get
    {
    return _DropDownHeight;
    }
    set
    {
    _DropDownHeight = value;
    }
    } /// <summary>
    /// 设置控件数据源
    /// </summary>
    public DataTable DataSource 
    {
    set
    {
    _DataSource = value;
    }
    }

    /// <summary>
    /// 设置数据绑定的值
    /// </summary>
    public string DataValueField
    {
    set
    {
    _DataValueField = value;
    }
    } /// <summary>
    /// 设置列表数据显示文本
    /// </summary>
    public string DataTextField
    {
    set
    {
    _DataTextField = value;
    }
    } public string SelectedListText
    {
    set
    {
    this.txtListContent.Text = value;
    }
    get
    {
    return this.txtListContent.Text;
    }
    } public string SelectedListValue
    {
    set
    {
    this.txtListID.Text = value;
    }
    get
    {
    return this.txtListID.Text;
    }
    } protected override void LoadViewState(object savedState)
    {
    base.LoadViewState (savedState);
    this.SelectedListText = ViewState["CUR_TEXT"].ToString();
    this.SelectedListValue = ViewState["CUR_VALUE"].ToString();
    } protected override object SaveViewState()
    {
    ViewState["CUR_TEXT"] = this.txtListContent.Text.Trim();
    ViewState["CUR_VALUE"] = this.txtListID.Text;
    return base.SaveViewState ();
    } protected override void CreateChildControls()
    {
    txtListContent = new TextBox();
    txtListContent.Attributes.Add("style","border-right-style:none");
    //txtListContent.ReadOnly = true;
    //txtListContent.Height = 20;
    txtListID = new TextBox();
    txtListID.Attributes.Add("style","display:none"); txtListContent.ID = this.ClientID + "_Text";
    txtListID.ID = this.ClientID + "_Value";
    this.Controls.Add(txtListContent);
    this.Controls.Add(txtListID);
    base.CreateChildControls ();
    } /// <summary> 
    /// 将此控件呈现给指定的输出参数。
    /// </summary>
    /// <param name="output"> 要写出到的 HTML 编写器 </param>
    protected override void Render(HtmlTextWriter output)
    {
    // output.AddAttribute("width",(Convert.ToInt32(txtListContent.Width)+17).ToString());
    output.AddAttribute(HtmlTextWriterAttribute.Border,"0");
    output.AddAttribute(HtmlTextWriterAttribute.Cellpadding,"0");
    output.AddAttribute(HtmlTextWriterAttribute.Cellspacing,"0");
    output.RenderBeginTag(HtmlTextWriterTag.Table);
    output.RenderBeginTag(HtmlTextWriterTag.Tr);
    output.RenderBeginTag(HtmlTextWriterTag.Td);
    txtListContent.RenderControl(output);
    txtListID.RenderControl(output);
    output.RenderEndTag();
    output.RenderBeginTag(HtmlTextWriterTag.Td);
    output.AddAttribute("type","Button");//
    output.AddAttribute("class","DropDown_Button");//
    output.AddAttribute("onclick","TripalCheckList_ShowListPanel('"+this.ClientID+"')");
    output.RenderBeginTag(HtmlTextWriterTag.Input);
    output.RenderEndTag();
    output.RenderEndTag();
    output.RenderEndTag();
    output.RenderEndTag();
    output.WriteFullBeginTag("iframe id='"+this.ClientID+"_iframe' style='display:none;position:absolute;z-index:9;height:"+_DropDownHeight+";'");
    output.WriteEndTag("iframe");
    output.WriteFullBeginTag("div id='"+this.ClientID+"_DivDataList' style='background-color:white;overflow-y:auto;display:none;position:absolute;z-index:10;height:"+_DropDownHeight+";'");
    output.AddAttribute(HtmlTextWriterAttribute.Id,this.ClientID+"_HabitList");
    output.AddAttribute("width","100%");
    output.AddAttribute("class","SearchPanelContentTable");
    output.RenderBeginTag(HtmlTextWriterTag.Table);
    if(_DataSource != null)
    {
    foreach(DataRow row in _DataSource.Rows)
    {
    output.RenderBeginTag(HtmlTextWriterTag.Tr);
    output.RenderBeginTag(HtmlTextWriterTag.Td);
    CheckBox box = new CheckBox();
    if(_DataValueField != string.Empty && _DataTextField != string.Empty)
    {
    box.ID=this.ClientID + "_" +row[_DataValueField].ToString();
    if(_DataTextField != string.Empty)
    {
    box.Attributes.Add("onclick","TripalCheckList_ListClick('"+this.ClientID+"',this,'"+row[_DataValueField].ToString()+"','"+row[_DataTextField].ToString()+"')");
    }
    box.RenderControl(output);

    }
    output.RenderEndTag();
    output.RenderBeginTag(HtmlTextWriterTag.Td);
    if(_DataTextField != string.Empty)
    {
    output.Write(row[_DataTextField].ToString());
    }
    output.RenderEndTag();
    output.RenderEndTag();

    }
    }
    output.RenderEndTag();
    output.WriteEndTag("div");
    }
    }
    }
    整个控件的代码
      

  7.   

    public TripalCheckList()
    {
                this.txtListContent = new TextBox();
                this.txtListID = new TextBox();
    }放在构造函数里.去掉CreadChild那里的两句.值可以保存.
      

  8.   

    首先非常感谢回复
    构造函数每次执行new清空了文本框的值,属性再获取值时都是string.Empty
      

  9.   

    private void Button1_Click(object sender, System.EventArgs e)
    {
    TextBox1.Text = TripalCheckList1.SelectedListText;
    }
      

  10.   

    public TripalCheckList()
    {
                 
                this.txtListContent = new TextBox();
                this.txtListContent.ID = "AAAAAA";
                this.txtListID = new TextBox();
                this.Controls.Add(this.txtListContent);
                this.Controls.Add(this.txtListID);
                 
    }CreadChild那里不要添加
      

  11.   

    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.ComponentModel;namespace Tripal.Web.UI.Controls
    {
    /// <summary>
    /// WebCustomControl1 的摘要说明。
    /// </summary>
    [DefaultProperty("Text"), 
    ToolboxData("<{0}:TripalCheckList runat=server></{0}:TripalCheckList>")]
    public class TripalCheckList : System.Web.UI.WebControls.WebControl
    {
    private TextBox txtListContent;
    private TextBox txtListID;
    private DataTable _DataSource = null;
    private string _DropDownHeight=string.Empty; public TripalCheckList()
    {
    txtListContent = new TextBox();
    txtListContent.Attributes.Add("style","border-right-style:none");
    //txtListContent.ReadOnly = true;
    //txtListContent.Height = 20;
    txtListID = new TextBox();
    txtListID.Attributes.Add("style","display:none");
    this.Controls.Add(txtListContent);
    this.Controls.Add(txtListID);

    public override Unit Width
    {
    get
    {
    return txtListContent.Width;
    }
    set
    {
    txtListContent.Width = value;
    base.Width = value;
    }
    } public string DropDownHeight
    {
    get
    {
    return _DropDownHeight;
    }
    set
    {
    _DropDownHeight = value;
    }
    } /// <summary>
    /// 设置控件数据源
    /// </summary>
    public DataTable DataSource 
    {
    set
    {
    _DataSource = value;
    }
    get
    {
    return _DataSource;
    }
    }

    /// <summary>
    /// 设置数据绑定的值
    /// </summary>
    public string DataValueField
    {
    get
    {
    return ViewState["_DataValueField"].ToString();
    }
    set
    {
    ViewState["_DataValueField"] = value;
    }
    } /// <summary>
    /// 设置列表数据显示文本
    /// </summary>
    public string DataTextField
    {
    get
    {
    return ViewState["_DataTextField"].ToString();
    }
    set
    {
    ViewState["_DataTextField"] = value;
    }
    } public string SelectedListText
    {
    set
    {
    this.txtListContent.Text = value;
    }
    get
    {
    return this.txtListContent.Text;
    }
    } public string SelectedListValue
    {
    set
    {
    this.txtListID.Text = value;
    }
    get
    {
    return this.txtListID.Text;
    }
    } protected override void LoadViewState(object savedState)
    {
    base.LoadViewState (savedState);
    this.DataSource = (DataTable)ViewState["ListData"];
    } protected override object SaveViewState()
    {
    ViewState["CUR_TEXT"] = this.txtListContent.Text.Trim();
    ViewState["CUR_VALUE"] = this.txtListID.Text;
    ViewState["ListData"] = this.DataSource;
    return base.SaveViewState ();
    } protected override void CreateChildControls()
    {
    txtListContent.ID = this.ClientID + "_Text";
    txtListID.ID = this.ClientID + "_Value";
    base.CreateChildControls ();
    } /// <summary> 
    /// 将此控件呈现给指定的输出参数。
    /// </summary>
    /// <param name="output"> 要写出到的 HTML 编写器 </param>
    protected override void Render(HtmlTextWriter output)
    {
    // output.AddAttribute("width",(Convert.ToInt32(txtListContent.Width)+17).ToString());
    output.AddAttribute(HtmlTextWriterAttribute.Border,"0");
    output.AddAttribute(HtmlTextWriterAttribute.Cellpadding,"0");
    output.AddAttribute(HtmlTextWriterAttribute.Cellspacing,"0");
    output.RenderBeginTag(HtmlTextWriterTag.Table);
    output.RenderBeginTag(HtmlTextWriterTag.Tr);
    output.RenderBeginTag(HtmlTextWriterTag.Td);
    txtListContent.RenderControl(output);
    txtListID.RenderControl(output);
    output.RenderEndTag();
    output.RenderBeginTag(HtmlTextWriterTag.Td);
    output.AddAttribute("type","Button");//
    output.AddAttribute("class","DropDown_Button");//
    output.AddAttribute("onclick","TripalCheckList_ShowListPanel('"+this.ClientID+"')");
    output.RenderBeginTag(HtmlTextWriterTag.Input);
    output.RenderEndTag();
    output.RenderEndTag();
    output.RenderEndTag();
    output.RenderEndTag();
    output.WriteFullBeginTag("iframe id='"+this.ClientID+"_iframe' style='display:none;position:absolute;z-index:9;height:"+_DropDownHeight+";'");
    output.WriteEndTag("iframe");
    output.WriteFullBeginTag("div id='"+this.ClientID+"_DivDataList' style='background-color:white;overflow-y:auto;display:none;position:absolute;z-index:10;height:"+_DropDownHeight+";'");
    output.AddAttribute(HtmlTextWriterAttribute.Id,this.ClientID+"_HabitList");
    output.AddAttribute("width","100%");
    output.AddAttribute("class","SearchPanelContentTable");
    output.RenderBeginTag(HtmlTextWriterTag.Table);
    if(_DataSource != null)
    {
    foreach(DataRow row in _DataSource.Rows)
    {
    output.RenderBeginTag(HtmlTextWriterTag.Tr);
    output.RenderBeginTag(HtmlTextWriterTag.Td);
    CheckBox box = new CheckBox();
    if(DataValueField != string.Empty && DataTextField != string.Empty)
    {
    box.ID=this.ClientID + "_" +row[DataValueField].ToString();
    if(DataTextField != string.Empty)
    {
    box.Attributes.Add("onclick","TripalCheckList_ListClick('"+this.ClientID+"',this,'"+row[DataValueField].ToString()+"','"+row[DataTextField].ToString()+"')");
    }
    box.RenderControl(output);

    }
    output.RenderEndTag();
    output.RenderBeginTag(HtmlTextWriterTag.Td);
    if(DataTextField != string.Empty)
    {
    output.Write(row[DataTextField].ToString());
    }
    output.RenderEndTag();
    output.RenderEndTag();

    }
    }
    output.RenderEndTag();
    output.WriteEndTag("div");
    output.Write("<input id='"+this.ClientID+"_Hidden' type='hidden' value='"+txtListContent.Text+"'>");
    }
    }
    }
    修改以后的,还是不行,要疯了
      

  12.   

    protected override void CreateChildControls()
    {
    txtListContent.ID = this.ClientID + "_Text";
    txtListID.ID = this.ClientID + "_Value";
    base.CreateChildControls ();
    }
    id脚本需要调用,构造函数中this.ClientID还未生成
      

  13.   

    txtListContent.ID = this.ClientID + "_Text";
                txtListID.ID = this.ClientID + "_Value";这两个去掉.
      

  14.   

    为什么不能重新给控件赋新的ID,构造函数中的this.ClientID无效
      

  15.   

    txtListContent.ID = this.ClientID + "_Text";
                txtListID.ID = this.ClientID + "_Value";这两个去掉=======就没有问题.
      

  16.   

    构造函数中给文本框ID,这样2个文本框在页面上的ID不能保证唯一啊,一个页面有2个这个控件时,脚本调用会出错
      

  17.   

    生成后的TextBox的ID和构造函数中的给的ID一样,没变化
      

  18.   

    虽然已经结贴了,但是还是写一下吧。1.1 里面继承一个接口就可以了,这个接口比较特殊,不用实现。继承这个就可以了,就会自动保存一些控件的值。INamingContainer比如public Class JYKUniteList System.Web.UI.WebControls.PlaceHolder , INamingContainer
    {
    }Public Class JYKUniteList
        Inherits System.Web.UI.WebControls.PlaceHolder
        Implements INamingContainerEnd Class
      

  19.   

    回复楼上的,我明白INamingContainer的作用,但是我怕我的脚本无法控制自动生成的ID,控件其实没什么复杂的,但是脚本很多