C# 如何防止在编辑模式下选择gridview行。(asp.net服务器端)
我有一个充满数据的表,如果用户单击一行,所选行中的数据将填充输入字段。我创建了一个onclick函数,允许用户选择正在工作的gridview中的行,我想要的是,当用户单击编辑按钮时,用户不能选择所选行以外的其他行(禁用行选择),当用户单击保存/取消时,行选择再次启用。我不太会使用asp.net,有人能帮我吗 这是gridview代码C# 如何防止在编辑模式下选择gridview行。(asp.net服务器端),c#,asp.net,C#,Asp.net,我有一个充满数据的表,如果用户单击一行,所选行中的数据将填充输入字段。我创建了一个onclick函数,允许用户选择正在工作的gridview中的行,我想要的是,当用户单击编辑按钮时,用户不能选择所选行以外的其他行(禁用行选择),当用户单击保存/取消时,行选择再次启用。我不太会使用asp.net,有人能帮我吗 这是gridview代码 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Da
<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>
您无需创建自己的按钮来选择、编辑/取消更新操作。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_行编辑、GridView1_行取消编辑和GridView1_行更新后的代码上的相应事件来处理编辑、更新和取消编辑操作。希望这能有所帮助。谢谢。您好,谢谢您的回答,我知道有这样一个函数,但我正在以不同的方式使用它。
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="Edit" />
<asp:Button ID="Button2" runat="server" onclick="Button2_Click"
Text="Save" Enabled="false"/>
<asp:Button ID="Button3" runat="server" onclick="Button3_Click"
Text="Cancel" Enabled="false"/>
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: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>