Skip to content

Adding checkbox and textbox to gridview using ASP.Net

Aly Lucas

Asked Sep 29, 2013, 2:27 AM EDT1 answer
Share
LinkedInX
I'm trying to add a checkbox and textbox to a gridview list of products.

The gridview is populated from a Product table (ProductID, ProductName, Price) in SQL DB. I'm trying to get each line of the gridview to have a checkbox and textbox so the user can select the row and enter a quantity of the product then and store it in Product_Order table(Product_OrderID, ProductID, Quantity), after all the products they are ordering are checked w/ quantities input next to each line, the list is inserted into the database via a button event.

Can you help me how to do this? Is gridview was the best control in for doing this? Thanks in advanced! :)

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    You can try the below code.
    You need to write javascript to remove the readonly attribute from txtQty textbox.

    <asp:GridView ID="GridView1" runat="server"
                                 AllowPaging="True"                             
                                 AutoGenerateColumns="false">
    <Columns>
        <asp:TemplateField HeaderText="Select">
        <ItemTemplate>
        <asp:CheckBox ID="chkSelect" runat="server"
                      AutoPostBack="true"
                 OnCheckedChanged="chkSelect_CheckedChanged"/>
        </ItemTemplate>    
        </asp:TemplateField>
                                       
        <asp:TemplateField HeaderText="Product Name">
        <ItemTemplate>
        <asp:TextBox ID="txtProductName" runat="server"
                     Text='<%# Bind("ProductName") %>' ReadOnly="true"
                     ForeColor="Blue" BorderStyle="none"
                     BorderWidth="0px" >
        </asp:TextBox>
        </ItemTemplate>
        </asp:TemplateField>
        
        <asp:TemplateField HeaderText="Qty">
        <ItemTemplate>
        <asp:TextBox ID="txtQty" runat="server"                 
                     ForeColor="Blue" ReadOnly="true"
                     BorderStyle="none" BorderWidth="0px"  >
        </asp:TextBox>
        </ItemTemplate>
        </asp:TemplateField>
    </Columns>
    </asp:GridView>

    The above html markup is for an idea. 
    Please let me know, if you need to more information. 
    0