停止根据IF条件更新DetailsView

本文关键字:条件 更新 DetailsView IF | 更新日期: 2023-09-27 18:15:58

我有DetailsView与连接的SqlDataSource,当我的条件在OnRowUpdating事件为真时,我想取消更新行我做了这一步,但是当条件为真DetailsView仍然在编辑模式下,如果条件为真,我需要返回到正常视图

protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
    // Iterates through the rows of the GridView control
    foreach (DetailsViewRow row in DetailsView1.Rows)
    {
        // Selects the text from the TextBox
        // which is inside the GridView control
        // Selects the text from the DropDownList
        // which is inside the GridView control
        string dropDownListText = ((DropDownList)
           row.FindControl("DropDownList12")).SelectedItem.Text;
        SqlConnection conn = new SqlConnection(GetConnectionString());
        SqlCommand cmd2 = new SqlCommand();
        conn.Open();
        cmd2.CommandText = @"Select StatusID from ComplainMain Where TicketID=@tktid";
        cmd2.Parameters.AddWithValue("@tktid", ticketid.tktid);
        cmd2.Connection = conn;
        SqlDataReader rdr = cmd2.ExecuteReader();
        int status;
        while (rdr.Read())
        {
            status = rdr.GetInt32(0);
            if (status == 3)
            {
                Label18.Visible = true;
                Label18.Text = "Can not Modify this Complaint as Ticket Closed refer to the Admin";
                e.Cancel = true;
            }   
        }
    }
}

Page_Load

protected void Page_Load(object sender, EventArgs e)
{
    Label18.Visible = false;
    DetailsView1.ItemUpdating += new DetailsViewUpdateEventHandler(DetailsView1_ItemUpdating);
}

停止根据IF条件更新DetailsView

在DetailsView上使用ChangeMode方法

DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);