Link to home
Start Free TrialLog in
Avatar of misnstt
misnstt

asked on

Limit Selection to 5 Items in Asp DataList

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

Avatar of Big Monty
Big Monty
Flag of United States of America image

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
Avatar of misnstt
misnstt

ASKER

Hello yes that is a great method however im not sure how to do that either .
can you post the code for the button that does the selection as well as the save button?
Avatar of misnstt

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Jesus Rodriguez
Jesus Rodriguez
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of misnstt

ASKER

Thank You