Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Limit Selection to 5 Items in Asp DataList

Posted on 2014-01-16
Medium Priority
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">
                                      <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:LinkButton ID="LinkButton2" runat="server" CssClass="btn btn-link" Text = '<%# Eval("cuisinename") %>' CommandName ="Edit" ></asp:LinkButton>

Open in new window

Question by:misnstt
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 34

Expert Comment

by:Big Monty
ID: 39788547
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

Author Comment

ID: 39788598
Hello yes that is a great method however im not sure how to do that either .
LVL 34

Expert Comment

by:Big Monty
ID: 39788604
can you post the code for the button that does the selection as well as the save button?
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Author Comment

ID: 39789465
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)
            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())

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

            Panel2.Visible = False
        End Using

    End Sub
LVL 13

Accepted Solution

Jesus Rodriguez earned 2000 total points
ID: 39789734
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
        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

Open in new window


Author Closing Comment

ID: 39790847
Thank You

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

609 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