Итак, у меня есть этот код для отключения выбора строки в виде сетки (отключение выбора строки выполняется, если пользователь нажимает кнопку редактирования), проблема в том, что после того, как я нажму на кнопку редактирования, будет разрешена одна временная строкаВыделение, а затем отключить выбор строки, что мне нужно, это то, что, как только я нажимаю на редактирование, он не должен позволять какой-либо выбор строки в виде сетки. Что я сделал, так это создал оператор if в RowDataBound, в котором, если texbox.enabled = false, он разрешит выбор, если true, он не разрешит какой-либо выбор строки (настройка по умолчанию для texboxes - «texbox.enabled = false». ), также отключение выбора строки выполняется только в режиме редактирования (после нажатия кнопки «Изменить»), если пользователь нажимает кнопку «Сохранить / Отменить», выбор строки должен быть снова включен
Я использую поле команды длявыделение строки в виде сетки.
HTML-код:
<asp:GridView ID="grdRecentCases" runat="server" AutoGenerateColumns="False" DataKeyNames="CASE_KEY"
Height="250px" Width="1109px" BackColor="White"
BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical"
OnRowDataBound="grdRecentCases_RowDataBound"
onselectedindexchanged="grdRecentCases_SelectedIndexChanged" >
<AlternatingRowStyle BackColor="Gainsboro" />
<Columns>
<asp:CommandField HeaderText="Select" ShowSelectButton="True"/>
<asp:BoundField DataField="CASE_KEY" HeaderText="CASE_KEY" ReadOnly="True" SortExpression="CASE_KEY"
Visible="true" />
<asp:BoundField DataField="DEPARTMENT_CASE_NUMBER" HeaderText="Department Case #"
SortExpression="DEPARTMENT_CASE_NUMBER" />
<asp:BoundField DataField="DEPARTMENT_NAME" HeaderText="Department" SortExpression="DEPARTMENT_NAME" />
<asp:BoundField DataField="CHARGE" HeaderText="Charge" SortExpression="CHARGE" />
<asp:BoundField DataField="LAB_CASE" HeaderText="Lab Case #" SortExpression="LAB_CASE" />
<asp:BoundField DataField="OFFENSE_DATE" HeaderText="Incident Report Date" SortExpression="OFFENSE_DATE" />
</Columns>
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#0000A9" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#000065" />
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
<br />
<asp:Label ID="lblCaseDetails" runat="server" Font-Bold="True" Font-Names="Arial"
Font-Size="Small" Text="Case Details"></asp:Label>
<br />
<asp:UpdatePanel ID="upCaseDetails" runat="server">
<ContentTemplate>
<table class="style2">
<tr>
<td class="style3">
Department Case #
</td>
<td>
<asp:TextBox ID="txtDepartmentCase" runat="server" Enabled="False"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style3">
Department
</td>
<td>
<asp:DropDownList ID="drpDepartment" runat="server" Height="18px" Width="153px" Enabled="False"
AppendDataBoundItems="true" AutoPostBack="true" OnSelectedIndexChanged="drpDepartment_SelectedIndexChanged"
Visible="False">
</asp:DropDownList>
<asp:TextBox ID="txtDepartment" runat="server" Enabled="False"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style3">
Charge
</td>
<td>
<asp:DropDownList ID="drpCharge" runat="server" Height="22px" Width="153px" Enabled="False"
AppendDataBoundItems="true" AutoPostBack="true" OnSelectedIndexChanged="drpCharge_SelectedIndexChanged"
Visible="False">
</asp:DropDownList>
<asp:TextBox ID="txtCharge" runat="server" Enabled="False"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style3">
Lab Case #
</td>
<td>
<asp:TextBox ID="txtLabCase" runat="server" Enabled="False"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style3">
Incident Report Date
</td>
<td>
<asp:TextBox ID="txtIncidentReportDate" runat="server" Enabled="False"></asp:TextBox>
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
<asp:TextBox ID="txtCaseKey" runat="server" Visible="False"></asp:TextBox>
<br />
<asp:Button ID="btnEdit" runat="server" OnClick="btnEdit_Click" Text="Edit" />
<asp:Button ID="btnSave" runat="server" OnClick="btnSave_Click" Text="Save" Enabled="false" />
<asp:Button ID="btnCancel" runat="server" OnClick="btnCancel_Click" Text="Cancel"
Enabled="false" />
Код позади
protected void grdRecentCases_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.textDecoration='underline';this.style.cursor='Pointer'";
e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
if (txtLabCase.Enabled == false)//disabling the row selection
{
e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.grdRecentCases, "Select$" + e.Row.RowIndex);
}
}
}
protected void grdRecentCases_SelectedIndexChanged(object sender, EventArgs e)
{
LoadData();
}
///<summary> LoadData is used to populate inputboxes with the value of the selected griview row</summary>
///<param name="rowNumber"> Indicates whether there is a selected row or non</param>
private void LoadData(int? rowNumber = null)
{
//if rowNumber is null use GridView1.SelectedIndex
var index = rowNumber ?? grdRecentCases.SelectedIndex;
//Populate the input box with the value of selected row.
GridViewRow gr = grdRecentCases.Rows[index];
txtDepartmentCase.Text = gr.Cells[2].Text;
txtDepartment.Text = gr.Cells[3].Text;
txtCharge.Text = gr.Cells[4].Text;
txtLabCase.Text = gr.Cells[5].Text;
txtIncidentReportDate.Text = gr.Cells[6].Text;
txtCaseKey.Text = gr.Cells[1].Text;
}
protected void drpDepartment_SelectedIndexChanged(object sender, EventArgs e)
{ //Use to pass the selected value item inside the dropdownlist.
drpDepartment.SelectedValue = drpDepartment.SelectedItem.Value;
drpCharge.SelectedValue = drpCharge.SelectedItem.Value;
}
protected void drpCharge_SelectedIndexChanged(object sender, EventArgs e)
{ //Use to pass the selected value item inside the dropdownlist.
drpCharge.SelectedValue = drpCharge.SelectedItem.Value;
}
protected void btnEdit_Click(object sender, EventArgs e)
{
EnableDisableControls(true);
}
protected void btnSave_Click(object sender, EventArgs e)
{
EnableDisableControls(false);
string connStr = ConfigurationManager.ConnectionStrings["****ConnectionString"].ConnectionString;
var cnn = new SqlConnection(connStr);
cnn.Open();
SqlCommand cmd = new SqlCommand("Update TV_LABCASE Set DEPARTMENT_CASE_NUMBER=@DEPARTMENT_CASE_NUMBER,DEPARTMENT_CODE=@DEPARTMENT_NAME,OFFENSE_CODE=@OFFENSE_DESCRIPTION,LAB_CASE=@LAB_CASE,OFFENSE_DATE=@OFFENSE_DATE where CASE_KEY=@CASE_KEY", cnn);
cmd.Parameters.AddWithValue("@DEPARTMENT_CASE_NUMBER", txtDepartmentCase.Text);
cmd.Parameters.AddWithValue("@DEPARTMENT_NAME", drpDepartment.SelectedValue);
cmd.Parameters.AddWithValue("@OFFENSE_DESCRIPTION", drpCharge.SelectedValue);
cmd.Parameters.AddWithValue("@LAB_CASE", txtLabCase.Text);
cmd.Parameters.AddWithValue("@OFFENSE_DATE", txtIncidentReportDate.Text);
cmd.Parameters.AddWithValue("@CASE_KEY", txtCaseKey.Text);
cmd.ExecuteNonQuery();
txtDepartment.Text = drpDepartment.SelectedItem.Text;
txtCharge.Text = drpCharge.SelectedItem.Text;
grdRecentCases.DataBind();
}
protected void btnCancel_Click(object sender, EventArgs e)
{
EnableDisableControls(false);
LoadData();
}
///<summary> define the editing state to determine which behavior of buttons/inputbox/dropdown to manipulate.</summary>
///<param name="editable" type="button;text;dropdown"> Editing State</param>
private void EnableDisableControls(bool editable)
{
btnEdit.Enabled = !editable;
btnSave.Enabled = editable;
btnCancel.Enabled = editable;
txtDepartmentCase.Enabled = editable;
txtLabCase.Enabled = editable;
txtIncidentReportDate.Enabled = editable;
txtDepartment.Visible = !editable;
txtCharge.Visible = !editable;
drpDepartment.Visible = editable;
drpCharge.Visible = editable;
drpDepartment.Enabled = editable;
drpCharge.Enabled = editable;
}