如何防止在编辑模式下选择 GridView 行.(asp.net 服务器端)



我有一个充满数据的表,如果用户单击一行,所选行内的数据将填充输入字段。我创建了一个 onclick 函数,使用户能够在正在工作的网格视图中选择行,我想要的是当用户单击编辑按钮时,用户无法选择所选行以外的其他行(禁用行选择(,当用户单击保存/取消时,再次启用行选择。我是使用 asp.net 的新手,有人可以帮助我吗?

这是网格视图代码

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
DataKeyNames="CASE_KEY" DataSourceID="SqlDataSource1" Height="250px" 
Width="1109px" BackColor="White" BorderColor="#999999" BorderStyle="None" 
BorderWidth="1px" CellPadding="3" GridLines="Vertical" onrowcommand="GridView1_RowCommand"
OnRowDataBound="GridView1_RowDataBound">
<AlternatingRowStyle BackColor="Gainsboro" />
<Columns>
<asp:buttonfield buttontype="Link" commandname="Select" text="Select" Visible="False" />
<asp:BoundField DataField="CASE_KEY" HeaderText="CASE_KEY" ReadOnly="True" 
SortExpression="CASE_KEY" Visible="False" />
<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>

这是 html 输入字段

<table class="style2" >
<tr>
<td class="style3">
Department Case #</td>
<td>
<asp:TextBox ID="TextBox1" runat="server" Enabled="False"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style3">
Department</td>
<td>
<asp:TextBox ID="TextBox2" runat="server" Enabled="False"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style3">
Charge</td>
<td>
<asp:TextBox ID="TextBox3" runat="server" Enabled="False"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style3">
Lab Case #</td>
<td>
<asp:TextBox ID="TextBox4" runat="server" Enabled="False"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style3">
Incident Report Date</td>
<td>
<asp:TextBox ID="TextBox5" runat="server" Enabled="False"></asp:TextBox>
</td>
</tr>
</table>

按钮

<asp:Button ID="Button1" runat="server" onclick="Button1_Click" 
Text="Edit" />
&nbsp;
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" 
Text="Save" Enabled="false"/>
&nbsp;
<asp:Button ID="Button3" runat="server" onclick="Button3_Click" 
Text="Cancel" Enabled="false"/>

C#(服务器端(代码

protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Change the mouse cursor to Hand symbol to show the user the cell is selectable
e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.textDecoration='underline';this.style.cursor='Pointer'";
e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
//Attach the click event to each cells
e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
}
}
protected void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
{
// If multiple buttons are used in a GridView control, use the
// CommandName property to determine which button was clicked.
if (e.CommandName == "Select")
{
// Convert the row index stored in the CommandArgument
// property to an Integer.
int index = Convert.ToInt32(e.CommandArgument);
// Retrieve the row that contains the button clicked 
// by the user from the Rows collection.
GridViewRow row = GridView1.Rows[index];
// Populate the input box with the value of selected row.     
GridViewRow gr = GridView1.Rows[index];
TextBox1.Text = gr.Cells[2].Text;
TextBox2.Text = gr.Cells[3].Text;
TextBox3.Text = gr.Cells[4].Text;
TextBox4.Text = gr.Cells[5].Text;
TextBox5.Text = gr.Cells[6].Text;
}
}    

protected void Button1_Click(object sender, EventArgs e)
{
Button1.Enabled = false;
Button2.Enabled = true;
Button3.Enabled = true;
TextBox1.Enabled = true;
TextBox2.Enabled = true;
TextBox3.Enabled = true;
TextBox4.Enabled = true;
TextBox5.Enabled = true;
}
protected void Button2_Click(object sender, EventArgs e)
{
Button1.Enabled = true;
Button2.Enabled = false;
Button3.Enabled = false;
TextBox1.Enabled = false;
TextBox2.Enabled = false;
TextBox3.Enabled = false;
TextBox4.Enabled = false;
TextBox5.Enabled = false;
}
protected void Button3_Click(object sender, EventArgs e)
{
Button1.Enabled = true;
Button2.Enabled = false;
Button3.Enabled = false;
TextBox1.Enabled = false;
TextBox2.Enabled = false;
TextBox3.Enabled = false;
TextBox4.Enabled = false;
TextBox5.Enabled = false;
}

}
} 

您无需创建自己的按钮来选择、编辑/取消更新操作。 Asp.net GridView 具有内置选项来处理这些操作。

我已经修改了您的 GridView 代码以具有内置的编辑按钮,请参阅以下内容

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
DataKeyNames="CASE_KEY" DataSourceID="SqlDataSource1" Height="250px" 
Width="1109px" BackColor="White" BorderColor="#999999" BorderStyle="None" 
BorderWidth="1px" CellPadding="3" GridLines="Vertical"  
>
<AlternatingRowStyle BackColor="Gainsboro" />
<Columns>
<asp:CommandField ShowEditButton="true" /> 
<asp:BoundField DataField="CASE_KEY" HeaderText="CASE_KEY" ReadOnly="True" 
SortExpression="CASE_KEY" Visible="False" />
<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>

并且您可以通过它们在代码隐藏GridView1_RowEditing、GridView1_RowCancelingEdit和GridView1_RowUpdating上的相应事件来处理编辑、更新和取消编辑操作。希望这能有所帮助。谢谢。

最新更新