我现在用的是这个方法来向页面添加控件
TextBox newControls=new TextBox();
newControls.ID = "textbox1";
newControls.Width = 100;
newControls.Style.Add("LEFT","txt1.text");
newControls.Style.Add("TOP","txt2.text");
Page.Controls[1].Controls.Add(newControls);
但用这个方法定义出的控件页面一刷新就没了,而且不能加载事件。知不知道有什么方法可以让这样添加的控件一直留在页面上,并且把相关信息保存到数据库里,下次能动态的显示出来,并且要能加载事件。
TextBox newControls=new TextBox();
newControls.ID = "textbox1";
newControls.Width = 100;
newControls.Style.Add("LEFT","txt1.text");
newControls.Style.Add("TOP","txt2.text");
Page.Controls[1].Controls.Add(newControls);
但用这个方法定义出的控件页面一刷新就没了,而且不能加载事件。知不知道有什么方法可以让这样添加的控件一直留在页面上,并且把相关信息保存到数据库里,下次能动态的显示出来,并且要能加载事件。
if(你的条件==true)
Botton1.Visable = true;
else
Botton1.Visable = false;
建立一个保存控件属性的数据库,在page_load里面读取(并转换)保存的控件,再添加到页面上就可以了。 你做的这个东西很高级嘛
这个网站有www.asp.net
保存为html也可以吗?应该要保存为aspx的吧
newControls.ID = "textbox1";
newControls.Width = 100;
newControls.Style.Add("LEFT","txt1.text");
newControls.Style.Add("TOP","txt2.text");
Page.Controls[1].Controls.Add(newControls);
但用这个方法定义出的控件页面一刷新就没了,而且不能加载事件。知不知道有什么方法可以让这样添加的控件一直留在页面上,并且把相关信息保存到数据库里,下次能动态的显示出来,并且其中某些控件要能加载事件。当然事件应该就是一些简单按钮点击事件。
你可以把用户添加了的控件放到数组中存在session里
每次pageload时候通过判断某些条件从session中取出来
Page_Init()
{
if ( Session["collection"] == null )
{
Collection = new PlaceHolder();
}
else
Collection = (PlaceHolder) Session["collection"]; this.Controls.Add( Collection ); //然后,对Collection进行Controls.Add等操作。
}Page_Unload()
{
Session["collection"] = Collection;
}
你本来是往页面上填加控件,变成往这个容器里添加。
这个容器在Unload的时候自动存到Session里面去,保证刷新或者其他操作之后,这些控件还在。
=======>你试试把所加的物件放在"Panel"等能容乃物件的服务器控件里面,估计,通过它的viewState属性为true,能保持住控件。 还有你想添加服务器端的事件,也只有让控件放在你已知的服务器控件中,然后让事件上浮给父控件,再处理!
======>其实也好象不行,处理事件要的代码除非你早写好了。否则你无法往cs文件中添加新的代码,那不等于这个程序的运行能够修改程序本身的源代码了。
动态生成aspx文件,事件固定几个,固定cs文件处理所有的事件。
你的最终目的是什么/
因为这个项目需要处理的事情不确定,随时可能会有增加,每新增一个处理事项,就要为它设定一个处理页面,当然这些页面的处理事件无非就是信息的保存修改删除,但不可能通用一个页面,所以只能让用户自己定义页面上的文本框阿什么的.
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;namespace MZ99.Trade.Test
{
/// <summary>
/// Test25 的摘要说明。
/// </summary>
public class Test25 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.TextBox TextBox1; private PlaceHolder Collection; protected Test25()
{
Init += new EventHandler(Test25_Init);
Unload += new EventHandler(Test25_Unload);
}
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void Button1_Click(object sender, System.EventArgs e)
{
Label label = new Label();
label.ID = Images.getRandomSerial();//获得随机字符串的函数
label.Text = TextBox1.Text;
Collection.Controls.Add( label );
} private void Test25_Init(object sender, EventArgs e)
{
if ( Session["collection"] == null )
Collection = new PlaceHolder();
else
Collection = (PlaceHolder) Session["collection"]; this.Controls.Add( Collection );
} private void Test25_Unload(object sender, EventArgs e)
{
Session["collection"] = Collection;
}
}
}
服务器控件总是要到父控件注册。
请高手吧.没办法了
--------------------------------------------------------
ms的ASP.NET开发环境,我每天用的visual studio.net2003的主要功能好象就是这功能吧。楼主要使用者来写事件程序代码吗?如果是你事先写好,ms为什么不为我们写好aspx和cs.
{
if ( Session["collection"] == null )
Collection = new PlaceHolder();
else
Collection = (PlaceHolder) Session["collection"]; this.Controls.Add( Collection );
} private void Test25_Unload(object sender, EventArgs e)
{
Session["collection"] = Collection;
}
这段代码老实说我不太明白是什么意思
{
if ( Session["collection"] == null )//如果Session中没有我们的控件容器。
Collection = new PlaceHolder();//创建一个。
else
Collection = (PlaceHolder) Session["collection"];//否则将容器从Session中还原。 this.Controls.Add( Collection );//将这个控件容器添加到页面上
} private void Test25_Unload(object sender, EventArgs e)
{
Session["collection"] = Collection;//在页面显示完毕后,将我们的控件容器(以及里面所有的控件)放到Session中,以保证刷新和回发时我们动态添加的控件不会丢掉。
}
Ivony() ,非常感谢,现在添加htmlcontrol可以了,页面刷新也不会没有了,但添加webcontrol他报“类型“Button”的控件“button1”必须放在具有 runat=server 的窗体标记内。 ”========================一般来说是不推荐用WebControls,要解决这个问题也很简单: this.Controls.Add( Collection );改为:this.FindControl("form1"/*这里是那个<form runat=server>的id号*/).Controls.Add( Collection )
aspx文件不需要编译。第一次访问的时候自动编译。
大家多帮忙,今天老板就要看结果了
name 控件名
x 位置
y 位置
type 是文本框还是下拉列表框还是其他什么
width 宽度
style 样式DataSource 当控件为下拉列表等东西时,那些选项是什么。form 隶属于哪一个表单dataTable 与数据库中的哪一个表绑定
dataField 与数据库里的哪一个字段绑定。
页面处理程序从数据库中取得这些信息,并动态的向页面上添加这些控件。
动态生成SQL语句是指,页面处理程序通过分析提交的数据,动态的生成Insert、Update、Delete和Select语句。
在Page_PreRender中写入
TextBox newControls=new TextBox();
newControls.ID = "textbox1";
newControls.Width = 100;
newControls.Style.Add("LEFT","txt1.text");
newControls.Style.Add("TOP","txt2.text");
Page.Controls[1].Controls.Add(newControls);
部分
而且不能加载事件:
建立自己的事件和事件委托。
现在我需要的是加载事件的方法。
譬如说这样:
每一个输入控件对应到数据库中某个表的某个字段上。当提交表单的时候,根据对应关系自动的生成SQL语句更新数据库
或者说sql语句应该是在用户添加页面控件时动态完成的,并存入数据库,到时把它调出来就可以了,这样可行吗?这样也是可行的,在添加页面控件完成后,自动生成一个用于该页面更新的存储过程。
TextBox newControls=new TextBox();
newControls.ID = "textbox1";
newControls.Width = 100;
newControls.Style.Add("LEFT","txt1.text");
newControls.Style.Add("TOP","txt2.text");newControls.CommandName = "aa"
Page.Controls[1].Controls.Add(newControls);Public Event Change As EventHandler 重写Protected Overrides Function OnBubbleEvent(ByVal source As Object, ByVal e As System.EventArgs) As Boolean 确定类型If TypeOf e Is CommandEventArgs Then或ce.CommandName确定来源。
这是对可以CommandEventArgs。按钮类如果是changEventArgs的,文本框什么的,目前情况下不知道怎么做。
~~
数据库的处理自己能搞定吧!!道理差不多了。
加载事件前面都说过了,不再说了
<script language=C# runat=server>
int Count
{
get
{
object o = ViewState["Count"];
if (o==null)
return 0;
return (int)ViewState["Count"];
}
set
{
ViewState["Count"] = value;
}
}DropDownList AddDDL()
{
TableRow tr = new TableRow();
TableCell tc = new TableCell(); DropDownList ddl = new DropDownList();
tc.Controls.Add(ddl);
tr.Cells.Add(tc);
tbl.Rows.Add(tr); return ddl;
}void PopulateDLL(DropDownList ddl)
{
for (int i=1; i <=10; i++)
ddl.Items.Add(i.ToString());
}
void Page_Load(Object sender, EventArgs e)
{
for (int i=0; i < Count; i++)
{
AddDDL();
}
}void Add(Object sender, EventArgs e)
{
DropDownList ddl= AddDDL();
PopulateDLL(ddl);
Count++;
}</script><form runat=server id=form1>
<asp:Table id=tbl runat=server />
<asp:Button id=btn runat=server text=add Onclick="Add" />
<asp:Button id=btn2 runat=server text=submit/>
</form>
动态加载的话,就必须保证每次页面pageload的时候也必须创建该控件。我开始做动态加载用户控件也遇到了这个问题。
动态加载的话,就必须保证每次页面pageload的时候也必须创建该控件。我开始做动态加载用户控件也遇到了这个问题。
应该在Init中创建,因为在Init之后就会加载ViewState,在Page_Load中还原则丢掉了ViewState。
加事件TextBox1.OntextChanged+=new new EventHandler(你的代理函数);