简体   繁体   中英

select only one checkbox in gridview and edit that checked row

i have a gridview and which has one column as the asp checkbox control and i want the user can check one check box and click on the edit button to edit that perticular row

my code for this is

                 <asp:GridView runat="server" CssClass="width" ID="gvGrades" AutoGenerateColumns="false" ShowHeader="true" OnRowCancelingEdit="gvGrades_RowCancelingEdit"                            OnRowCommand="gvGrades_RowCommand" OnRowDataBound="gvGrades_RowDataBound" OnRowEditing="gvGrades_RowEditing"                               OnRowUpdated="gvGrades_RowUpdated" OnRowUpdating="gvGrades_RowUpdating" OnRowDeleting="gvGrades_RowDeleting">
                            <Columns>
                                <asp:TemplateField>
                                    <HeaderTemplate>
                                        Select

                                    </HeaderTemplate>
                                    <ItemTemplate>
                                        <asp:CheckBox ID="chkSelect" class="asp" runat="server"  />

                                        <asp:Label ID="lblId" runat="server" Text='<%# Bind("id") %>' Visible="false"></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField>
                                    <HeaderTemplate>
                                        Grade Name
                                    </HeaderTemplate>
                                    <ItemTemplate>
                                        <asp:Label ID="lblName" runat="server" Text='<%# Bind("grade_name") %>'></asp:Label>
                                    </ItemTemplate>

                                 </asp:TemplateField>
                                <asp:TemplateField>
                                    <HeaderTemplate>
                                        Organization Name
                                    </HeaderTemplate>
                                    <ItemTemplate>
                                        <asp:Label ID="lblOrganizationName" runat="server" Text='<%# Bind("organization_name") %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField>
                                    <HeaderTemplate>
                                        Minimum Basic Salary
                                    </HeaderTemplate>
                                    <ItemTemplate>
                                        <asp:Label ID="lblSalary" runat="server" Text='<%# Bind("min_basic_salary") %>'></asp:Label>
                                    </ItemTemplate>

                                </asp:TemplateField>
                                <asp:TemplateField>
                                    <ItemTemplate>
                                        <asp:LinkButton runat="server" ID="lnkDelete" CommandName="Delete" Text="Delete"></asp:LinkButton>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField>
                                    <ItemTemplate>
                                        <asp:LinkButton runat="server" ID="lnkEdit" CommandName="Edit" Text="Edit"></asp:LinkButton>
                                    </ItemTemplate>

                                </asp:TemplateField>
                            </Columns>
                        </asp:GridView>
                    </fieldset>
                </ContentTemplate>
            </asp:UpdatePanel>
            <asp:Button Text="Add" Width="70px" ID="btnAdd" runat="server" OnClick="btnAdd_Click" />&nbsp;&nbsp;
            <asp:Button Text="Edit" ID="btnEdit" Width="70px" runat="server" OnClick="btnEdit_Click"
                />

Please provide code for edit button click also

thanks

use can use GridView.SelectedIndexChanged Event.

you might do something like this

<asp:TemplateField >
            <ItemTemplate>     
                <asp:CheckBox ID="chb1" AutoPostBack="true" runat="server" />
            </ItemTemplate>
 </asp:TemplateField>

and you can set the edit fied enable=false and in the postback you can set enable=true

protected void GVSelectedIndexChanged(object sender, GridViewRowEventArgs e)
{
            var check = (CheckBox) e.Row.FindControl("chb1");
            if(check != null)
            {
                    // do something
            }
}

Follow the below steps for enable edit button without postback [using javascript]

  1. Register OnRowDataBound event of Gridview.

  2. Find the checkbox and Edit button using the itemindex [in Onrowbound event]

  3. After finding checkbox, register javascript function onchange event with parameter [of Edit button Id]

  4. Then you will get the Edit button in javascript

  5. Put the javascript enable code.

Let me know if you have any problem or query.

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