MSDN示例存在缺陷 [英] MSDN Examples flawed
问题描述
大家好,
我认为互联网上的大多数示例存在固有问题
(MSDN也是如此!)显示数据网格''数据绑定。这是,他们看起来如何
喜欢:
====================== ==
在Page_Load
if!IsPostBack:BindGrid()
On_Edit
设置数据网格's EditItem点击项目
BindGrid
void BindGrid()
通过dataadapter填充DataSet
DataGrid.DataBind()
========================
想象一个简单的表格名字有两列:NameID和Name
你的SQL Select语句读取SELECT NameID,Name FROM Names ORDER BY
Name"并且你有两条记录:
ID = 1,Name =" aaaa"
ID = 2,Name =" cccc"
因此,当您第一次调用该页面时,您将获得以下网格:
1" aaaa"编辑按钮
2" cccc"编辑按钮
在您在IE浏览器上查看此html页面的时候
在表格中插入另一条记录:" ID = 3 ,Name =''bbbb''"。
然后单击第二行的编辑按钮进入你的网格(2
" cccc")打算编辑名称cccc。当您点击编辑按钮时,
但是,您会得到以下网格:
1" aaaa"编辑按钮
3 [bbbb] - 可更新!!!更新按钮,取消按钮
2" cccc"编辑按钮
我相信,你了解原因是什么。什么是防止这个问题的最佳解决方案
?
感谢您抽出宝贵时间了解我的意思。我想到了一个
逻辑解决方案然而却不起作用。如果发生讨论,我会写b $ b.b $
一切都好
Stamen >
Stamen,
通常在进行这样的操作时你不会重新绑定网格>
编辑操作之前。
所以,在页面加载子使用:
如果不是IsPostBack然后
''---在这里绑定你的网格
结束如果
(确保为网格打开了viewstate。这种方式你没有
必须重新绑定网格。)
然后在编辑数据后重新绑定网格。
此致,
-
S. Justin Gengo,MCP
Web开发人员
免费代码库:
www.aboutfortunate.com
出于混乱而来的订单。
Nietzche
&qu ot; Stamen Gortchev < S ******** @ onventis.de>。在消息中写道
news:uu ************** @ TK2MSFTNGP10.phx.gbl ...大家好,
我认为互联网上的大多数例子存在固有的问题
(MSDN也是如此!)显示了datagrid的数据绑定。这是,他们看起来如何:
========================
在Page_Load
if!IsPostBack:BindGrid()
On_Edit
将datagrid的EditItem设置为单击的项目
BindGrid
void BindGrid()
通过dataadapter填充DataSet
DataGrid.DataBind()
========================
<想象一个简单的表格名字有两列:NameID和Name
您的SQL Select语句读取SELECT NameID,Name FROM Names ORDER BY
Name并且你有两条记录:
ID = 1,Name =" aaaa"
ID = 2,Name =" cccc"
所以,你第一次调用页面,你得到以下网格:
1" aaaa"编辑按钮
2" cccc"编辑按钮
在您查看IE浏览器上的这个html页面时,有人会在表格中插入另一条记录:ID = 3,Name =''bbbb''然后单击第二行的编辑按钮进入你的网格(2
cccc),打算编辑名称cccc。当您点击编辑按钮时,
,您将获得以下网格:
1" aaaa"编辑按钮
3 [bbbb] - 可更新!!!更新按钮,取消按钮
2" cccc"编辑按钮
我相信,你了解原因是什么。什么是防范这个问题的最佳解决方案?
感谢您抽出宝贵时间了解我的意思。我认为
是一个逻辑解决方案然而却不起作用。如果发生讨论,
我会写一下。
最好的
Stamen
Hallo Justin,
感谢您的回复。你提出的建议正是我正在做的事情:
下面的代码不能免受数据库更改的影响:
private void Page_Load(object sender,System。 EventArgs e)
{
if(!IsPostBack)
{
this.da.Fill(ds1, 国家);
this.DataGrid1.DataBind();
}
}
private void DataGrid1_EditCommand(对象来源,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
this.da.Fill (ds1," Names");
DataGrid1.EditItemIndex = e.Item.ItemIndex;
DataGrid1.DataBind();
}
你怎么看?
谢谢
Stamen
" ; S。 Justin Gengo < SJ ***** @ aboutfortunate.com> schrieb im Newsbeitrag
新闻:eN ************** @ TK2MSFTNGP10.phx.gbl ...Stamen,>
通常在进行这样的操作时,你不会在编辑操作之前重新绑定网格。
所以,在页面加载子工具中使用:
如果不是IsPostBack然后
''---在这里绑定你的网格
结束如果
(确保为网格打开了viewstate。这样你不要重新绑定网格。)
然后在编辑数据后重新绑定网格。
真诚的,
> -
S. Justin Gengo,MCP
Web开发人员
免费代码库位于:
www.aboutfortunate.com
出于混乱而来的订单。
Nietzche
Stamen Gortchev < S ******** @ onventis.de>。在消息中写道
新闻:uu ************** @ TK2MSFTNGP10.phx.gbl ...大家好,
我认为互联网上的大多数例子存在固有的问题
(MSDN也是如此!)显示了datagrid的数据绑定。这是,他们看起来如何:
========================
在Page_Load
if!IsPostBack:BindGrid()
On_Edit
将datagrid的EditItem设置为单击的项目
BindGrid
void BindGrid()
通过dataadapter填充DataSet
DataGrid.DataBind()
========================
<想象一个简单的表格名字有两列:NameID和Name
您的SQL Select语句读取SELECT NameID,Name FROM Names ORDER BY
Name并且你有两条记录:
ID = 1,Name =" aaaa"
ID = 2,Name =" cccc"
所以,你第一次调用页面,你得到以下网格:
1" aaaa"编辑按钮
2" cccc"编辑按钮
在您在IE
上查看此html页面的时候,有人在表格中插入了另一条记录:ID = 3,Name =''bbbb''然后单击第二行的编辑按钮进入你的网格(2
cccc),打算编辑名称cccc。但是,当你点击编辑
按钮时,你会得到以下网格:
1" aaaa"编辑按钮
3 [bbbb] - 可更新!!!更新按钮,取消按钮
2" cccc"编辑按钮
我相信,你了解原因是什么。
防范此问题的最佳解决方案是什么?
感谢您抽出宝贵时间了解我的意思。我认为
的逻辑解决方案然而不起作用。如果发生讨论
,我会写下它。
最好的
雄蕊>
" Stamen Gortchev" < S ******** @ onventis.de>。在消息中写道
news:uu ************** @ TK2MSFTNGP10.phx.gbl ...大家好,
我认为互联网上的大多数例子存在固有的问题
(MSDN也是如此!)显示了datagrid的数据绑定。这是,他们看起来如何:
您能否发表一篇MSDN文章的参考文献,其中说您应该
在编辑之前从数据库重新加载数据?
-
John Saunders
互联网工程师
jo *********** @ surfcontrol.com
>
Hi all,
I think there is an inherent problem with most examples on the internet
(with MSDN, too!) showing datagrid''s databinding. Here is, how they look
like:
========================
On Page_Load
if !IsPostBack: BindGrid()
On_Edit
Set datagrid''s EditItem to clicked item
BindGrid
void BindGrid()
Fill DataSet through dataadapter
DataGrid.DataBind()
========================
Imagine a simple table "Names" with two columns: NameID and Name
Your SQL Select Statement reads "SELECT NameID, Name FROM Names ORDER BY
Name" and you have two records in it:
ID=1, Name = "aaaa"
ID=2, Name = "cccc"
So, the first time you call the page, you get following grid:
1 "aaaa" edit-button
2 "cccc" edit-button
during the time in which you are viewing this html page on your IE someone
inserts another record in the table: "ID=3, Name=''bbbb''".
Then you click the edit button of the second row into your grid (2
"cccc") intending to edit the name "cccc". When you klick the edit button,
however, you get following grid:
1 "aaaa" edit-button
3 [bbbb] - updatable!!! Update-button, cancel-button
2 "cccc" edit-button
I am sure, you ghet what the reason is. What is the best solution to guard
against this problem?
Thank you for taking your time and understanding what I mean. I thought of a
logical solution which however does not work. In case a discussion occur, I
will write about it.
All best
Stamen
Stamen,
Typically when doing an operation like this you wouldn''t rebind the grid
before the edit operation.
So, in the page load sub use:
If Not IsPostBack Then
''---Bind your grid here
End If
(Make certain that viewstate is turned on for the grid. This way you don''t
have to rebind the grid.)
Then rebind the grid after you edit the data.
Sincerely,
--
S. Justin Gengo, MCP
Web Developer
Free code library at:
www.aboutfortunate.com
"Out of chaos comes order."
Nietzche
"Stamen Gortchev" <s.********@onventis.de> wrote in message
news:uu**************@TK2MSFTNGP10.phx.gbl...Hi all,
I think there is an inherent problem with most examples on the internet
(with MSDN, too!) showing datagrid''s databinding. Here is, how they look
like:
========================
On Page_Load
if !IsPostBack: BindGrid()
On_Edit
Set datagrid''s EditItem to clicked item
BindGrid
void BindGrid()
Fill DataSet through dataadapter
DataGrid.DataBind()
========================
Imagine a simple table "Names" with two columns: NameID and Name
Your SQL Select Statement reads "SELECT NameID, Name FROM Names ORDER BY
Name" and you have two records in it:
ID=1, Name = "aaaa"
ID=2, Name = "cccc"
So, the first time you call the page, you get following grid:
1 "aaaa" edit-button
2 "cccc" edit-button
during the time in which you are viewing this html page on your IE someone
inserts another record in the table: "ID=3, Name=''bbbb''".
Then you click the edit button of the second row into your grid (2
"cccc") intending to edit the name "cccc". When you klick the edit button,
however, you get following grid:
1 "aaaa" edit-button
3 [bbbb] - updatable!!! Update-button, cancel-button
2 "cccc" edit-button
I am sure, you ghet what the reason is. What is the best solution to guard
against this problem?
Thank you for taking your time and understanding what I mean. I thought of a logical solution which however does not work. In case a discussion occur, I will write about it.
All best
Stamen
Hallo Justin,
thanks for replying. What you propose however is exactly what I am doing:
The code below is not immune against database changes:
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
this.da.Fill(ds1, "Countrys");
this.DataGrid1.DataBind();
}
}
private void DataGrid1_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
this.da.Fill(ds1, "Names");
DataGrid1.EditItemIndex = e.Item.ItemIndex;
DataGrid1.DataBind();
}
What do you think about it?
Thanks
Stamen
"S. Justin Gengo" <sj*****@aboutfortunate.com> schrieb im Newsbeitrag
news:eN**************@TK2MSFTNGP10.phx.gbl...Stamen,
Typically when doing an operation like this you wouldn''t rebind the grid
before the edit operation.
So, in the page load sub use:
If Not IsPostBack Then
''---Bind your grid here
End If
(Make certain that viewstate is turned on for the grid. This way you don''t
have to rebind the grid.)
Then rebind the grid after you edit the data.
Sincerely,
--
S. Justin Gengo, MCP
Web Developer
Free code library at:
www.aboutfortunate.com
"Out of chaos comes order."
Nietzche
"Stamen Gortchev" <s.********@onventis.de> wrote in message
news:uu**************@TK2MSFTNGP10.phx.gbl...Hi all,
I think there is an inherent problem with most examples on the internet
(with MSDN, too!) showing datagrid''s databinding. Here is, how they look
like:
========================
On Page_Load
if !IsPostBack: BindGrid()
On_Edit
Set datagrid''s EditItem to clicked item
BindGrid
void BindGrid()
Fill DataSet through dataadapter
DataGrid.DataBind()
========================
Imagine a simple table "Names" with two columns: NameID and Name
Your SQL Select Statement reads "SELECT NameID, Name FROM Names ORDER BY
Name" and you have two records in it:
ID=1, Name = "aaaa"
ID=2, Name = "cccc"
So, the first time you call the page, you get following grid:
1 "aaaa" edit-button
2 "cccc" edit-button
during the time in which you are viewing this html page on your IE someone inserts another record in the table: "ID=3, Name=''bbbb''".
Then you click the edit button of the second row into your grid (2
"cccc") intending to edit the name "cccc". When you klick the edit button, however, you get following grid:
1 "aaaa" edit-button
3 [bbbb] - updatable!!! Update-button, cancel-button
2 "cccc" edit-button
I am sure, you ghet what the reason is. What is the best solution to guard against this problem?
Thank you for taking your time and understanding what I mean. I thoughtof a
logical solution which however does not work. In case a discussionoccur, I
will write about it.
All best
Stamen
"Stamen Gortchev" <s.********@onventis.de> wrote in message
news:uu**************@TK2MSFTNGP10.phx.gbl...Hi all,
I think there is an inherent problem with most examples on the internet
(with MSDN, too!) showing datagrid''s databinding. Here is, how they look
like:
Could you please post a reference to an MSDN article which says you should
reload the data from the database before the edit?
--
John Saunders
Internet Engineer
jo***********@surfcontrol.com
这篇关于MSDN示例存在缺陷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!