关于删除方法问题的GridView [英] GridView on Delete Method problem

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

问题描述

您好,


我正在使用ASP.NET 2.0和SQL Server 2005与VS 2005 Pro。


我有一个价格页面(我的网站需要登录)与GridView的

以下栏目


PriceID,金额,已批准,CrtdUser和日期

和编辑和删除按钮


我创建了一个函数来检索当前用户


受保护的函数GetUserName()As String

返回User.Identity.Name

结束函数


在SQLDatasource1上我添加了


受保护的Sub SqlDataSource1_Updating(ByVal sender As Object,ByVal e As

System.Web.UI.WebControls.SqlDataSourceCommandEven tArgs)句柄

SqlDataSource1.Updating

e.Command.Parameters(" @ CrtdUser")。Value = GetUserName()

End Sub

我将CrtdUser转换为模板并更改了字段绑定

从crtdUser到GetUserName()函数


另外我创建了一个触发更新,基本上将更新的记录

插入到Inserted = NEW的日志表中并删除=旧


一切正常


我还创建了一个删除触发器,看起来像这样

CREATE TRIGGER [dbo]。[tr_Price_Delete]

ON [dbo] 。[价格]

删除后

AS

BEGIN


设置NOCOUNT ON;

插入dbo.PriceArchive

选择

''新'',''D'',

Price_ID,金额,Store_ID,BSP,ALC_ID,GN_ID,已批准,CrtdUser,GetDate()

已删除


结束

因为用户删除时每次删除删除触发器

记录用创建

记录的原始用户名填充的日志表


我还使用GetUserName()更改了GridView上的标签,所以现在它只显示当前用户名(仅覆盖实际记录)

但是它不是用现在的用户名来填充表格

crtdUser显示新值的旧值。


如何我是否使用此GridView

控件的删除方法检索当前用户?


谢谢,

Ed Dror

Hi there,

I''m using ASP.NET 2.0 and SQL Server 2005 with VS 2005 Pro.

I have a Price page (my website require login) with GridView with the
following columns

PriceID, Amount, Approved, CrtdUser and Date
And Edit and Delete buttons

I created a function to retrive the current user

Protected Function GetUserName() As String
Return User.Identity.Name
End Function

And on SQLDatasource1 I added

Protected Sub SqlDataSource1_Updating(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.SqlDataSourceCommandEven tArgs) Handles
SqlDataSource1.Updating
e.Command.Parameters("@CrtdUser").Value = GetUserName()
End Sub
I converted the CrtdUser into a template and changed the Field binding
from crtdUser to GetUserName() function

Also I created a Trigger for Update which basically insert updated records
into a log table from Inserted = NEW and deleted = Old

Everything works fine

I also created a trigger for Delete look like this
CREATE TRIGGER [dbo].[tr_Price_Delete]
ON [dbo].[Price]
AFTER DELETE
AS
BEGIN

SET NOCOUNT ON;
Insert into dbo.PriceArchive
Select
''New'',''D'',
Price_ID,Amount,Store_ID,BSP,ALC_ID,GN_ID,Approved ,CrtdUser,GetDate()
From Deleted

END
Because the delete trigger is from deleted every times when user delete
record the log table populated with the original user name that create the
record

I also changed the label on the GridView with GetUserName() so now it show
only the current user name (overrite the acual record) but it dsen''t
populate the table with current user name
The crtdUser show old values insted of new.

How do I retreived the current user on delete method whith this GridView
control?

Thanks,
Ed Dror

推荐答案

你好Ed,


我试图理解Price页面的逻辑。基于我对

的理解,你正在编写一个价格表(GridView),允许编辑

并删除。

当用户编辑项目时在表中,用户的名字(GetUserName())将被填入其CrtdUser字段中,并且原始的CtrdUser值将被支持为'b
backuped into a' 'PriceArchive''数据库表。

当用户点击''删除''按钮时,CtrdUser值显示

目前将被移动到''PriceArchive' 'table,和原来

CtrdUser值在PriceArchive数据库表中备份将是

恢复到CrtdUser字段。

所以你的问题是当用户点击删除按钮时如何获得显示在

Price GridView中的当前用户名。

这是对的吗?


我们可以将字段CtrdUser添加到GridView的DataKeyNames属性中,

并设置t的DeleteCommand属性他将SqlDataSource as:

" DELETE FROM [PriceTable] WHERE CtrdUser = @ CtrdUser"

SqlDataSource本身知道如何处理@CtrdUser来检索它

当前值。有关详细信息,请参阅MSDN文章:
http ://msdn2.microsoft.com/en-us/library/z72eefad.aspx


另一种方法是从SqlDataSource中删除DeleteCommand,

