[英]how to prevent gridview row selection on editing mode.(asp.net server-side)
我有一個充滿數據的表,如果用戶單擊一行,則所選行內的數據將填充輸入字段。 I have created a onclick function which enable the user to select row inside the gridview which is working and what i want is that when user click on edit button, the user cannot select other row (disable row selection) other than the selected row and when用戶單擊保存/取消,然后再次啟用行選擇。 我是使用 asp.net 的新手,有人可以幫幫我嗎?
這是 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" 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" />
<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"/>
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 后面的代碼上的相應事件來處理編輯、更新和取消編輯操作。 希望這能有所幫助。 謝謝。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.