This is a migrated thread and some comments may be shown as answers.

ComboBox selectedindexchanged within asp repeater

7 Answers 300 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Web Services
Top achievements
Rank 2
Web Services asked on 02 Oct 2009, 03:51 PM
I have a radcombobox that runs some code when the user starts typing in numbers to pull relevant part numbers. Once a user selects a number I need to populate some asp labels. The problem is, these combobox and labels are in an asp repeater because the user can essentially keep adding parts. To see what it does you can go here. http://devpartsonline.intellicomweb.com/test.aspx
Basically when the users selects a number to go into the radcombobox I need the two labels (right now say test) to update from the database with the price and part name of that number. I need to run serverside code and update those labels but I can't figure out how to get the rad combo box to execute code since it is in a repeater. I really have no idea how to get started on this. Here is my aspx and vb just for a reference. Thanks,

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="bulkOrder.aspx.vb" Inherits="devOrthman.bulkOrder"
    MasterPageFile="~/MasterPage.Master" Title="Bulk Ordering" %>

<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

    <script language="javascript" type="text/javascript">
function OnClientItemsRequested(sender, eventArgs)
 {
   if(sender.get_items().get_count() == 0)
   {
    sender.clearSelection();
    sender.hideDropDown();
    
   }
 }
    </script>

    <div class="ContentPadding">
        <asp:UpdatePanel ID="Panel1" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                <span class="H2Internal">Bulk Ordering</span>
                <p>
                    <asp:Repeater ID="Repeater1" runat="server">
                        <HeaderTemplate>
                            <table cellspacing="5">
                        </HeaderTemplate>
                        <ItemTemplate>
                            <tr valign="middle">
                                <td width="80px">
                                    
                                    <asp:Button ID="AddButton" runat="server" Text="Add" CommandName="Add" CausesValidation="True" ValidationGroup ="bulkValidation" />
                                    <asp:Button ID="RemoveBtn" runat="server" Text="Delete" CommandName="Remove" CausesValidation="false"
                                        Visible="false" />
                                </td>
                                <td width="175px">
                                    <telerik:RadComboBox ID="RadSearchComboBox" runat="server" Width="150px" Height="175px"  ValidationGroup ="bulkValidation"
                                        OnSelectedIndexChanged="itemChanged"
                                        AllowCustomText="True" ShowToggleImage="False"
                                        ShowMoreResultsBox="true" EnableLoadOnDemand="True" Skin="Telerik" MarkFirstMatch="True"
                                        OnItemsRequested="loadSearch" EnableVirtualScrolling="true" EmptyMessage="Enter Part Number"
                                        ErrorMessage="Value not Found" AutoPostBack="False" OnClientItemsRequested="OnClientItemsRequested">
                                        <CollapseAnimation Duration="100" Type="OutQuint" />
                                    </telerik:RadComboBox>
                                    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="*"  ValidationGroup ="bulkValidation"
                                        ControlToValidate="RadSearchComboBox" />
                                </td>
                                <td width="45px">
                                    <telerik:RadNumericTextBox ID="QtyTextBox" runat="server" MaxLength="3" MaxValue="999"  ValidationGroup ="bulkValidation"
                                        MinValue="1" NumberFormat-DecimalDigits="0" ShowSpinButtons="false" Type="Number"
                                         Width="20px" />
                                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*"  ValidationGroup ="bulkValidation"
                                        ControlToValidate="QtyTextBox" />
                                </td>
                                <td class="H2Internal">
                                    Part Name: <asp:Label CssClass ="MainText" id="partName" runat="server" Text="test"></asp:Label>&nbsp;&nbsp;&nbsp;
                                    Price: <asp:Label CssClass ="MainText" id="price" runat="server" Text="test"></asp:Label>
                                </td>
                            </tr>
                        </ItemTemplate>
                        <FooterTemplate>
                            </table>
                        </FooterTemplate>
                    </asp:Repeater>
                </p>
                <p>
                    <asp:Button ID="Button1" runat="server" Text="All Items to Cart" CausesValidation="True"  ValidationGroup ="bulkValidation"
                        OnClick="addToCart"></asp:Button>
                </p>
                <p>
                    <asp:Label ID="msgLabel" runat="server" Text=""></asp:Label>
                </p>
            </ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
            </Triggers>
        </asp:UpdatePanel>
    </div>