注册GridView的RowDeleting事件,在其事件处理程序中,我们可以从e.RowIndex获取行项,并调用相应的delete

命令。


如果您有任何其他疑虑或问题,请随时告诉我。


问候,

Jialiang Ge(ji****@online.microsoft.com,删除''在线。'')

Microsoft在线社区支持


欢乐客户是我们的首要任务。我们欢迎您的意见和

有关我们如何改进我们为您提供的支持的建议。请

随时让我的经理知道您对服务水平的看法

提供。您可以直接向我的经理发送反馈:
ms****@microsoft.com


========================================== ========

通过电子邮件收到我的帖子通知?请参阅
http://msdn.microsoft .com / subscripti ... ult.aspx#notif

ications。


注意:MSDN托管新闻组支持服务是针对非紧急问题

如果社区或微软支持人员在1个工作日内做出初步回复是可以接受的。请注意,每个跟随

的响应可能需要大约2个工作日作为支持

专业人士与您合作可能需要进一步调查才能达到

最有效的分辨率。该产品不适用于需要紧急,实时或基于电话的交互或复杂的b $ b项目分析和转储分析问题的情况。这种性质的问题最好通过联系

Microsoft客户支持服务(CSS)处理
href =http://msdn.microsoft.com/subscriptions/support/default.aspx\"target =_ blank> http://msdn.microsoft.com/subscripti...t/default.aspx

======================================== ==========

此帖子按原样提供。没有保证,也没有授予任何权利。

Hello Ed,

I am trying to understand the logic of the Price page. Based on my
understanding, you are composing a Price table (GridView) that allows edit
and delete.
When a user edit an item in the table, the user''s name (GetUserName()) will
be filled into its CrtdUser field, and the orginal CtrdUser value will be
backuped into a ''PriceArchive'' DB table.
When the user click on the ''Delete'' button, the CtrdUser value shows
currently wll be moved to the ''PriceArchive'' table, and the original
CtrdUser value which was backuped in ''PriceArchive'' DB table will be
restored to the CrtdUser field.
So your question is how to get the current user name that shows in the
Price GridView when users click on the ''Delete'' button.
Is this right?

We can add the field CtrdUser into the DataKeyNames property of GridView,
and set the DeleteCommand property of the SqlDataSource as:
"DELETE FROM [PriceTable] WHERE CtrdUser = @CtrdUser"
The SqlDataSource itself knows how to handle @CtrdUser to retrieve its
current value. For more details, see the MSDN article:
http://msdn2.microsoft.com/en-us/library/z72eefad.aspx

Another approach is to remove the DeleteCommand from SqlDataSource,
register the RowDeleting event of the GridView, and in its event handler,
we can get the row item from e.RowIndex, and call the corresponding delete
command.

If you have any other concerns, or questions, feel free to let me know.

Regards,
Jialiang Ge (ji****@online.microsoft.com, remove ''online.'')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.


嗨Ed Dror,


你说的是你将CrtdUser转换成模板并且

将字段绑定从crtdUser更改为GetUserName()函数。


如果我理解正确的话,你做了这样的事情:


< asp:GridView ID =" gvPrices" runat =" server">

< Columns>

< asp:TemplateField>

< ItemTemplate>

< asp:Label ID =" lblUser" Text ="<%#

GetUserName()%>" RUNAT = QUOT;服务器" />

< / ItemTemplate>

< / asp:TemplateField>

< / Columns>

< / asp:GridView>


现在您希望GetUserName()返回的值将保存到数据库的
。我的理解是否正确?


这个问题是你真的不能绑定你自己的

方法。只有Bind()方法会自动加载并保存

字段到数据库。并且你只能在你的

数据源中的字段中使用它。


你可以做的是处理RowUpdating事件

aspx文件中的



< asp:GridView ID =" gvPrices" OnRowUpdating =" gvPrices_RowUpdating"

runat =" server">

代码隐藏中的



受保护的子gvPrices_RowUpdating(ByVal发送者为对象,ByVal e As

GridViewUpdateEventArgs)

e.NewValues(" CrtUser")= GetUserName()

结束分


=========

问候,

Steve
www.stkomp.com

Ed Dror写道:
Hi Ed Dror,

You''re saying that you "converted the CrtdUser into a template and
changed the Field binding from crtdUser to GetUserName() function".

If I understand this correctly, you did something like this:

<asp:GridView ID="gvPrices" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblUser" Text="<%#
GetUserName() %>" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Now you expect that the value GetUserName() returns will be saved to
the database. Is my understanding correct?

The problem with this is that you really can''t bind to your own
method. Only the Bind() method will automatically load from and save
fields to the DB. And you can only use it with fields in your
DataSource.

