Solved

Limit Selection to 5 Items in Asp DataList

Posted on 2014-01-16
6
640 Views
Last Modified: 2014-01-18
I have a ASP DataList which allows the user to select a item then once selected there is a second button to insert it into the Database.
I want to limit the user to only be able to select up to 5 items that re displayed in the DataList.  I don't know how to do this.  Please Help.  Thanks.


<asp:DataList ID="DataList3" runat="server" OnEditCommand="DataList3_EditCommand" DataKeyField="cuisineid" DataSourceID="SqlDataSource1" RepeatColumns="7" RepeatDirection="Horizontal" GridLines="Vertical">
                                  <ItemTemplate>
                                      <asp:Panel ID="Panel1" runat="server" Visible="False">
                                       <asp:Label ID="cuisinenameLabel" runat="server" Text='<%# Eval("cuisinename") %>' />
                                      <asp:Label ID="imageLabel" runat="server" Text='<%# Eval("image") %>' />
                                      </asp:Panel>
                                     <asp:LinkButton ID="LinkButton2" runat="server" CssClass="btn btn-link" Text = '<%# Eval("cuisinename") %>' CommandName ="Edit" ></asp:LinkButton>
                                </ItemTemplate>
                                 </asp:DataList>

Open in new window

0
Comment
Question by:misnstt
  • 3
  • 2
6 Comments
 
LVL 32

Expert Comment

by:Big Monty
Comment Utility
the button that does the inserting into the database should keep a running count of how many items have been selected, then when it reaches 5 (it would do the check every time the button that does the selection is pressed) it can either disable itself or display a message that max amount of items have been selected
0
 

Author Comment

by:misnstt
Comment Utility
Hello yes that is a great method however im not sure how to do that either .
0
 
LVL 32

Expert Comment

by:Big Monty
Comment Utility
can you post the code for the button that does the selection as well as the save button?
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:misnstt
Comment Utility
Hello here is the Select Button:
<asp:LinkButton ID="LinkButton2" runat="server" CssClass="btn btn-link" Text = '<%# Eval("cuisinename") %>' CommandName ="Edit" ></asp:LinkButton>

Here is the Insert Button Code:

Protected Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim SQL As String = ""

        ' Insert a new record
        Dim connectionString As String = ConfigurationManager.ConnectionStrings("dbMyCMSConnectionString").ConnectionString
        Dim insertSql As String = "INSERT INTO [chefscuisine] ([UserId], [cuisinetype], [image]) VALUES (@UserId, @cuisinetype, @image)"

        Using myConnection As New SqlConnection(connectionString)
            myConnection.Open()
            Dim myCommand As New SqlCommand(insertSql, myConnection)
            myCommand.Parameters.AddWithValue("UserId", UserId.Text.Trim())
            myCommand.Parameters.AddWithValue("@cuisinetype", TextBox2.Text.Trim())
            myCommand.Parameters.AddWithValue("@image", TextBox3.Text.Trim())
            myCommand.ExecuteNonQuery()
            myConnection.Close()

            'UserId.Text = String.Empty
            TextBox2.Text = String.Empty
            TextBox3.Text = String.Empty

            DataList2.DataBind()
            'SqlDataSource2.DataBind()
            Panel2.Visible = False
        End Using

    End Sub
0
 
LVL 13

Accepted Solution

by:
Jesus Rodriguez earned 500 total points
Comment Utility
I do and example for you with Something a little similar (With checkboxes )

On the CheckBox1_CheckedChanged of the template for the DatalistItem I put this code
 Dim ChkCurrent As CheckBox = TryCast(sender, CheckBox)
        Dim CCk As Integer = 0
        For Each Item As DataListItem In DataList1.Items
            Dim Chk As CheckBox = TryCast(Item.FindControl("CheckBox1"), CheckBox)
            If Chk.Checked Then
                CCk += 1
            End If
        Next
        If CCk > 5 Then
            ChkCurrent.Checked = False
        End If

Open in new window


And for insert Do this on the procedure
 For Each Item As DataListItem In DataList1.Items
            Dim Chk As CheckBox = TryCast(Item.FindControl("CheckBox1"), CheckBox)
            If Chk.Checked Then
                'Qry to insert the values
            End If
        Next

Open in new window

TRadeM.aspx
TRadeM.aspx.vb
0
 

Author Closing Comment

by:misnstt
Comment Utility
Thank You
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
This article discusses how to create an extensible mechanism for linked drop downs.
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now