GridView.RowUpdating Olay

Tanım

Bir satırın Güncelleştir düğmesine tıklandığında, ancak denetim satırı güncelleştirmeden önce GridView gerçekleşir.

C#
public event System.Web.UI.WebControls.GridViewUpdateEventHandler RowUpdating;

Olay Türü

Örnekler

Aşağıdaki örnek, veri kaynağı program aracılığıyla ayarlandığında veri kaynağı nesnesindeki değerleri güncelleştirmek için olayının nasıl kullanılacağını RowUpdating gösterir.

ASP.NET (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 < 20; 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_PageIndexChanging(object sender, GridViewPageEventArgs e)
  {
    TaskGridView.PageIndex = e.NewPageIndex;
    //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[row.DataItemIndex]["Id"] = ((TextBox)(row.Cells[1].Controls[0])).Text;
    dt.Rows[row.DataItemIndex]["Description"] = ((TextBox)(row.Cells[2].Controls[0])).Text;
    dt.Rows[row.DataItemIndex]["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 xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>GridView example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
      <asp:GridView ID="TaskGridView" runat="server" 
        AutoGenerateEditButton="True" 
        AllowPaging="true"
        OnRowEditing="TaskGridView_RowEditing"         
        OnRowCancelingEdit="TaskGridView_RowCancelingEdit" 
        OnRowUpdating="TaskGridView_RowUpdating"
        OnPageIndexChanging="TaskGridView_PageIndexChanging">
      </asp:GridView>
    
    </div>
    </form>
</body>
</html>

Açıklamalar

Olay RowUpdating , bir satırın Güncelleştir düğmesine tıklandığında, ancak denetim satırı güncelleştirmeden önce GridView tetikler. Bu, bu olay gerçekleştiğinde güncelleştirme işlemini iptal etme gibi özel bir yordam gerçekleştiren bir olay işleme yöntemi sağlamanıza olanak tanır.

GridViewUpdateEventArgs Geçerli satırın dizinini belirlemenize ve güncelleştirme işleminin iptal edilmesi gerektiğini belirtmenize olanak tanıyan olay işleme yöntemine bir nesne geçirilir. Güncelleştirme işlemini iptal etmek için nesnesinin CancelGridViewUpdateEventArgs özelliğini olarak trueayarlayın. Ayrıca, gerekirse, değerler veri kaynağına geçirilmeden önce , OldValuesve NewValues koleksiyonlarını da işleyebilirsinizKeys. Bu koleksiyonları kullanmanın yaygın bir yolu, veri kaynağında depolanmadan önce kullanıcı tarafından sağlanan değerleri HTML ile kodlamaktır. Bu, betik ekleme saldırılarını önlemeye yardımcı olur.

Not

Keysve OldValuesNewValues koleksiyonları, yalnızca denetim özelliği kullanılarak DataSourceID verilere bağlı olduğunda GridView otomatik olarak doldurulur.

Olayları işleme hakkında daha fazla bilgi için bkz. Olayları İşleme ve Oluşturma.

Şunlara uygulanır

Ürün Sürümler
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

Ayrıca bkz.