</asp:Content>
----------------------------------------------------------------------------------------------------------------
VB Code
----------------------------------------------------------------------------------------------------------------
Imports Telerik.Web.UI
Imports System.Data.SqlClient
Imports System.Net.Mail
Partial Public Class bulkOrder
    Inherits System.Web.UI.Page
    Shared allItems As ArrayList

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            allItems = New ArrayList
            Me.bulkItems = allItems
            Me.bulkItems.Add(New bulkOrderItem())
            Me.Repeater1.DataSource = bulkItems
            Me.Repeater1.DataBind()
        Else
            storeList()
        End If
    End Sub
    Protected Sub loadSearch(ByVal sender As Object, ByVal e As RadComboBoxItemsRequestedEventArgs)
        Dim connection As New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("orthmanConnectionString").ToString)
        Dim selectCommand As New SqlCommand("searchByPartNumber", connection)

        selectCommand.Parameters.AddWithValue("partNumber", e.Text)
        selectCommand.CommandType = CommandType.StoredProcedure
        Dim adapter As New SqlDataAdapter(selectCommand)
        Dim data As New DataTable()
        adapter.Fill(data)
        Try

            Dim itemsPerRequest As Integer = 10
            Dim itemOffset As Integer = e.NumberOfItems
            Dim endOffset As Integer = itemOffset + itemsPerRequest
            If endOffset > data.Rows.Count Then
                endOffset = data.Rows.Count
            End If

            If endOffset = data.Rows.Count Then
                e.EndOfItems = True
            End If
            Dim i As Integer = itemOffset
            While i < endOffset
                CType(sender, RadComboBox).Items.Add(New RadComboBoxItem(data.Rows(i)("PartNumber").ToString(), data.Rows(i)("PartId").ToString()))
                i = i + 1
            End While

            If data.Rows.Count > 0 Then
                e.Message = [String].Format("Items <b>1</b>-<b>{0}</b> out of <b>{1}</b>", endOffset.ToString(), data.Rows.Count.ToString())
            Else
                e.Message = "No Matches"
            End If
        Catch
            e.Message = "No Matches"
        Finally
            adapter.Dispose()
        End Try
    End Sub
    'Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAdd.Click
    '    Me.bulkItems.Add(New bulkOrderItem())
    '    Me.Repeater1.DataSource = bulkItems
    '    Me.Repeater1.DataBind()

    'End Sub
    Protected Sub rptrDatabound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs) Handles Repeater1.ItemDataBound
        Dim index As Integer = e.Item.ItemIndex
        If index < bulkItems.Count And index > -1 Then
            Dim bulkItem As bulkOrderItem
            Dim txtPart As RadComboBox
            Dim qtyPart As Telerik.Web.UI.RadNumericTextBox
            txtPart = CType(e.Item.FindControl("RadSearchComboBox"), RadComboBox)
            qtyPart = CType(e.Item.FindControl("QtyTextBox"), Telerik.Web.UI.RadNumericTextBox)
            bulkItem = CType(bulkItems.Item(index), bulkOrderItem)
            txtPart.Text = bulkItem.itemNumber
            qtyPart.Value = bulkItem.itemQty
            e.Item.FindControl("AddButton").Visible = Not bulkItem.canDelete
            e.Item.FindControl("RemoveBtn").Visible = bulkItem.canDelete
        End If
    End Sub
    Protected Sub storeList()
        Dim index As Integer = 0
        Dim bulkItem As bulkOrderItem
        For Each rptrItem As RepeaterItem In Repeater1.Items
            Dim txtPart As RadComboBox
            Dim qtyPart As Telerik.Web.UI.RadNumericTextBox
            txtPart = CType(rptrItem.FindControl("RadSearchComboBox"), RadComboBox)
            qtyPart = CType(rptrItem.FindControl("QtyTextBox"), RadNumericTextBox)
            bulkItem = CType(bulkItems.Item(index), bulkOrderItem)
            bulkItem.itemNumber = txtPart.Text
            bulkItem.itemQty = qtyPart.Value
            bulkItem.canDelete = Not (rptrItem.FindControl("AddButton").Visible)
            index = index + 1
        Next

    End Sub
    'Protected Sub emailAdministrator(ByVal OrderId As String)
    '    Dim msgBody As String
    '    msgBody = "A new order has been placed." & vbCrLf
    '    msgBody &= " Order ID: " & OrderId & vbCrLf
    '    msgBody &= "Please click on the following link to view the details of the order:" & vbCrLf
    '    msgBody &= "http://orthman.intellicomweb.com/admin/orders/orderDetails.aspx?ID=" & OrderId

    '    Dim msgTo As String = System.Configuration.ConfigurationManager.AppSettings("sentToAdmin")
    '    Dim msgFrom As String = System.Configuration.ConfigurationManager.AppSettings("sendfrom")
    '    Dim msgSubject As String = "Orthman Online Catalog - New Order Placed"
    '    Try
    '        Dim checkoutMail As New MailMessage(msgFrom, msgTo, msgSubject, msgBody)
    '        Dim mailClient As New SmtpClient()
    '        mailClient.Host = "inteexchange01"
    '        checkoutMail.IsBodyHtml = False
    '        mailClient.Send(checkoutMail)
    '    Catch err As Exception
    '        msgLabel.Text = "An error ocurred, please try again later. <BR/>" & err.ToString
    '    End Try
    'End Sub

    Protected Sub itemChanged(ByVal o As Object, ByVal e As Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs)
        Response.Write("test")
    End Sub
    Protected Sub rptrCommand(ByVal sender As Object, ByVal e As RepeaterCommandEventArgs) Handles Repeater1.ItemCommand
        Dim index As Integer
        index = e.Item.ItemIndex
        If e.CommandName = "Remove" Then
            bulkItems.RemoveAt(index)
            Me.Repeater1.DataSource = bulkItems
            Me.Repeater1.DataBind()

        ElseIf e.CommandName = "Add" Then
            Page.Validate()
            If Page.IsValid() Then
                Me.bulkItems.Add(New bulkOrderItem())
                Me.Repeater1.DataSource = bulkItems
                Me.Repeater1.DataBind()
                Me.Repeater1.Items(index).FindControl("AddButton").Visible = False
                Me.Repeater1.Items(index).FindControl("RemoveBtn").Visible = True

            End If
            Panel1.Update()
        End If
    End Sub
    Protected Sub addToCart(ByVal sender As Object, ByVal e As System.EventArgs)

        Page.Validate()
        If Page.IsValid() Then
            Dim bulkItem As bulkOrderItem
            storeList()
            Dim i As Integer = 0
            While i < bulkItems.Count
                bulkItem = CType(bulkItems.Item(i), bulkOrderItem)
                storeInDB(bulkItem.Number, bulkItem.Qty)
                i = i + 1
            End While
            Response.Redirect("~/cart/viewcart.aspx")
        End If
    End Sub
    Private Property bulkItems() As IList
        Get
            Return CType(ViewState("allItems"), ArrayList)
        End Get
        Set(ByVal value As IList)
            ViewState("allItems") = value
        End Set
    End Property
    Protected Sub storeInDB(ByVal partNumber As String, ByVal qty As Integer)
        Dim connString As String = System.Configuration.ConfigurationManager.ConnectionStrings("orthmanConnectionString").ToString
        Dim myConnection As New Data.SqlClient.SqlConnection(connString)
        Dim strSQL = "addPartToCartByNumber"
        Dim insertCommand As New Data.SqlClient.SqlCommand(strSQL, myConnection)
        Dim PartIdParameter As New SqlClient.SqlParameter("@PartNumber", SqlDbType.NVarChar)
        Dim qtyParameter As New SqlClient.SqlParameter("@Quantity", SqlDbType.Int)
        Dim userNameParameter As New SqlClient.SqlParameter("@UserName", SqlDbType.NVarChar)

        insertCommand.CommandType = CommandType.StoredProcedure

        PartIdParameter.Value = partNumber
        qtyParameter.Value = qty
        userNameParameter.Value = User.Identity.Name

        insertCommand.Parameters.Add(PartIdParameter)
        insertCommand.Parameters.Add(qtyParameter)
        insertCommand.Parameters.Add(userNameParameter)
        Try
            myConnection.Open()
            insertCommand.ExecuteNonQuery()
        Catch ex As Exception
            'msgLabel.Text = ex.ToString()
        Finally
            myConnection.Close()
        End Try
    End Sub