What you could do instead is handle the RowUpdating event

in the aspx file:

<asp:GridView ID="gvPrices" OnRowUpdating="gvPrices_RowUpdating"
runat="server">

in your codebehind:

Protected Sub gvPrices_RowUpdating(ByVal sender As Object, ByVal e As
GridViewUpdateEventArgs)
e.NewValues("CrtUser") = GetUserName()
End Sub

=========
Regards,
Steve
www.stkomp.com

Ed Dror wrote:

你好,



我有一个价格页面(我的网站需要登录)和GridView的

以下栏目


PriceID,金额,已批准,CrtdUser和日期

和编辑和删除按钮


我创建了一个功能来检索当前用户


受保护的函数GetUserName()As String

返回User.Identity.Name

结束函数


在SQLDatasource1上我添加了


受保护的Sub SqlDataSource1_Updating(ByVal sender As Object,ByVal e As

System.Web.UI.WebControls.SqlDataSourceCommandEven tArgs)句柄

SqlDataSource1.Updating

e.Command.Parameters(" @ CrtdUser")。Value = GetUserName()

End Sub


我将CrtdUser转换为模板更改了字段绑定

从crtdUser到GetUserName()函数


我还创建了一个触发更新,它基本上插入了更新的记录

从Inserted = NEW进入日志表并删除=旧


一切正常


我还为Delete创建了一个触发器看起来像这样

CREATE TRIGGER [dbo]。[tr_Price_Delete]

ON [dbo]。[价格]

删除后

AS

BEGIN


S. ET NOCOUNT ON;

插入dbo.PriceArchive

选择

''新'',''D'',

Price_ID,Amount,Store_ID,BSP,ALC_ID,GN_ID,Approved,CrtdUser,GetDate()

来自已删除


END


因为每次用户删除时都会删除删除触发器

记录用创建

记录的原始用户名填充的日志表


我还使用GetUserName()更改了GridView上的标签,所以现在它只显示当前用户名(仅覆盖实际记录)

但它是dsen '$

用当前用户名填充表格

crtdUser显示新值的旧值。


我如何使用此GridView

控件的删除方法检索当前用户?


谢谢,

Ed Dror
Hi there,

I''m using ASP.NET 2.0 and SQL Server 2005 with VS 2005 Pro.

I have a Price page (my website require login) with GridView with the
following columns

PriceID, Amount, Approved, CrtdUser and Date
And Edit and Delete buttons

I created a function to retrive the current user

Protected Function GetUserName() As String
Return User.Identity.Name
End Function

And on SQLDatasource1 I added

Protected Sub SqlDataSource1_Updating(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.SqlDataSourceCommandEven tArgs) Handles
SqlDataSource1.Updating
e.Command.Parameters("@CrtdUser").Value = GetUserName()
End Sub
I converted the CrtdUser into a template and changed the Field binding
from crtdUser to GetUserName() function

Also I created a Trigger for Update which basically insert updated records
into a log table from Inserted = NEW and deleted = Old

Everything works fine

I also created a trigger for Delete look like this
CREATE TRIGGER [dbo].[tr_Price_Delete]
ON [dbo].[Price]
AFTER DELETE
AS
BEGIN

SET NOCOUNT ON;
Insert into dbo.PriceArchive
Select
''New'',''D'',
Price_ID,Amount,Store_ID,BSP,ALC_ID,GN_ID,Approved ,CrtdUser,GetDate()
From Deleted

END
Because the delete trigger is from deleted every times when user delete
record the log table populated with the original user name that create the
record

I also changed the label on the GridView with GetUserName() so now it show
only the current user name (overrite the acual record) but it dsen''t
populate the table with current user name
The crtdUser show old values insted of new.

How do I retreived the current user on delete method whith this GridView
control?

Thanks,
Ed Dror


Jialiang,

当你看看GridView的所有记录是当前用户名所以

声明

从价格中删除CrtdUser = @CrtdUser将更新所有记录和

这是我们不知道的事情不想要

另外它不会在PriceLog表中显示当前用户名

显示原始用户名


我想将删除按钮转换为模板和OnClick事件调用

存储过程在更新之前更新PriceTable


或者GridView_RowDeleted ...

拨打usp_UpdateCurrentUser

结束子

看看发生了什么


什么你觉得这个吗?


谢谢,


Ed Dror


贾良戈[MSFT] QUOT; < ji **** @ online.microsoft.com写信息

新闻:Ic ************** @ TK2MSFTNGHUB02.phx.gbl ...
Jialiang ,

