GridView and CheckBoxes in a TemplateField

Last post 04-18-2007 10:05 PM by Rex Lin - MSFT. 1 replies.

Sort Posts:

  • GridView and CheckBoxes in a TemplateField

    04-17-2007, 9:04 AM
    • Member
      point Member
    • WManger
    • Member since 04-17-2007, 12:27 PM
    • Posts 1

    Hello,

    I am trying to get the values of CheckBoxes in a GridView, but when i try this the CheckBox is always false.

    Here is the code.

     

    Private Sub lbUpdatePricing_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbUpdatePricing.Click
    For Each r As GridViewRow In gvProductBrowser.Rows
    Dim txtBox As New TextBox
    Dim chkBox As CheckBox = CType(r.FindControl("chkNoSale"), CheckBox)
    txtBox = CType(r.FindControl("txtNewPrice"), TextBox)
    Dim productID As Integer = CInt(gvProductBrowser.DataKeys(r.RowIndex).Value)
    If Not String.IsNullOrEmpty(txtBox.Text) And IsNumeric(txtBox.Text) And Not txtBox.Text = "0.00" Then
    Call
    UpdateProductPricing(productID, CDec(txtBox.Text))
    End If

    Call
    UpdateProductDisabled(productID, chkBox.Checked)

    Next
    Call
    LoadData()
    End Sub

      

    And on the front end the HTML looks like:

        <asp:GridView ID="gvProductBrowser" runat="server" 
    GridLines="None"
    AutoGenerateColumns="False"
    DataKeyNames="ProductID"
    CssClass="Grid"
    CellPadding="0"
    CellSpacing="0"
    BorderWidth="0"
    AllowPaging="false"
    AllowSorting="True"
    OnSorting="gvProductBrowser_Sorting"
    >
    <Columns>
    <asp:TemplateField ItemStyle-Width="15" ItemStyle-HorizontalAlign="center">
    <ItemTemplate>
    <asp:CheckBox ID="chkNoSale" runat="server" />
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField ItemStyle-Width="75">
    <HeaderTemplate>
    <asp:linkbutton runat="server" ID="SortSku" CommandName="sort" CommandArgument="Sku" Text="Sku"></asp:linkbutton>
    </HeaderTemplate>
    <ItemTemplate>
    <asp:HyperLink ID="hlSku" runat="server" Text='<%# Eval("Sku") %>' NavigateUrl='<%# GetNavigateUrl(Eval("Sku"))%>'></asp:HyperLink>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>

    I have read in some articles that there are problems with the GridView and CheckBoxes but have not been able to solve the problem.

    Any help would be appreciated,

    - Walter Manger 

  • Re: GridView and CheckBoxes in a TemplateField

    04-18-2007, 10:05 PM
    Answer

    Hi, Walter Manger : 

    Make sure you do not bind your GV on each postback in Page_Load() event.

    protected void Page_Load()
    {
             if(!IsPostBack)
                //Bind your GV

    }

    I hope the above information will be helpful. If you have any issues or concerns, please let me know. It's my pleasure to be of assistance

    Best Regards,
    __________________________________________________
    Sincerely,
    Rex Lin
    Microsoft Online Community Support

    If there is any question or the issue is not resolved, please feel free to mark the thread as not resolved
Page 1 of 1 (2 items)