GridView.RowUpdating 事件

下面的示例演示如何使用 RowUpdating 事件在更新数据源之前对用户提供的所有值进行 HTML 编码。

C#
<%@ Page language= "C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat= "server">

  void CustomersGridView_RowUpdating(Object sender, GridViewUpdateEventArgs e)
  {

    // Iterate through the NewValues collection and HTML encode all
    // user-provided values before updating the data source.
    foreach (DictionaryEntry entry in e.NewValues)
    {

      e.NewValues[entry.Key] = Server.HtmlEncode(entry.Value.ToString());

    }

  }

</script>

<html  >
  <head runat= "server">
    <title>GridView RowUpdating Example</title>
</head>
<body>
    <form id= "form1" runat= "server">

      <h3>GridView RowUpdating Example</h3>

      <!-- The GridView control automatically sets the columns     -->
      <!-- specified in the datakeynames property as read-only.    -->
      <!-- No input controls are rendered for these columns in     -->
      <!-- edit mode.                                              -->
      <asp:gridview id= "CustomersGridView"
        datasourceid= "CustomersSqlDataSource"
        autogeneratecolumns= "true"
        autogenerateeditbutton= "true"
        allowpaging= "true"
        datakeynames= "CustomerID"
        onrowupdating= "CustomersGridView_RowUpdating" 
        runat= "server">
      </asp:gridview>

      <!-- This example uses Microsoft SQL Server and connects  -->
      <!-- to the Northwind sample database. Use an ASP.NET     -->
      <!-- expression to retrieve the connection string value   -->
      <!-- from the Web.config file.                            -->
      <asp:sqldatasource id= "CustomersSqlDataSource" 
        selectcommand= "Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        updatecommand= "Update Customers SET CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country WHERE (CustomerID = @CustomerID)"
        connectionstring= "<%$ ConnectionStrings:NorthWindConnectionString%>"
        runat= "server">
      </asp:sqldatasource>

    </form>
  </body>
</html>


下面的示例演示如何使用 RowUpdating 事件在以编程方式设置数据源时更新数据源对象中的值。

C#
<%@ Page Language= "C#" %>
<%@ Import Namespace= "System.Data" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat= "server">

  protected void Page_Load(object sender, EventArgs e)
  {

    if (!Page.IsPostBack)
    {
      // Create a new table.
      DataTable taskTable = new DataTable( "TaskList");

      // Create the columns.
      taskTable.Columns.Add( "Id", typeof( int));
      taskTable.Columns.Add( "Description", typeof( string));
      taskTable.Columns.Add( "IsComplete", typeof( bool) );

      //Add data to the new table.
      for ( int i = 0; i < 10; i++)
      {
        DataRow tableRow = taskTable.NewRow();
        tableRow[ "Id"] = i;
        tableRow[ "Description"] = "Task " + i.ToString();
        tableRow[ "IsComplete"] = false;           
        taskTable.Rows.Add(tableRow);
      }

      //Persist the table in the Session object.
      Session[ "TaskTable"] = taskTable;

      //Bind data to the GridView control.
      BindData();
    }

  }

  protected void TaskGridView_RowEditing(object sender, GridViewEditEventArgs e)
  {
    //Set the edit index.
    TaskGridView.EditIndex = e.NewEditIndex;
    //Bind data to the GridView control.
    BindData();
  }

  protected void TaskGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
  {
    //Reset the edit index.
    TaskGridView.EditIndex = -1;
    //Bind data to the GridView control.
    BindData();
  }

  protected void TaskGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
  {
    //Retrieve the table from the session object.
    DataTable dt = (DataTable)Session[ "TaskTable"];

    //Update the values.
    GridViewRow row = TaskGridView.Rows[e.RowIndex];
    dt.Rows[TaskGridView.EditIndex][ "Id"] = ((TextBox)(row.Cells[1].Controls[0])).Text;
    dt.Rows[TaskGridView.EditIndex][ "Description"] = ((TextBox)(row.Cells[2].Controls[0])).Text;
    dt.Rows[TaskGridView.EditIndex][ "IsComplete"] = ((CheckBox)(row.Cells[3].Controls[0])).Checked;

    //Reset the edit index.
    TaskGridView.EditIndex = -1;

    //Bind data to the GridView control.
    BindData();
  }

  private void BindData()
  {
    TaskGridView.DataSource = Session[ "TaskTable"];
    TaskGridView.DataBind();
  }
</script>

<html >
<head runat= "server">
    <title>GridView example</title>
</head>
<body>
    <form id= "form1" runat= "server">
    <div>

      <asp:GridView ID= "TaskGridView" runat= "server"
        AutoGenerateEditButton= "True"
        OnRowEditing= "TaskGridView_RowEditing"        
        OnRowCancelingEdit= "TaskGridView_RowCancelingEdit"
        OnRowUpdating= "TaskGridView_RowUpdating">
      </asp:GridView>

    </div>
    </form>
</body>
</html>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值