简体   繁体   English

ASP在sqlDataSource中创建自己的更新语句

[英]ASP Create own Update Statement in sqlDataSource

I am trying to make own update using graphic interface in VisualStudio but my update dont work. 我正在尝试使用VisualStudio中的图形界面进行自己的更新,但我的更新无法正常工作。

When i test it in query builder everything works but when i run the web page after click update added to grid it's restore to previous state because the update did nothing. 当我在查询生成器中对其进行测试时,一切正常,但是,当我在单击更新添加到网格后运行网页时,由于更新没有执行任何操作,因此恢复了以前的状态。

Params("ID,IsCompleted") are added to DataKeyNames in Property in GridView. Params("ID,IsCompleted")添加到GridView中“属性Params("ID,IsCompleted") DataKeyNames中。 Unfortunately I could not find any solutions using a graphical interface. 不幸的是,我找不到使用图形界面的解决方案。

在此处输入图片说明

subscribe SqlDataSource's OnUpdated event, check AffectedRows value. 订阅SqlDataSource的OnUpdated事件,检查AffectedRows值。
ref msdn SqlDataSource.UpdateCommand ref msdn SqlDataSource.UpdateCommand
codes 代码

private void OnDSUpdatedHandler(Object source, SqlDataSourceStatusEventArgs e) {
    if (e.AffectedRows > 0) {
        // Perform any additional processing, 
        // such as setting a status label after the operation.
        Label1.Text = Request.LogonUserIdentity.Name +
            " changed user information successfully!";    
    }
    else {
        Label1.Text = "No data updated!";
    }
 }

aspx ASPX

<asp:SqlDataSource
  id="SqlDataSource1"
  runat="server"
  DataSourceMode="DataSet"
  ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
  SelectCommand="SELECT EmployeeID,FirstName,LastName,Title FROM Employees"
  UpdateCommand="Update Employees SET FirstName=@FirstName,LastName=@LastName,Title=@Title WHERE EmployeeID=@EmployeeID"
  OnUpdated="OnDSUpdatedHandler">
</asp:SqlDataSource>

<asp:GridView
  id="GridView1"
  runat="server"
  AutoGenerateColumns="False"
  DataKeyNames="EmployeeID"
  AutoGenerateEditButton="True"
  DataSourceID="SqlDataSource1">
  <columns>
      <asp:BoundField HeaderText="First Name" DataField="FirstName" />
      <asp:BoundField HeaderText="Last Name" DataField="LastName" />
      <asp:BoundField HeaderText="Title" DataField="Title" />
  </columns>
</asp:GridView>

<asp:Label
  id="Label1"
  runat="server">
</asp:Label>

other ref SqlDataSourceView.OnUpdating 其他参考SqlDataSourceView.OnUpdating

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM