动态修改表 [英] Dynamically modifying Table

查看:54
本文介绍了动态修改表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我是asp.net的新手。我想动态创建一个表。假设有两个按钮''添加行'','删除行''调用addRow,deleteRow事件处理程序。下面给出了一个示例代码:


这里的问题是当页面回发时,调用pageLoad并且页面没有更新的表信息。所以addRow不起作用(row.count总是返回0)。


如何解决这个问题?我读到ControlState可用于在回发中保存状态。我可以使用ControlState吗?如果是的话,请你给我一个示例代码。


protected void addRow(object sender,EventArgs e)

{

int count2 = table2.Rows.Count;

TableRow r = new TableRow();

TableCell c = new TableCell();


CheckBox ck = new CheckBox();

c.Controls.Add(ck);

r.Cells.Add(c);

int count = table2.Rows.Count;

table2.Rows.Add(r);

count = table2.Rows.Count;

//抛出新的异常(方法或操作未实现。);

}


protected void Page_Load(object发件人,EventArgs e)

{


}


谢谢和问候

Roopesh

Hi

I am a newbee in asp.net. I want to create a table dynamically. Say there are two buttons ''add row'', ''delete row'' which calls addRow, deleteRow event handlers. A sample code is given below :

Problem here is when the page is postback, pageLoad is called and the Page doesnt have the updated table information. So addRow doesn''t work (row.count will always return 0).

How to solve this problem? I read that ControlState can be used to save states across postbacks. Can I use ControlState? If so, can you please give me an example code.

protected void addRow(object sender, EventArgs e)
{
int count2 = table2.Rows.Count;
TableRow r = new TableRow();
TableCell c = new TableCell();

CheckBox ck = new CheckBox();
c.Controls.Add(ck);
r.Cells.Add(c);
int count = table2.Rows.Count;
table2.Rows.Add(r);
count = table2.Rows.Count;
//throw new Exception("The method or operation is not implemented.");
}

protected void Page_Load(object sender, EventArgs e)
{

}

Thanks and Regards
Roopesh

推荐答案



我已经修改了一点代码。现在我将修改后的表对象存储在会话中。当每个回发发生时,表对象从会话对象更新。


现在问题是在第一次添加之后,进一步的添加不可见,它会删除最初添加的复选框(行)。


这是一个首选的方法?


protected void addRow(object sender,EventArgs e)

{

int count2 = table2.Rows.Count;

TableRow r = new TableRow();

TableCell c = new TableCell();


CheckBox ck = new CheckBox();

c.Controls.Add(ck);

r.Cells.Add(c);

int count = table2.Rows.Count ;

table2.Rows.Add(r);

count = table2.Rows.Count;


Session [" table2"] = table2;

}


protected void Page_Load(object sender,EventArgs e)

{

if(IsPostBack)

{

Table table =(Table)Session [" table2"];


if(table!= null){

table2 = table;

}

}

}


(我需要克隆表?)。


谢谢和问候

Roopesh
Hi
I have modified the code a little bit. Now I store the modified table object in a session. When each postback happens the table object is updated from the session object.

Now the problem is after the first addition further additions are not visible and it erases the checkbox (row) added initially.

Is this a prefered approach?

protected void addRow(object sender, EventArgs e)
{
int count2 = table2.Rows.Count;
TableRow r = new TableRow();
TableCell c = new TableCell();

CheckBox ck = new CheckBox();
c.Controls.Add(ck);
r.Cells.Add(c);
int count = table2.Rows.Count;
table2.Rows.Add(r);
count = table2.Rows.Count;

Session["table2"] = table2;
}

protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
Table table = (Table)Session["table2"];

if(table!=null){
table2 = table;
}
}
}

(Do I need to clone the table?).

Thanks and regards
Roopesh





您必须在Page的load事件上执行表创建。因为如果您没有在页面上执行表创建或任何添加控件,则下次加载页面时不会保留,同样的事情发生在您身上。


请随意询问您是否有任何问题或是否真的需要代码。
Hi,

You have to perform the table creation on the load event of the Page. Because if you did''t perform the table creation or any addition of controls on the page, it will not remain next time you load the page, the same thing is happening to you .

Please fell free to ask if you have any Problem or if you really need a code.



您好,


您必须在Page的load事件上执行表创建。因为如果您没有在页面上执行表创建或任何添加控件,则下次加载页面时不会保留,同样的事情发生在您身上。


请随意询问您是否有任何问题或者您是否真的需要代码。
Hi,

You have to perform the table creation on the load event of the Page. Because if you did''t perform the table creation or any addition of controls on the page, it will not remain next time you load the page, the same thing is happening to you .

Please fell free to ask if you have any Problem or if you really need a code.



但是如果我们需要动态修改表格,比如在表格中添加一行,那该怎么办?要添加的代码将出现在某个事件处理程序中,而不是在page_load事件中吗?这是我陷入困境的地方。


我每次发生页面加载时都尝试从数据库生成表。所以,从逻辑上看,我认为该表应始终具有最近的状态。但它没有发生。


你能告诉我为什么会这样吗?任何能帮助我理解的代码都会非常有用。


谢谢和问候

Roopesh

But what if we need to dynamically modify the table like adding a row to the table. The code to add will occur in the some event handler not in the page_load event right? This is where I am getting stuck.

I have also tried generating the table from the database each time when the page load happens. So, what appears to me logically is that the table should always have the recent state. But it is not happening.

Can you please tell me why this is happening. Any piece of code that will help me understand will be very helpful.

Thanks and Regards
Roopesh


这篇关于动态修改表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