开发者

asp .net checkbox inside gridview checked

开发者 https://www.devze.com 2023-03-18 09:34 出处:网络
I have a main \'Products\' table and a \'Products_Recommended\' table. I want the users to be able to select several products from a GridView using checkboxes and then insert those Product IDs (prodid

I have a main 'Products' table and a 'Products_Recommended' table. I want the users to be able to select several products from a GridView using checkboxes and then insert those Product IDs (prodid) into the Products_Recommended table in such a way that a main Product ID is entered (coming from query string) and potentially several recommended ProdIDs get entered. So far so good. But I need to be able to show the checkboxes to be checked if there were already prodids in the Products_Recommended table previously. The code below show a 'sqldatasource1' which gets the data from the Products_Recommended table based on query string. I just don't know how the checkboxes can get checked because the GridView has a different sqldatasource binding it. Thanks! Meengla

  <form id="form1" runat="server">
<asp:GridView ID="Products" runat="server" AutoGenerateColumns="False" DataKeyNames="prodid"
    DataSourceID="alldata" EnableModelValidation="True">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:CheckBox ID="ProductSelector" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField  DataField="itemnumber" HeaderText="Item 开发者_JAVA技巧Number" SortExpression="itemnumber" />

        <asp:BoundField DataField="itemtitle" HeaderText="itemtitle" SortExpression="itemtitle" />
    </Columns>
</asp:GridView>
<p>
    <asp:Button ID="SelectedProducts" runat="server" Text="Recommend" OnClick="SelectedProducts_Click" />
</p>
<p>
    <asp:Label ID="lblProdSelected" runat="server" EnableViewState="False" Visible="False"></asp:Label>
</p>
<asp:SqlDataSource ID="alldata" runat="server" ConnectionString="<%$ ConnectionStrings:dbconnection %>"
    SelectCommand="SELECT * FROM Products">
    <SelectParameters>
        <asp:QueryStringParameter DefaultValue="14" Name="itemid" QueryStringField="itemid"
            Type="Int32" />
    </SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:dbconnection %>"
    SelectCommand="SELECT * FROM dbo.products_recommended WHERE prodid = @itemid)">
    <SelectParameters>
        <asp:QueryStringParameter DefaultValue="14" Name="itemid" QueryStringField="itemid"
            Type="Int32" />
    </SelectParameters>
</asp:SqlDataSource>


Handle the RowDataBound event, and in the method find the checkbox and set its Checked value.

<asp:GridView ID="Products" OnRowDataBound="GridViewRowEventHandler">

void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
  if(e.Row.RowType == DataControlRowType.DataRow)
  {
    var ProductSelector = e.Row.FindControl("ProductSelector") as CheckBox;
    ProductSelector.Checked = true;
  }
}

You can use the Select method of DataSource to retrieve the data you need

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号