"动态装载该控件"?
在LoadControl之后加上:
base.OnInit(e);
在LoadControl之后加上:
base.OnInit(e);
解决方案 »
- 请教:IIS browse helloworld.aspx 包错cs1528
- 数据库相关问题
- 急急急 如何把asp.net2.0的登陆控件的默认数据库 自动生成ACCESS数据库
- 在asp.net中对字段加密后,在asp中能否使用?
- 请教如何在DataTable中添加新行到ACCESS数据表中。
- 超难的用正则表达式提取文本内容
- 寻求asp.net组件开发,c#组件开发的书籍,能免费下载的。谢谢
- 数据库能读 不能写
- 我想实现在一个框架里按一个按钮刷新整个框架,有办法吗?UP有分,謝謝
- 急、急、急!!分页问题!!!
- 请教:为什么动态创建的web控件创建后无法找到?一定给分
- 请问.net中可否调用oracle Report开发的报表?
Label lbl;2. 在Button1_Click中加入:
lbl = new Label();
this.Controls[1].Controls.Add(lbl);
lbl.Text = "lbl";3. 在Button2_Click中加入:
lbl = new Label(); //重写装载控件
this.Controls[1].Controls.Add(lbl);
Response.Write("From Button2_Click, Label.Text=" + lbl.Text);最后的输出结果:Label.Text=lbl
但是有人说:loadviewstate时它会将viewstate中的数据绑定到各个控件中,如果此时找不到对应的控件,它会忽略。而在所有的控件都加载完毕后,对应于动态加载的控件的viewstate可以正确应用到各个控件上去。当然,这里的前提是每次动态加载的控件在viewstate中的index 是一致的。事实上,我近这种方法去做也成功了。只是有label和textbox控件的区别。
<%@ Page language="c#" Codebehind="Test.aspx.cs" AutoEventWireup="false" Inherits="Kennel.Customer.Test" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Test</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Test" method="post" runat="server">
<P>
<asp:Button id="Button1" runat="server" Text="load"></asp:Button></P>
<P>
<asp:Button id="Button3" runat="server" Text="post back"></asp:Button></P>
<P>
<asp:PlaceHolder id="PlaceHolder1" runat="server"></asp:PlaceHolder></P>
<P> </P>
</form>
</body>
</HTML>Text.aspx.cs:
using System;
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 Kennel.Customer
{
/// <summary>
/// Summary description for Test.
/// </summary>
public class Test : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.PlaceHolder PlaceHolder1;
protected System.Web.UI.WebControls.Button Button3;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here if (ViewState["t1_loaded"] != null)
{
T1 t1 = (T1)Page.LoadControl("t1.ascx");
PlaceHolder1.Controls.Add(t1); }
} #region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </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)
{
T1 t1 = (T1)Page.LoadControl("t1.ascx");
t1.GroupID = 1234;
t1.TextGroupID = 1234; PlaceHolder1.Controls.Add(t1); ViewState["t1_loaded"] = "true";
}
}
}
T1.ascx:
<%@ Control Language="c#" AutoEventWireup="false" Codebehind="T1.ascx.cs" Inherits="Kennel.Customer.T1" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<P>label groupid:
<asp:Label id="lblGroupID" runat="server"></asp:Label></P>
<P>textbox groupid:
<asp:TextBox id="txtGroupID" runat="server"></asp:TextBox></P>
T1.ascx.cs:namespace Kennel.Customer
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls; /// <summary>
/// Summary description for T1.
/// </summary>
public abstract class T1 : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.TextBox txtGroupID;
protected System.Web.UI.WebControls.Label lblGroupID; private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
} public int GroupID
{
get
{
try
{
return System.Convert.ToInt32(lblGroupID.Text);
}
catch { return 0;}
}
set
{
lblGroupID.Text = value.ToString();
}
} public int TextGroupID
{
get
{
try
{
return System.Convert.ToInt32(txtGroupID.Text);
}
catch { return 0;}
}
set
{
txtGroupID.Text = value.ToString();
}
} #region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load); }
#endregion
}
}编译运行后你会发现 TextBox的值保留了,而label的丢失了。
用 disabled 的不行,因为 disabled的 textbox是不会保存视图状态的。
我现在用的是 readonly 的textbox. 页面看起来有点怪,但程序已能正常工作。在这里提出来大家探讨的目的是想弄清楚为什么 textbox 和 label之间有这种不同。
Textbox之所以能保存状态儿Label不能
是因为这个不关 ViewState的事
而是与 form提交有关Label生成的html码是
<span id="_ctl0_lblGroupID">1234</span>TextBox生成的html码是
<input name="_ctl0:txtGroupID" type="text" value="1234" id="_ctl0_txtGroupID" />很明显 TextBox 的value会提交 而Label的值不会提交
如果你在pageload里动态添加textbox 她生成的ID刚好还是前一个
_ctl0:txtGroupID 鬼死神差她就接受了这个value
Label发送到客户端:<span id="Label1">Label</span>
TextBox发送到客户端:<input name="TextBox1" type="text" id="TextBox1"/>
TextBox仍在Form里,具有name属性,在客户端js可以用WebForm1.TextBox1.value赋值
而Label为具有id的span,只能用Label1.innerText赋值
会不会是这种差异导致了你的情况
具体原因我也说不出来(html机制原理不过关,见笑了),只能指出这些差异:)
_ctl0:txtGroupID动态生成的代码的ID就一个地方改变
就是_ctl后面的那个自动增加的整数to : spring_ok
现在的代码能用
但最好还是小心点,毕竟这个ID不是由你控制的
不要一不小心被别的控件使用了
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 Kennel.Customer
{
/// <summary>
/// Summary description for Test.
/// </summary>
public class Test : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.PlaceHolder PlaceHolder1;
protected System.Web.UI.WebControls.Button Button3;
private void Page_Load(object sender, System.EventArgs e)
{
T1 t1 = (T1) PlaceHolder1.FindControl("t111");
if (ViewState["t1_loaded"] == null)
{
t1.Visible = false;
}
} private void Page_Init(object sender, System.EventArgs e)
{
T1 t1 = (T1)Page.LoadControl("t1.ascx");
t1.ID = "t111";
PlaceHolder1.Controls.Add(t1);
}
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
this.Init += new System.EventHandler(this.Page_Init);
} private void Button1_Click(object sender, System.EventArgs e)
{
T1 t1 = (T1) PlaceHolder1.FindControl("t111");
t1.GroupID = 1234;
t1.TextGroupID = 2345;
t1.Visible = true;
ViewState["t1_loaded"] = "true";
}
}
}
这个时候你的label没有建立实例所以ViewState也就没有负上
如果能把
T1 t1 = (T1)Page.LoadControl("t1.ascx");
PlaceHolder1.Controls.Add(t1);
这个东西放到loadView 之前的话,应该就没有问题
到底是那个事件,我就不知道了
嗯
saucer是不是就是这个意思?
不要用就隐藏
这还不如在设计器里面把它添上,不用的就隐藏
(而是与 form提交有关)
我关掉 TextBox的ViewState效果一样
TextBox 还是能保留它的值