Limit Selection to 5 Items in Asp DataList

Posted on 2014-01-16
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 33

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 33

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?
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.


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 500 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

730 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