1. using keyword "Array" as a variable name is a very bad style2. you should not use Render to create controls through text output, you should override CreateChildControls() protected override void CreateChildControls() { Table t = new Table(); for (......) { TableRow tr = new TableRow(); TableCell tc = new TableCell(); TextBox tb = new TextBox(); //set its properties tc.Controls.Add(tb); tr.Cells.Add(tc); t.Rows.Add(tr); } this.Controls.Add((t)); }3. see some discussions here:http://expert.csdn.net/Expert/topic/1204/1204836.xml?temp=.559643 http://expert.csdn.net/Expert/topic/1204/1204125.xml?temp=.373913
我修改成这样,可是不行,postback后 值没有改变 我想应该是SaveViewState和LoadViewState的问题 当我把他们去掉的时候 提示 [InvalidCastException: 指定的转换无效。] System.Web.UI.WebControls.WebControl.LoadViewState(Object savedState) +24 System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +125 System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +311 System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +311 System.Web.UI.Page.LoadPageViewState() +182 System.Web.UI.Page.Proce 下面是代码 using System; using System.Web.UI; using System.Web.UI.WebControls; using System.ComponentModel; using System.Text; using System.Collections.Specialized;namespace WebControl1 { public class Test : System.Web.UI.WebControls.WebControl,IPostBackDataHandler { public string[] ArrayValue { get { return (String[])ViewState["ArrayValue"]; } set { ViewState["ArrayValue"]=value; } } protected override void CreateChildControls() { if(ArrayValue!=null) { Table t = new Table(); for(int i=0;i<ArrayValue.Length;i++) { TableRow tr = new TableRow(); TableCell tc = new TableCell(); TextBox tb = new TextBox(); tb.ID = this.UniqueID + ":" + i; tb.Text = ArrayValue[i] ; tc.Controls.Add(tb); tr.Cells.Add(tc); t.Rows.Add(tr); } this.Controls.Add((t)); } } protected override object SaveViewState() { return (object)ArrayValue; } protected override void LoadViewState(object state) { ArrayValue=(string[])state; } public bool LoadPostData(string postDataKey,NameValueCollection postData) { string[] ArrayTemp=new string[ArrayValue.Length]; for(int i=0;i<ArrayValue.Length;i++) { ArrayTemp[i]=(string)postData[this.UniqueID + ":" + i]; }
protected override void CreateChildControls()
{
Table t = new Table();
for (......)
{
TableRow tr = new TableRow();
TableCell tc = new TableCell(); TextBox tb = new TextBox();
//set its properties
tc.Controls.Add(tb); tr.Cells.Add(tc);
t.Rows.Add(tr); }
this.Controls.Add((t));
}3. see some discussions here:http://expert.csdn.net/Expert/topic/1204/1204836.xml?temp=.559643
http://expert.csdn.net/Expert/topic/1204/1204125.xml?temp=.373913
我想应该是SaveViewState和LoadViewState的问题
当我把他们去掉的时候
提示
[InvalidCastException: 指定的转换无效。]
System.Web.UI.WebControls.WebControl.LoadViewState(Object savedState) +24
System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +125
System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +311
System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +311
System.Web.UI.Page.LoadPageViewState() +182
System.Web.UI.Page.Proce
下面是代码
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Text;
using System.Collections.Specialized;namespace WebControl1
{
public class Test : System.Web.UI.WebControls.WebControl,IPostBackDataHandler
{
public string[] ArrayValue
{
get
{
return (String[])ViewState["ArrayValue"];
} set
{
ViewState["ArrayValue"]=value;
}
}
protected override void CreateChildControls()
{
if(ArrayValue!=null)
{
Table t = new Table();
for(int i=0;i<ArrayValue.Length;i++)
{
TableRow tr = new TableRow();
TableCell tc = new TableCell();
TextBox tb = new TextBox();
tb.ID = this.UniqueID + ":" + i;
tb.Text = ArrayValue[i] ;
tc.Controls.Add(tb);
tr.Cells.Add(tc);
t.Rows.Add(tr);
}
this.Controls.Add((t));
}
} protected override object SaveViewState()
{
return (object)ArrayValue;
} protected override void LoadViewState(object state)
{
ArrayValue=(string[])state;
} public bool LoadPostData(string postDataKey,NameValueCollection postData)
{
string[] ArrayTemp=new string[ArrayValue.Length]; for(int i=0;i<ArrayValue.Length;i++)
{
ArrayTemp[i]=(string)postData[this.UniqueID + ":" + i]; }
ArrayValue=ArrayTemp;
return false; } public void RaisePostDataChangedEvent()
{ }
}
}
调用代码
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
{
string[] a=new string[2];
a[0]="a";
a[1]="b";
Test1.ArrayValue=a;
}
}
请问是怎么回事?
tb.ID = this.UniqueID + ":" + i; 不应该这样写,应该是 tb.ID = this.UniqueID;
谢了