Solved

Limit Selection to 5 Items in Asp DataList

Posted on 2014-01-16
6
667 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
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
0
 

Author Comment

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

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?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:misnstt
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)
            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
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
        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
ID: 39790847
Thank You
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article discusses how to create an extensible mechanism for linked drop downs.
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

911 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

16 Experts available now in Live!

Get 1:1 Help Now