Avatar of Ed
Ed
Flag for United Kingdom of Great Britain and Northern Ireland asked on

Gridview Checkbox Select

I have a grid view that contains postcodes.

I want to be able to enable a use to select 2 postcodes(only) from the list to set as an a and b address.

I can select a row and access the postcode field no problems but i need to do the following.

Only allow the users to select 2 postcodes. One for A address and one for B address.

Once selected I would like to save as maybe a session variable so that I can pass to google maps which works out the distance between A and B.
ASP.NET.NET Programming

Avatar of undefined
Last Comment
Ryan Chong

8/22/2022 - Mon
Ryan Chong

can you illustrate and show how's your gridview layout looks like?
Ed

ASKER
<asp:GridView ID="gvAppointments" runat="server" AutoGenerateColumns="False" DataKeyNames="IGSaleID,AppointmentPostcode" DataSourceID="dsAppointmentsbyDate">
                                            <Columns>
                                                <asp:BoundField DataField="IGSaleID" HeaderText="IGSaleID" InsertVisible="False" ReadOnly="True" SortExpression="IGSaleID" />
                                                <asp:BoundField DataField="SaleID" HeaderText="SaleID" SortExpression="SaleID" />
                                                <asp:BoundField DataField="DateAdded" HeaderText="DateAdded" SortExpression="DateAdded" />
                                                <asp:BoundField DataField="AppointmentStartTime" HeaderText="AppointmentStartTime" SortExpression="AppointmentStartTime" />
                                                <asp:BoundField DataField="AppointmentEndTime" HeaderText="AppointmentEndTime" SortExpression="AppointmentEndTime" />
                                                <asp:BoundField DataField="HomePostCode" HeaderText="HomePostCode" SortExpression="HomePostCode" />
                                                <asp:BoundField DataField="AppointmentPostcode" HeaderText="AppointmentPostcode" SortExpression="AppointmentPostcode" />
                                                <asp:BoundField DataField="AppointmentSubject" HeaderText="AppointmentSubject" SortExpression="AppointmentSubject" />
                                                <asp:TemplateField HeaderText="Compare Distance">
                                                    <ItemTemplate>
                                                        <asp:CheckBox ID="ckPostcode" runat="server" OnCheckedChanged="ckPostcode_CheckedChanged" />
                                                    </ItemTemplate>
                                                </asp:TemplateField>
                                            </Columns>
                                        </asp:GridView>

Open in new window

Ryan Chong

you may do some verification like this:

protected void ckPostcode_CheckedChanged(object sender, EventArgs e)
        {
            List<String> selection = new List<string>();

            int checkedCnt = 0;
            foreach (GridViewRow row in gvAppointments.Rows)
            {
                CheckBox chk = (CheckBox)row.FindControl("ckPostcode");
                if (chk.Checked)
                {
                    //put IGSaleID into selection
                    selection.Add(row.Cells[0].Text);
                    checkedCnt++;
                }
            }
            if (checkedCnt > 2)
            {
                Response.Write("Please select only 2 items");
            }
            else if (checkedCnt <=1)
            {
                Response.Write("Please select items for comparison");
            }
        }

Open in new window


you may also add the AutoPostBack tag in your checkbox to:
<asp:CheckBox ID="ckPostcode" runat="server" OnCheckedChanged="ckPostcode_CheckedChanged" />

Open in new window


to:

<asp:CheckBox ID="ckPostcode" AutoPostBack="true" runat="server" OnCheckedChanged="ckPostcode_CheckedChanged" />

Open in new window

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Ed

ASKER
Thanks Ryan although I do have a problem with the postback clearing the checkbox selections.
Ed

ASKER
Just me being a tool, the post back thing is fine as I was data binding the grid-view on post back.

This now almost works as intended.  


The only issue is that the checkboxes are disabled on only below the rows of the 2 ticked items  

        Protected Sub ckPostcode_CheckedChanged(sender As Object, e As EventArgs)
            Dim selection As List(Of [String]) = New List(Of String)()

            Dim checkedCnt As Integer = 0
            For Each row As GridViewRow In gvAppointments.Rows
                Dim chk As CheckBox = DirectCast(row.FindControl("ckPostcode"), CheckBox)
                If chk.Checked Then
                    'put IGSaleID into selection
                    selection.Add(row.Cells(0).Text)
                    checkedCnt += 1

                End If

                If checkedCnt >= 2 Then
                    lbSelected.Text = "Too many items"
                    chk.Enabled = False


                ElseIf checkedCnt <= 1 Then
                    lbSelected.Text = "the right amount"
                    chk.Enabled = True
                End If
            Next


        End Sub

Open in new window

Ryan Chong

The only issue is that the checkboxes are disabled on only below the rows of the 2 ticked items
well.... is that mean you want to disable other checkboxes after 2 items were checked?

and... is that mean the user cannot make changes to his/her selection?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Ed

ASKER
well.... is that mean you want to disable other checkboxes after 2 items were checked?

Yes

and... is that mean the user cannot make changes to his/her selection?

Yes.


Thanks
ASKER CERTIFIED SOLUTION
Ryan Chong

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Ed

ASKER
Thanks, that works so I'll give the points for that.

 IS there a way I can set the 1st checked box as an ' A'  and the send as ' b'. The reason why I want to do this is that I want the first checkbox selected to go into textbox1  and the 2nd checkbox selected to go into textbox 2. Thanks
Ryan Chong

IS there a way I can set the 1st checked box as an ' A'  and the send as ' b'. The reason why I want to do this is that I want the first checkbox selected to go into textbox1  and the 2nd checkbox selected to go into textbox 2
is that mean you want to show "A" (or 1st selected checkbox's value ??) into first text box? and later do the same to show "B" (or 2nd selected checkbox's value ??)  into 2nd text box?
Your help has saved me hundreds of hours of internet surfing.
fblack61
Ryan Chong

any further comments before closing this question?
Ryan Chong

Suggested solution works

Asker can create another new thread for his/her new questions