根据IF条件停止更新DetailsView

时间:2015-10-05 11:32:17

标签: c# asp.net

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);
}

1 个答案:

答案 0 :(得分:0)

在DetailsView上使用ChangeMode方法

DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);