菜鸟来了,高手请进,50分求助!
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 _SelectedListText;
}
}
string _SelectedListValue = string.Empty;
public string SelectedListValue
{
set
{
_SelectedListValue = value;
}
get
{
return _SelectedListValue;
}
}
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 ");
}
}
}
文本框子控件总是被重新实例化了,值被清空
[解决办法]