简体   繁体   中英

how to prevent gridview row selection on editing mode.(asp.net server-side)

I have a table that is filled with data and if the user clicks on a row, the data inside the selected row will populate the input fields. 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 the user click save/cancel then the row selection is enabled again. I am new in using asp.net, can someone help me, t

this is the gridview code

<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>

This is the html input fields

<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>

The Buttons

<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#(server-side) code

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;
       }


      }
   } 

You are no need to create your own buttons for selecting, editing/cancelling the update operation. Asp.net GridView have in-built option to handle these operations.

I have modified your GridView Code to have in-built edit button, please refer below

<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>

And your can handle edit, update and cancel edit operation by their corresponding events on code behind GridView1_RowEditing, GridView1_RowCancelingEdit and GridView1_RowUpdating. Hope this can help. Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM