PostBack上的DataBinding? [英] DataBinding on PostBack?

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

问题描述

我是ASP.NET的新手,我不习惯用这种技术处理事情的方式。我被告知,当我有一个控件时,我应该

只将数据绑定到它一次,而不是每个帖子后面(基本上

有数据)在If NOT IsPostBack Then语句中完成绑定。

为什么会这样?如何保存这些信息?


这提出了另一个问题。因为我想摆脱

spaghetti代码方法,我想在代码隐藏中以编程方式创建一些控件

,而不是aspx文件。我将在哪里创建它?
好吧,例如我想创建一个表用于

循环(用于行然后用于列),然后将一些数据从

DataTable绑定到这个动态创建的表。我是否会在Page_Load中创建表

(不是DataTable)并且只绑定一次(不是每个回发的
)?这是如何工作的。


任何建议或解释都非常感谢。非常感谢

提前。


Merci,

Krista Lemieux

I''m new to ASP.NET and I''m not use to the way things are handled with
this technology. I''ve been told that when I have a control, I should
only bind the data to it once, and not on each post back (basically
have the data binding done in the If NOT IsPostBack Then statement).
How come? How does this information get preserved?

Which raises another question. Becuase I want to get away from the
spaghetti code approach, I would like to create some of the controls
programatically in the code-behind, not the aspx file. Where would I
create that. Well for instance I want to create a table using for
loops (for rows and then for columns), and then bind some data from a
DataTable to this dynamically created table. Would I create the Table
(not the DataTable) in the Page_Load and bind it only once (not on
each postback)??? How does that work.

Any suggestions, or explanations greatly appreciated. Thank you so
much in advance.

Merci,
Krista Lemieux

推荐答案



" Krista Lemieux" <き*********** @ hotmail.com>在消息新闻中写道:bc ************************** @ posting.google.c om ...

"Krista Lemieux" <ki***********@hotmail.com> wrote in message news:bc**************************@posting.google.c om...
......这些信息如何得到保护?
ViewState!

我是否会在Page_Load中创建表格(而不是DataTable)并仅将其绑定一次(不是每次回发)?这是如何工作的。
...How does this information get preserved? ViewState!
Would I create the Table
(not the DataTable) in the Page_Load and bind it only once (not on
each postback)??? How does that work.



查看DataGrid控件,您可以轻松地将数据表中的信息绑定到它。无需创建自己的表。您最初会在页面加载时绑定它,如果每个页面都发生了变化(选择了一条记录,转到结果的下一页,排序等),您将重新绑定内容以反映新的更改。


如果你使用它就足够了,相信我

- 迈克尔


Check out the DataGrid control, you can easily bind the information in a datatable to it. No need to create your own table. You would bind this initially when the page first loads, and if something every changed in it (selected a record, went to the next page of results, sort, etc) you would rebind the contents to reflect the new changes.

It''ll make sense if you work with it enough, trust me
--Michael


谢谢快速回复Michael


您提到使用DataGrid。我正在试验它当前是
,但我很难为它添加列

以编程方式(或动态方式),所以我放弃了它。我不记得确切的问题是什么,但是我认为这是每个回发中的东西,而不是
,它们无法出现

正确或不重新添加。是否有一个特定的

的地方我应该添加动态列,所以我在回发后没有

问题。我的意思是我应该将它们添加到

初始化部分,还是Page_Load(因为我不能,或者不要
想要创建我的while循环这在aspx文件中)。


提前感谢您的帮助。


Merci,

Krista Lemieux
Thanks for the quick reply Michael

You mentioned to use DataGrid. I''m experimenting with it
currently, but I had a hard time adding columns to it
programatically (or dynamically) so I dropped it. I don''t
remember what the exact problem was, but I think it was
something on each postback they waren''t appearing
correcly or just not being re-added. Is there a specific
place where I should add the dynamic columns so I have no
problems on post back. I mean should I add them in the
Init section, or the Page_Load (since I can''t, or don''t
want to put my while loop creating this in the aspx file).