End Class

7 Answers, 1 is accepted

Sort by
0
Web Services
Top achievements
Rank 2
answered on 06 Oct 2009, 02:33 PM
Does anyone have any suggestions on this, I'm still kind of lost.
0
Vesko
Top achievements
Rank 2
answered on 07 Oct 2009, 02:26 PM
Take a look at this forum post - it might give you an idea how to start:

ComboBox in Repeater - javascript


0
Web Services
Top achievements
Rank 2
answered on 07 Oct 2009, 08:11 PM
I don't see how that would work for my instance. If you go to the link I provided you can see what my page looks like and what I mean. Do you have any other suggestions.
0
Web Services
Top achievements
Rank 2
answered on 12 Oct 2009, 06:52 PM
I submitted a support ticket.
0
Veselin Vasilev
Telerik team
answered on 13 Oct 2009, 09:40 AM
Hello,

I have already replied to your ticket:

You need to find the labels in the itemChanged (your handler of the SelectedIndexChanged server event) and update them.
Here is a sample code:

<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
    <ItemTemplate>
      <telerik:RadComboBox ID="RadComboBox1"
        runat="server"
        AutoPostBack="true"
        onselectedindexchanged="RadComboBox1_SelectedIndexChanged">
        <Items>
            <telerik:RadComboBoxItem runat="server" Text="RadComboBoxItem1"
                Value="RadComboBoxItem1" />
            <telerik:RadComboBoxItem runat="server" Text="RadComboBoxItem2"
                Value="RadComboBoxItem2" />
            <telerik:RadComboBoxItem runat="server" Text="RadComboBoxItem3"
                Value="RadComboBoxItem3" />
            <telerik:RadComboBoxItem runat="server" Text="RadComboBoxItem4"
                Value="RadComboBoxItem4" />
            <telerik:RadComboBoxItem runat="server" Text="RadComboBoxItem5"
                Value="RadComboBoxItem5" />
            <telerik:RadComboBoxItem runat="server" Text="RadComboBoxItem6"
                Value="RadComboBoxItem6" />
            </Items>           
       </telerik:RadComboBox>
       <asp:Label ID="lbl1" runat="server"></asp:Label>
    </ItemTemplate>
