for (int i = 0; i < j; i++)
{
DropDownList t = new DropDownList();
t.ID = "a" + i.ToString();
t.DataSource = source;
t.DataTextField = "ppositions";
t.DataBind();
t.Height = 25;
TextBox d = new TextBox();
d.ID = "b" + i.ToString();
d.Width = 40;
d.Height = 20;
TextBox c = new TextBox();
c.ID = "c" + i.ToString();
c.Width = 40;
c.Height = 20;
TextBox f = new TextBox();
f.ID = "f" + i.ToString();
f.Width = 40;
RangeValidator r = new RangeValidator();
r.ID = "r" + i.ToString();
r.ControlToValidate = "f" + i.ToString();
r.ErrorMessage="只能输入数字";
//r.Type = "Integer";
this.Panel1.Controls.Add(t);
this.Panel2.Controls.Add(d);
this.Panel3.Controls.Add(c);
this.Panel4.Controls.Add(f);
}
添加控件是成功了,可老是取不到值,我要做的是往数据库里填数据的模块,谁知道怎么取动态生成控件的值啊? 尽量详细些 最好给个例子看看 感激不尽
{
DropDownList t = new DropDownList();
t.ID = "a" + i.ToString();
t.DataSource = source;
t.DataTextField = "ppositions";
t.DataBind();
t.Height = 25;
TextBox d = new TextBox();
d.ID = "b" + i.ToString();
d.Width = 40;
d.Height = 20;
TextBox c = new TextBox();
c.ID = "c" + i.ToString();
c.Width = 40;
c.Height = 20;
TextBox f = new TextBox();
f.ID = "f" + i.ToString();
f.Width = 40;
RangeValidator r = new RangeValidator();
r.ID = "r" + i.ToString();
r.ControlToValidate = "f" + i.ToString();
r.ErrorMessage="只能输入数字";
//r.Type = "Integer";
this.Panel1.Controls.Add(t);
this.Panel2.Controls.Add(d);
this.Panel3.Controls.Add(c);
this.Panel4.Controls.Add(f);
}
添加控件是成功了,可老是取不到值,我要做的是往数据库里填数据的模块,谁知道怎么取动态生成控件的值啊? 尽量详细些 最好给个例子看看 感激不尽
解决方案 »
- 如何获取动态控件的值?
- winform日历控件问题,解决了一百分全部送上 ====急
- href后加变量怎么连接外网啊
- 压缩Access数据库后,表中的记录为何会重新排序?
- 关于remoting安全的问题
- 请问谁有转换好的拼音文件,共享一下,用于提取汉字的首字母。
- C# Winform中使用mapxtreme
- 用ADO.NET访问数据库的问题
- 开机自动启动winform程序
- C++ dll调用的问题!!!无法加载制定模块
- 用c#新建一个csv文件,其中有一字段是备注memo类型,为什么插入数据内容时不全,内容被截断了?
- 如何判断鼠标点击的是DATAGRID的表内数据行上?而不是表头或列头或其它空白处?
然后方法通过事件处理方法的 object sender 参数获取控件的任何值
不知道是不是你要的那个:
protected void Page_Load(object sender, EventArgs e)
{
DropDownList drp;
TextBox txt;
Label lbl;
for (int i = 0; i < 4; i++)
{
drp = new DropDownList();
drp.ID = "drp" + i.ToString();
drp.Width = 120;
txt = new TextBox();
txt.ID = "txt" + i.ToString();
txt.Width = 120; lbl = new Label();
lbl.ID = "lbl" + i.ToString();
lbl.Width = 120; this.form1.Controls.Add(drp);
this.form1.Controls.Add(txt);
this.form1.Controls.Add(lbl);
}
if (!IsPostBack)
bindDrp0();
}
protected void Button1_Click(object sender, EventArgs e)
{
DropDownList drp0=(DropDownList) this.Page.FindControl("drp0");
Label lbl0 = (Label)this.Page.FindControl("lbl0");
lbl0.Text = drp0.SelectedValue;
} private void bindDrp0()
{
for (int i = 0;i < 4; i++ )
{
DropDownList drp0 = (DropDownList)this.Page.FindControl("drp0");
drp0.Items.Add(new ListItem(i.ToString(), i.ToString()));
}
}
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;
using System.Collections.Generic;
using OAManagerBLL;
using OAManagerModel;
using System.Drawing;public partial class CheckSet : System.Web.UI.Page
{
private CheckTimeManager ctm = new CheckTimeManager();
private List<CheckTime> list = null;
int listcount = 0; protected void Page_Load(object sender, EventArgs e)
{ if (!Page.IsPostBack)
{
csDivb.Visible = false;
}
CreateList();
} //选择日期段,插入数据库并查询出来
protected void btnSet_Click(object sender, EventArgs e)
{
string start = this.txtStart.Text.Trim();
string end = this.txtEnd.Text.Trim(); if (!string.IsNullOrEmpty(start) && !string.IsNullOrEmpty(end))
{
CheckTime ctStart = new CheckTime(start);
CheckTime ctEnd = new CheckTime(end);
ctm.AddDates(ctStart, ctEnd);
list = ctm.GetSomeCheckTime(ctStart, ctEnd);
if (list != null)
{
listcount = list.Count;
ViewState["listcount"] = listcount;
}
}
CreateList();
} //动态创建表格行列和lable
protected void CreateList()
{
csDivb.Visible = true;
Label lbl = null;
TableRow tr = null;
TableCell tc = null; int rs = listcount / 7 + 1;
string str = ""; tr = new TableRow();
tr.Cells.Clear(); string[] strings = { "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日" };
for (int i = 0; i < strings.Length; i++)
{
lbl = new Label();
tc = new TableCell();
lbl.Text = strings[i];
tc.Controls.Add(lbl);
tr.Cells.Add(tc);
tc.HorizontalAlign = HorizontalAlign.Center;
tr.VerticalAlign = VerticalAlign.Middle;
cstable.Rows.Add(tr);
} int n = 0;
for (int row = 1; row <= rs; row++)
{
tr = new TableRow();
for (int cs = row * 7; cs < (row + 1) * 7; cs++)
{
tc = new TableCell();
if (n < listcount)
{
if (n == 0)
{
str = list[0].CheckDate.Trim();
DateTime dt = Convert.ToDateTime(str);
int s = int.Parse(dt.DayOfWeek.ToString("d")); if (s == 0)
{
s = 7;
} for (int i = 0; i < s; i++)
{
tc = new TableCell();
tc.BackColor = Color.White;
tr.Cells.Add(tc);
cstable.Rows.Add(tr);
}
cs = cs + s - 1;
}
str = list[n].CheckDate.Trim();
lbl = new Label();
lbl.ID = "lbl" + n.ToString();
lbl.Text = str.Substring(0, str.IndexOf(" "));
lbl.EnableViewState = true;
tc.BackColor = Color.GreenYellow;
tc.Controls.Add(lbl);
tc.HorizontalAlign = HorizontalAlign.Center;
n++;
}
lbl.Attributes.Add("onClick", "cellClick(this)");//点击改变lable背景颜色
tr.Cells.Add(tc);
}
cstable.Rows.Add(tr);
cstable.EnableViewState = true;
csDivb.EnableViewState = true;
} } //获取背景颜色,根据颜色判定他是休息日还是工作日
protected void btnOver_Click(object sender, EventArgs e)
{
csDivb.Visible = false;
int i = Convert.ToInt32(ViewState["listcount"]);
for (int m = 0; m < i; m++)
{
Label lb = this.Page.FindControl("lbl" + m.ToString()) as Label;
if (lb.BackColor == Color.GreenYellow)
{
CheckTime ct = new CheckTime();//日期类
ct.CheckDate = lb.Text.Trim();
ct.TimeState = new TimeState(1);
}
else
{
if (!string.IsNullOrEmpty(lb.Text.Trim()))
{
CheckTime ct = new CheckTime();
ct.CheckDate = lb.Text.Trim();
ct.TimeState = new TimeState(2);
}
}
}
}
}
这个不知道对你有没有用 ,我也还没试过