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;
}
} string _SelectedListText = string.Empty;
public string SelectedListText
{
set
{
_SelectedListText = value;
}
get
{
if(txtListContent == null)
return string.Empty;
return txtListContent.Text.Trim();
}
} string _SelectedListValue = string.Empty;
public string SelectedListValue
{
set
{
_SelectedListValue = value;
}
get
{
if(txtListID == null)
return string.Empty;
return 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.SelectedListText;
ViewState["CUR_VALUE"] = this.SelectedListValue;
return base.SaveViewState ();
} protected override void CreateChildControls()
{
base.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);
} /// <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");
}
}
}
在获取文本框值的时候控件都被重新初始化了,怎样改?

解决方案 »

  1.   

    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;
    }
    } string _SelectedListText = string.Empty;
    public string SelectedListText
    {
    set
    {
    _SelectedListText = value;
    }
    get
    {
    return txtListContent.Text.Trim();
    }
    } string _SelectedListValue = string.Empty;
    public string SelectedListValue
    {
    set
    {
    _SelectedListValue = value;
    }
    get
    {
    return 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.SelectedListText;
    ViewState["CUR_VALUE"] = this.SelectedListValue;
    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");
    }
    }
    }
    这样也不行,文本框未实例化