Thank You so much in advance for help.

Merci,
Krista Lemieux
-----原始信息-----

Krista Lemieux <き*********** @ hotmail.com>写在
消息

news:bc ************************** @ posting.google.c om ...
-----Original Message-----

"Krista Lemieux" <ki***********@hotmail.com> wrote in message
news:bc**************************@posting.google.c om...
...如何保留此信息?
...How does this information get preserved?


ViewState!


ViewState!

我会在Page_Load中创建表格
(不是DataTable)并且只将
绑定一次(不是每次回发)???这是如何工作的。
Would I create the Table
(not the DataTable) in the Page_Load and bind it only once (not on each postback)??? How does that work.


检查DataGrid控件,您可以轻松地将数据表中的


Check out the DataGrid control, you can easily bind the



信息绑定到它。无需创建

自己的表。你最初会在第一次加载页面时绑定它,如果每次都改变了一些内容

(选择一条记录,转到下一页的结果,
sort等)你会重新绑定内容以反映

的新变化。
如果你使用它就足够了,相信我
- Michael


information in a datatable to it. No need to create your
own table. You would bind this initially when the page
first loads, and if something every changed in it
(selected a record, went to the next page of results,
sort, etc) you would rebind the contents to reflect the
new changes.
It''ll make sense if you work with it enough, trust me
--Michael
.



嗨Krista,


以编程方式向数据网格添加列是实际上相当容易

。你看过DataGrid的AutoGenerateColumns属性吗?它将根据您使用的数据源动态创建列。
。它是一个不错的功能,但在定制或控制方面没有太大帮助。

要动态添加列,你应该做的是如下:


<伪代码>

private void Page_Load(object sender,EventArgs e)

{

if(!Page.IsPostBack){BindData(); }

}


private void BindData()

{

//这里我们将添加两列到我们的数据网格。

//假设网格的名称是dg


ButtonColumn btnCol = new ButtonColumn();

btnCol.ButtonType = ButtonColumnType.LinkBut​​ton;

btnCol.CommandName =" Select" ;;

btnCol.HeaderText =" Select" ;;

btnCol.Text =" Select Item" ;;

dg.Columns.Add(btnCol);


BoundColumn boundCol = new BoundColumn();

boundCol.DataField =" Author";

boundCol.HeaderText =" Authors";

dg.Columns.Add(boundCol) ;


dg.DataKeyField =" AuthorID" ;;

dg.DataSource = authorsDataTable;

dg.DataBind();

}

< / pseudo-code>


现在,这只会在第一页加载时触发,因为

" if(!Page.IsPostBack)"。你现在要做的是连接数据网格事件

,以便你可以与网格进行交互。例如,要捕获链接

按钮之一的单击,您可以使用数据网格中的两个事件之一。

ItemCommand事件或SelectedIndexChanged事件。所有你需要做的事情

确保页面中任何会触发回发的内容(

按钮或下拉列表更改)都会调用BindData()重建方法

并重新绑定数据网格。现在,还有一些其他地方你可以把b $ b放在列创建代码中。在检查回发之前,您可以将它放在Page_Load事件

处理程序中。你也可以把它放在

重写的OnInit方法中。


就像迈克尔说的那样,给定时间,你会立刻拿起数据网格。它是
是.NET

框架中最灵活(也是最复杂)的控件之一,但它还是有回报的。 :)


HTH,

Kyril


" Krista Lemieux" < KR *********** @ hotmail.com>在留言中写道

新闻:2c ***************************** @ phx.gbl ...
Hi Krista,

Adding columns to the datagrid programmatically is actually pretty easy to
do. Have you looked at the DataGrid''s AutoGenerateColumns property? It will
dynamically create columns based on the data source that you use. It''s a
nice feature, but does not give much in terms of customization or control.
To dynamically add columns, what you should do is something like below:

<pseudo-code>
private void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack) { BindData(); }
}