when you look at the GridView all the record is the current user name so the
statement
Delete from Price where CrtdUser = @CrtdUser will update all the records and
this is somthing we don''t want
Also it will not show in the PriceLog table the current user name it will
show the original user name

I''m thinking to convert the delete button to template and OnClick event call
Stored Proc that update the PriceTable before it got deleted

Or in the GridView_RowDeleted ...
Call usp_UpdateCurrentUser
End Sub
And see what happend

What do you think of that?

Thanks,

Ed Dror

"Jialiang Ge [MSFT]" <ji****@online.microsoft.comwrote in message
news:Ic**************@TK2MSFTNGHUB02.phx.gbl...

你好Ed,


我试图理解Price页面的逻辑。基于我对

的理解,你正在编写一个价格表(GridView),允许编辑

并删除。

当用户编辑项目时在表中,用户名(GetUserName())



填入其CrtdUser字段,原始CtrdUser值将为

备份到''PriceArchive''DB表中。

当用户点击''删除''按钮时,CtrdUser值显示

目前wll移到''PriceArchive''表格,原来的

CtrdUser值在'PriceArchive''DB表中备份将是

恢复到CrtdUser字段。

所以你的问题是当用户点击删除按钮时,如何获得显示在

Price GridView中的当前用户名。

这是对的吗?


我们可以将字段CtrdUser添加到GridView的DataKeyNames属性中,

并设置DeleteCommand pro sqlDataSource的perty为:

" DELETE FROM [PriceTable] WHERE CtrdUser = @ CtrdUser"

SqlDataSource本身知道如何处理@CtrdUser来检索它的

当前值。有关详细信息,请参阅MSDN文章:
http ://msdn2.microsoft.com/en-us/library/z72eefad.aspx


另一种方法是从SqlDataSource中删除DeleteCommand,

注册GridView的RowDeleting事件,在其事件处理程序中,我们可以从e.RowIndex获取行项,并调用相应的delete

命令。


如果您有任何其他疑虑或问题,请随时告诉我。


问候,

Jialiang Ge(ji****@online.microsoft.com,删除''在线。'')

Microsoft在线社区支持


欢乐客户是我们的首要任务。我们欢迎您的意见和

有关我们如何改进我们为您提供的支持的建议。请

随时让我的经理知道您对服务水平的看法

提供。您可以直接向我的经理发送反馈:
ms****@microsoft.com


========================================== ========

通过电子邮件收到我的帖子通知?请参阅
http://msdn.microsoft .com / subscripti ... ult.aspx#notif

ications。


注意:MSDN托管新闻组支持服务是针对非紧急问题

如果社区或微软支持人员在1个工作日内做出初步回复是可以接受的。请注意,每个跟随

的响应可能需要大约2个工作日作为支持

专业人士与您合作可能需要进一步调查才能达到

最有效的分辨率。该产品不适用于需要紧急,实时或基于电话的交互或复杂的b $ b项目分析和转储分析问题的情况。这种性质的问题最好通过联系

Microsoft客户支持服务(CSS)处理
href =http://msdn.microsoft.com/subscriptions/support/default.aspx\"target =_ blank> http://msdn.microsoft.com/subscripti...t/default.aspx

======================================== ==========

此帖子按原样提供。没有保证,也没有赋予

权利。
Hello Ed,

I am trying to understand the logic of the Price page. Based on my
understanding, you are composing a Price table (GridView) that allows edit
and delete.
When a user edit an item in the table, the user''s name (GetUserName())
will
be filled into its CrtdUser field, and the orginal CtrdUser value will be
backuped into a ''PriceArchive'' DB table.
When the user click on the ''Delete'' button, the CtrdUser value shows
currently wll be moved to the ''PriceArchive'' table, and the original
CtrdUser value which was backuped in ''PriceArchive'' DB table will be
restored to the CrtdUser field.
So your question is how to get the current user name that shows in the
Price GridView when users click on the ''Delete'' button.
Is this right?

We can add the field CtrdUser into the DataKeyNames property of GridView,
and set the DeleteCommand property of the SqlDataSource as:
"DELETE FROM [PriceTable] WHERE CtrdUser = @CtrdUser"
The SqlDataSource itself knows how to handle @CtrdUser to retrieve its
current value. For more details, see the MSDN article:
http://msdn2.microsoft.com/en-us/library/z72eefad.aspx

Another approach is to remove the DeleteCommand from SqlDataSource,
register the RowDeleting event of the GridView, and in its event handler,
we can get the row item from e.RowIndex, and call the corresponding delete
command.

If you have any other concerns, or questions, feel free to let me know.

Regards,
Jialiang Ge (ji****@online.microsoft.com, remove ''online.'')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.



这篇关于关于删除方法问题的GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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