5 Replies - 8428 Views - Last Post: 15 July 2011 - 03:35 PM Rate Topic: -----

#1 Moogoo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 82
  • Joined: 06-January 10

Checkbox not working in Gridview in UpdatePanel

Posted 14 July 2011 - 07:02 AM

Hello all,

I have a GridView, multiple ones actually, each row has a checkbox associated with it. What I'm trying to do is validate that when a user checks a box for a row that has a specific date and time, I will then check all other rows and if any other rows besides the one I checked has the same date and time, the checkbox will be disabled. I was testing it to see if it worked, and when I step through the debugger on my code side, when it gets to my checkbox, the debugger says that my checkbox is not checked. Anyone have any ideas? Here is my Gridview:

<asp:Panel ID="pnlSchedule" runat="server">
                <asp:UpdatePanel ID="updSessions" runat="server">
                    <ContentTemplate>
                    <asp:Label ID="lblDayOne" runat="server" CssClass="HeaderLabel" Text="Wednesday, September 27th"></asp:Label>
                    <br />
                    <br />                    
                    <asp:GridView ID="gvScheduleDayOne" runat="server" AllowPaging="False"  
                        AutoGenerateColumns="False" CssClass="GridViewStyle" GridLines="None"> 
                        <Columns>                                                         
                            <asp:BoundField DataField="intSessionID" HeaderText="Session ID" />
                            <asp:BoundField DataField="strSessionName" HeaderText="Session" />
                            <asp:BoundField DataField="dteSessionDay" HeaderText="Date" />
                            <asp:BoundField DataField="intDurationMinute" HeaderText="Duration" />
                            <asp:BoundField DataField="strDesignation" HeaderText="Designated For" />
                            <asp:TemplateField HeaderText="Select">
                                <ItemTemplate>
                                    <asp:CheckBox ID="chkSession" AutoPostBack="true" runat="server" />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:BoundField DataField="bitCanChoose" HeaderText="Selected" />                                                         
                        </Columns> 
                        <RowStyle CssClass="RowStyle" /> 
                        <EmptyDataRowStyle CssClass="EmptyRowStyle" /> 
                        <PagerStyle CssClass="PagerStyle" /> 
                        <SelectedRowStyle CssClass="SelectedRowStyle" /> 
                        <HeaderStyle CssClass="HeaderStyle" /> 
                        <EditRowStyle CssClass="EditRowStyle" /> 
                        <AlternatingRowStyle CssClass="AltRowStyle" /> 
                    </asp:GridView>
                    </ContentTemplate>
                    </asp:UpdatePanel>                    
                </asp:Panel> 



And here's the code I've entered so far. Also I am calling this function in the Page_Load function, I'll put that in as well.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Me.Page_ResetErrors()
        CheckScheduleTimes()

        If Not Me.IsPostBack Then
            Me.pnlSubmitted.Visible = False
            LoadFirstDaySchedules()
            LoadSecondDaySchedules()
            LoadThirdDaySchedules()
            LoadFourthDaySchedules()
        End If

    End Sub

Private Sub CheckScheduleTimes()
        Dim cb As CheckBox
        Dim cb2 As CheckBox
        For Each row As GridViewRow In Me.gvScheduleDayOne.Rows
            cb = row.Cells(5).Controls(1)
            If cb.Checked Then
                For Each otherrow As GridViewRow In Me.gvScheduleDayOne.Rows
                    cb2 = otherrow.Cells(5).Controls(1)
                    If otherrow.Cells(2).Text = row.Cells(2).Text Then
                        If Not cb2.Checked Then
                            cb2.Enabled = False
                        End If
                    End If
                Next
            End If
        Next

    End Sub



Is This A Good Question/Topic? 0
  • +

Replies To: Checkbox not working in Gridview in UpdatePanel

#2 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 445
  • View blog
  • Posts: 1,501
  • Joined: 28-April 09

Re: Checkbox not working in Gridview in UpdatePanel

Posted 14 July 2011 - 07:55 AM

you should probably use either the prerender event or the rowdatabound event of the gridview.
Was This Post Helpful? 0
  • +
  • -

#3 Moogoo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 82
  • Joined: 06-January 10

Re: Checkbox not working in Gridview in UpdatePanel

Posted 14 July 2011 - 09:23 AM

I tried using the RowDataBound event and I get the same results. You mentioned using the PreRender event, which I have never used. I looked it up on MSDN, but not sure how or where to implement that event. Any help on that method?
Was This Post Helpful? 0
  • +
  • -

#4 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 445
  • View blog
  • Posts: 1,501
  • Joined: 28-April 09

Re: Checkbox not working in Gridview in UpdatePanel

Posted 14 July 2011 - 10:58 AM

The gridview has an OnPreRender property
<asp:GridView ID="gridview1" runat="server" OnPreRender="gridview1_PreRender">
...
</asp:GridView>



and in your code behind you would define the method

protected void gridview1_PreRender(object sender, EventArgs e)
{

}



You could also go into design mode, select the gridview and then in the properties box select the lightning bolt to view the gridview events. Double click on the PreRender event and visual studio will automatically create the PreRender method and assign it to the OnPreRender property of the gridview.

The RowDataBound is best to use when you are wanting to perform an action on each individual row and not so much if you're needing to loop through existing rows for some reason. If the date was coming from outside of the GridView then you the RowDataBound event would be the best choice since you could just get the value from the current row, compare it to the date and make the changes for that row. No loops would be needed.

The PreRender event is the last step before the GridView is rendered to the page so all of the rows, and data in the rows, have been loaded and should be available to do your loop operation on.
Was This Post Helpful? 1
  • +
  • -

#5 Moogoo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 82
  • Joined: 06-January 10

Re: Checkbox not working in Gridview in UpdatePanel

Posted 15 July 2011 - 05:17 AM

PreRender works!! Thank you so much for helping me on this topic. I told my boss that I was going to give up on this and just validate all the checkboxes upon submit, but you saved that application! I suppose I owe you a portion of my paycheck now? :smile2:
Was This Post Helpful? 0
  • +
  • -

#6 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 445
  • View blog
  • Posts: 1,501
  • Joined: 28-April 09

Re: Checkbox not working in Gridview in UpdatePanel

Posted 15 July 2011 - 03:35 PM

Glad it worked for you :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1