private void BindData()
{
//here we will add two columns to our data grid.
//assume the name of the grid is dg

ButtonColumn btnCol = new ButtonColumn();
btnCol.ButtonType = ButtonColumnType.LinkButton;
btnCol.CommandName = "Select";
btnCol.HeaderText = "Select";
btnCol.Text = "Select Item";
dg.Columns.Add(btnCol);

BoundColumn boundCol = new BoundColumn();
boundCol.DataField = "Author";
boundCol.HeaderText = "Authors";
dg.Columns.Add(boundCol);

dg.DataKeyField = "AuthorID";
dg.DataSource = authorsDataTable;
dg.DataBind();
}
</pseudo-code>

Now, this will fire only on the first page load because of the
"if(!Page.IsPostBack)". What you will do now is hook up the datagrid events
so that you can interact with the grid. To trap the click of one of the link
buttons for instance, you can use one of two events in the data grid. Either
the ItemCommand event or the SelectedIndexChanged event. All you have to do
is make sure that anything in your page that will trigger a post back (a
button or a drop down list changing) calls the BindData() method to rebuild
and rebind the data grid. Now, there are a couple of other places that you
can put the column creation code. You can put it in the Page_Load event
handler, before the check for a postback. You can also place it in the
overridden OnInit method.

Like Michael said, given time, you''ll pick up the data grid in no time. It
is one of the most flexible (and most complicated) controls in the .NET
Framework, but it pays off. :)

HTH,

Kyril

"Krista Lemieux" <kr***********@hotmail.com> wrote in message
news:2c*****************************@phx.gbl...
感谢您快速回复Michael

您提到过使用DataGrid。我正在试验它
目前,但我很难以编程方式(或动态地)添加列,所以我放弃了它。我不记得确切的问题是什么,但我认为这是每个回发的东西,他们没有出现
正确或只是没有被重新添加。是否有一个特定的地方我应该添加动态列,所以我在回发时没有问题。我的意思是我应该在
Init部分或Page_Load中添加它们(因为我不能,或者不希望我的while循环在aspx文件中创建它)。 br />
非常感谢你提前寻求帮助。

Merci,
Krista Lemieux
Thanks for the quick reply Michael

You mentioned to use DataGrid. I''m experimenting with it
currently, but I had a hard time adding columns to it
programatically (or dynamically) so I dropped it. I don''t
remember what the exact problem was, but I think it was
something on each postback they waren''t appearing
correcly or just not being re-added. Is there a specific
place where I should add the dynamic columns so I have no
problems on post back. I mean should I add them in the
Init section, or the Page_Load (since I can''t, or don''t
want to put my while loop creating this in the aspx file).

Thank You so much in advance for help.

Merci,
Krista Lemieux
-----原创消息-----

Krista Lemieux <き*********** @ hotmail.com>写在
-----Original Message-----

"Krista Lemieux" <ki***********@hotmail.com> wrote in


消息
新闻:bc ************************** @ posting.google.c om ...


message
news:bc**************************@posting.google.c om...

...如何保留此信息?
...How does this information get preserved?


ViewState!


ViewState!

我会在Page_Load中创建表格(而不是DataTable)并仅将其绑定一次(不是每次回发)???这是如何工作的。
Would I create the Table
(not the DataTable) in the Page_Load and bind it only once (not on each postback)??? How does that work.


检查DataGrid控件,您可以轻松地将数据表中的


Check out the DataGrid control, you can easily bind the


信息绑定到它。无需创建自己的表格。当页面首次加载时你最初会绑定它,如果每个页面都发生了变化
(选择了一条记录,转到下一页的结果,
排序等)你会重新绑定内容反映了新的变化。


information in a datatable to it. No need to create your
own table. You would bind this initially when the page
first loads, and if something every changed in it
(selected a record, went to the next page of results,
sort, etc) you would rebind the contents to reflect the
new changes.


如果你使用它就足够了,相信我
- 迈克尔

It''ll make sense if you work with it enough, trust me
--Michael
.



这篇关于PostBack上的DataBinding?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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