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了,结果值被清空了,怎么处理啊,急死
{
private TextBox txtListContent;
//构造函数
public TripalCheckList()
{
txtListContent = new TextBox();
}
public string SelectedListText
{
set
{
this.txtListContent.Text = value;
}
get
{
return this.txtListContent.Text;
}
}
}
点击button时将该控件的值赋给另一个文本框,可该控件在执行构造函数时new了,结果值被清空了,怎么处理啊,急死
{
txtListContent.ID = this.ClientID + "_Text";
this.Controls.Add(txtListContent);
base.CreateChildControls ();
}
我后面写了这段,主要是因为构造函数new了子控件,new控件的代码放哪里都不好使,如果不放的话调用属性又报未实例化的错
{
txtListContent = new TextBox();
txtListContent.ID = this.ClientID + "_Text";
this.Controls.Add(txtListContent);
base.CreateChildControls ();
}
设计时呈现不了
取值报错未实例化该控件
你要把这个textbox显示在哪里?
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");
}
}
}
整个控件的代码
{
this.txtListContent = new TextBox();
this.txtListID = new TextBox();
}放在构造函数里.去掉CreadChild那里的两句.值可以保存.
构造函数每次执行new清空了文本框的值,属性再获取值时都是string.Empty
{
TextBox1.Text = TripalCheckList1.SelectedListText;
}
{
this.txtListContent = new TextBox();
this.txtListContent.ID = "AAAAAA";
this.txtListID = new TextBox();
this.Controls.Add(this.txtListContent);
this.Controls.Add(this.txtListID);
}CreadChild那里不要添加
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+"'>");
}
}
}
修改以后的,还是不行,要疯了
{
txtListContent.ID = this.ClientID + "_Text";
txtListID.ID = this.ClientID + "_Value";
base.CreateChildControls ();
}
id脚本需要调用,构造函数中this.ClientID还未生成
txtListID.ID = this.ClientID + "_Value";这两个去掉.
txtListID.ID = this.ClientID + "_Value";这两个去掉=======就没有问题.
{
}Public Class JYKUniteList
Inherits System.Web.UI.WebControls.PlaceHolder
Implements INamingContainerEnd Class