</asp:Repeater>

C#

protected void RadComboBox1_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
{
    Label lbl = (o as Telerik.Web.UI.RadComboBox).NamingContainer.FindControl("lbl1") as Label;
    lbl.Text = e.Text;
}

converted to VB:

Dim lbl As Label = TryCast((TryCast(o, Telerik.Web.UI.RadComboBox)).NamingContainer.FindControl("lbl1"), Label)
lbl.Text = e.Text

I hope this helps.


Best wishes,
Veselin Vasilev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Web Services
Top achievements
Rank 2
answered on 13 Oct 2009, 02:41 PM
That was it thanks,
0
vivek
Top achievements
Rank 1
answered on 04 Nov 2010, 06:54 AM
Hi ,
I am doing the work with the help of wizard given
http://demos.telerik.com/aspnet-ajax/tabstrip/examples/applicationscenarios/wizard/defaultvb.aspx

There are several dropdown in page one
1. Master ddl
2. ddl1
3.ddl2
4.ddl3
I need 3-4 dropdown that need to change after the selection of master dropdown.Now while moving from NEXT to PREVIOUS all the controls withing update panel become disappear.Please help me to solve

Thanks in Advance.
Tags
ComboBox
Asked by
Web Services
Top achievements
Rank 2
Answers by
Web Services
Top achievements
Rank 2
Vesko
Top achievements
Rank 2
Veselin Vasilev
Telerik team
vivek
Top achievements
Rank 1
Share this question
or