【问题标题】:how to prevent gridview row selection on editing mode.(asp.net server-side)如何在编辑模式下防止gridview行选择。(asp.net服务器端)
【发布时间】:2019-10-03 04:03:36
【问题描述】:

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

这是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" />
    &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;
       }


      }
   } 

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    您无需创建自己的按钮来选择、编辑/取消更新操作。 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 后面的代码上的相应事件来处理编辑、更新和取消编辑操作。希望这能有所帮助。谢谢。

    【讨论】:

    • 您好,谢谢您的回答,我知道有这样的功能,但我正在以不同的方式工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-02
    • 2014-04-20
    • 1970-01-01
    相关资源
    最近更新 更多