switch (field_type)
{
case "datetime":
c1 = LoadControl(Request.ApplicationPath + "/UserControl/dateControl.ascx");
ctb.Controls.Add(c1);
dateControl dttb = (dateControl)c1; //强制转换
dateControl().stringLbValue(field_title); //Label赋值
break;
case "text":
//..........其它自定义控件..........
}自定义控件里有Label、TextBox 在修改页面时如何给这些自定义控件赋值、取值、如何确定哪个自定义控件中的TextBox对应相应的值 ,例子 例子!!高手帮忙、先谢谢!!
解决方案 »
- 如何快速在连续数字的中找出缺失数字
- 横向出现滚动条?
- 如何全选ID号相同的CHECKBOX
- jvascript得到datagrid的CurrentPageIndex属性
- 求各位:怎样实现把整个asp.net2.0网站的文件生成静态html文件啊?
- 怎样用 asp.net 在exchange2010 中创建信箱 ?
- ???关于TreeView中返回选中的checkbox的问题
- 新增一条数据后,马上获取这条记录的ID号
- 急....C#控制aspx浏览窗口的大小
- 请问application,session,cookie的用法。
- iframe中子页面如何让父页面中listView中得数据重新绑定。
- .net中怎么在网页中打开office组件
{
case "datetime":
c1 = LoadControl(Request.ApplicationPath + "/UserControl/dateControl.ascx");
ctb.Controls.Add(c1);
dateControl dttb = (dateControl)c1; //强制转换
dttb.stringLbValue(field_title); //Label赋值
break;
case "text":
//..........其它自定义控件..........
}
要定义控件的属性,也就是get 和 set
[code=C#]
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class Controls_DatePicker : System.Web.UI.UserControl
{
private string _Year, _Month, _Day,_text;
private int _MinYear, _MaxYear;
private string _style = "ymd"; //控件显示样式
const int MAXYEAR = 2015; //默认年份
const int MINYEAR = 1990;
#region 设置或返回此控件的值
/// <summary>
/// 设置或返回此控件的值
/// </summary>
public string Text
{
get
{
string temp = "";
if (dwDay.Visible)
{ temp= this.dwYear.Text + "-" + this.dwMonth.Text + "-" + this.dwDay.Text; }
else
{
if (dwMonth.Visible)
{
temp= this.dwYear.Text + "-" + this.dwMonth.Text + "-01";
}
else
{
temp= dwYear.Text + "-01-01";
}
}
return temp;
//if (Galsun.InputCheck.IsTrueDate(temp))
//{
// return temp;
//}
//else
//{
// Response.Write("<script>alert('不是有效的日期!');</script>");
// return "2007-1-1";
//}
}
set
{
_text = value;
setText(value);
}
}
private void setText(string value)
{
string date = DateTime.Parse(value).ToString("yyyy-MM-dd");
_Year = date.Substring(0, 4);
_Month = date.Substring(5, 2);
_Day = date.Substring(8, 2);
this.dwYear.Text = _Year;
this.dwMonth.Text = _Month;
this.dwDay.Text = _Day;
}
#endregion #region 返回年份
/// <summary>
/// 返回年份
/// </summary>
public string Year
{
get
{
return dwYear.Text;
}
set
{
dwYear.Text = value;
}
}
#endregion #region 设置控件显示样式
/// <summary>
/// 设置控件显示样式,如ym ,则只显示年月
/// </summary>
public string ShowStyle
{
set
{
_style = value;
} }
#endregion
#region 最小年份
/// <summary>
/// 最小年份,默认为1990年
/// </summary>
public int MinYear
{
set
{
_MinYear = value;
}
}
#endregion #region 最大年份
/// <summary>
/// 最大年份,默义为2015年
/// </summary>
public int MaxYear
{
set
{
_MaxYear = value;
}
}
#endregion
#region 加载
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//设置显示年份
int maxYear, minYear;
maxYear = _MaxYear == 0 ? MAXYEAR : _MaxYear;
minYear = _MinYear == 0 ? MINYEAR : _MinYear;
if (!IsPostBack)
{
for (int i = minYear; i <= maxYear; i++)
{
dwYear.Items.Add(new ListItem(i.ToString(), i.ToString()));
}
}
//显示月
for (int i = 1; i <= 12; i++)
{
string month = i.ToString().Length == 1 ? "0" + i.ToString() : i.ToString();
dwMonth.Items.Add(new ListItem(month, month));
}
//显示月
for (int i = 1; i <= 31; i++)
{
string day = i.ToString().Length == 1 ? "0" + i.ToString() : i.ToString();
dwDay.Items.Add(new ListItem(day, day));
}
//显示格式 if (_style.IndexOf('y') != -1)
dwYear.Visible = true;
else
dwYear.Visible = false;
if (_style.IndexOf('m') != -1)
dwMonth.Visible = true;
else
dwMonth.Visible = false;
if (_style.IndexOf('d') != -1)
dwDay.Visible = true;
else
dwDay.Visible = false; if (_text == null)
{
_text = DateTime.Now.Date.ToString();
}
setText(_text);
}
}
#endregion
}
[/code]
{
get{ return textbox1.text;}
}
然后获取这个属性就可以了。
ctb.Controls.Add(c1);
textBoxControl tb = (textBoxControl)c1;
tb.labelValue ="aaaaa"
但是取控件里面的值就取不上了str=tb.labelValue;一直是空。取的不是页面上带的自定义,好像是新增了个 所以一直取的空 该如何搞呢???
我控件里面写的public string textboxValue
{
get { return TextBox1.Text; }
set { TextBox1.Text = value; }
}
public string labelValue
{
get { return Label1.Text; }
set { Label1.Text = value; }
}
而且从数据库里判断显示那个自定义控件
textBoxControl tb = (textBoxControl)c1;
tb.labelValue ="aaaaa"
ctb.Controls.Add(c1);
先附值,再加载试试
textBoxControl tb = (textBoxControl)c1;
string value=tb.labelValue;
c1 = LoadControl(Request.ApplicationPath + "/UserControl/textBoxControl.ascx");
c1.ID="";//这个可以和你数据库中的记录挂钩,这样也便于查找对应Control
ctb.Controls.Add(c1);
然后在取值时使用 FindControl(id) eg:c1=FindControl(id);
textBoxControl tb = (textBoxControl)c1;
string value=tb.labelValue;ps: FindControl 这个方法效率很低下,你可以自己写个遍历指定控件子元素的方法我只是用FindControl来说明思路