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

get selected value and selected text in clint side from combobox

1 Answer 173 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Ahmed
Top achievements
Rank 1
Ahmed asked on 15 Jul 2014, 01:23 PM
hi
i have combo box i use it in  user control and this is code for it
code of  all user control

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="DirectSales.ascx.cs"
    Inherits="SallingSystem.GUI.Layer.DirectSales1" %>
<br />
    <script type="text/javascript">
        function pageLoad() {
        }
        ///*********************************************************************************///
        function OnClientDropDownClosed(sender, args) {
            var radcombobox = $find('<%=RedComboItem.ClientID %>').get_text();
                alert(radcombobox);
            }
        ///*********************************************************************************///
    </script>
<div id="DirectSalesControls" runat="server">
    <telerik:RadComboBox ID="RadComboTreasury" runat="server" Width="100px" DataSourceID="ODSTreasury"
        DataTextField="Name" DataValueField="ID" MarkFirstMatch="True" ResolvedRenderMode="Classic">
    </telerik:RadComboBox>
    <telerik:RadComboBox ID="RadComboSaler" runat="server" Width="150px" DataSourceID="ODSSaler"
        DataTextField="Name" DataValueField="ID" MarkFirstMatch="True" ResolvedRenderMode="Classic">
    </telerik:RadComboBox>
    <telerik:RadComboBox ID="RedComboItem" runat="server" AppendDataBoundItems="True"
        Width="545px" HighlightTemplatedItems="true" DropDownWidth="565px" EmptyMessage="Choose a Product"
        DataValueField="Value" DataTextField="Text"
        OnClientDropDownClosing="OnClientDropDownClosed" oninit="RedComboItem_Init" >
        <HeaderTemplate>
            <table cellspacing="0" cellpadding="0">
                <tr>
                    <td id="Td1" runat="server" style="width: 100px;">
                        Product Price
                    </td>
                    <td id="Td2" runat="server" style="width: 100px;">
                        Product Code
                    </td>
                    <td id="Td3" runat="server" style="width: 330px;">
                        Product Name
                    </td>
                </tr>
            </table>
        </HeaderTemplate>
        <ItemTemplate>
            <table cellspacing="0" cellpadding="0">
                <tr>
                        <td style="width: 100px;">
                            <asp:Label ID="lblProductPrice" runat="server"><%# DataBinder.Eval(Container, "Text")%></asp:Label>
                        </td>
                        <td style="width: 100px;">
                            <asp:Label ID="lblproductCode" runat="server"><%# DataBinder.Eval(Container, "Attributes['ProductCode']")%></asp:Label>
                        </td>
                        <td style="width: 330px;">
                            <asp:Label ID="lblProductName" runat="server"><%# DataBinder.Eval(Container, "Attributes['ProductName']")%></asp:Label>
                        </td>
                </tr>
            </table>
        </ItemTemplate>
    </telerik:RadComboBox>
    <telerik:RadNumericTextBox ID="RadNumTextIncrease" runat="server" Width="120" Label="Increase">
    </telerik:RadNumericTextBox>
    <telerik:RadNumericTextBox ID="RadNumTextDiscount" runat="server" Width="150" Label="Discount">
    </telerik:RadNumericTextBox>
    <telerik:RadNumericTextBox ID="RadNumTextTotale" runat="server" Width="110" Label="Totale">
    </telerik:RadNumericTextBox>
    <telerik:RadButton ID="RadBtnSave" runat="server" Text="Save" OnClick="RadBtnSave_Click">
    </telerik:RadButton>
    <br />
    <asp:ObjectDataSource ID="ODSTreasury" runat="server" SelectMethod="GetTreasuryDetails"
        TypeName="SallingSystem.DataBussinessLayer.TreasuryDBL"></asp:ObjectDataSource>
    <asp:ObjectDataSource ID="ODSSaler" runat="server" SelectMethod="GetDirectSalerDetails"
        TypeName="SallingSystem.DataBussinessLayer.DirectSalerDBL"></asp:ObjectDataSource>
</div>

and this is code behind for it too

protected void RadBtnSave_Click(object sender, EventArgs e)
        {
            // check if user not leave the field of item free
            if (RedComboItem.SelectedValue != "" && RedComboItem.Text != "Choose a Product" && RadNumTextTotale.Text != null && RadComboTreasury.SelectedValue != null && RadComboSaler.SelectedValue != null)
            {
                var objDirectSalesOperation = new DirectSalesOperation();
                objDirectSalesOperation.UserID = Convert.ToInt32(Session["LoginUserID"]);
                objDirectSalesOperation.TreasurID = Convert.ToInt32(RadComboTreasury.SelectedValue);
                objDirectSalesOperation.SalerID = Convert.ToInt32(RadComboSaler.SelectedValue);
                objDirectSalesOperation.ItemID = Convert.ToInt32(RedComboItem.SelectedValue);
                objDirectSalesOperation.TotalePrice = Convert.ToInt32(RadNumTextTotale.Text);
                var objDirectSalesOperationDAl = new DirectSalesOperationDAL();
                var Result = objDirectSalesOperationDAl.InsertNewSalesOperation(objDirectSalesOperation);
                if (Result >= 1)
                {
                    // inserted right
                    // hide the control who i use in insert new sales order
                    DirectSalesControls.Style.Add("display", "none");
                }
                else
                {
                    // not inserted
                    
                }
            }
            else
            {
              // some data mast inserted not inserted  
 
            }
        }

        protected void RedComboItem_Init(object sender, EventArgs e)
        {
            // create object from IteamDBL
            IteamDBL objIreamDBL = new IteamDBL();
            // create list of Iteam object
            List<Iteam> lstIteam = new List<Iteam>();
            // call method who get all iteam details
            lstIteam = objIreamDBL.GetIteamDetails();
            // create loop to add the list of object to cobobox
            foreach (var objiteam in lstIteam)
            {
                // create object of comboBoxIteam
                RadComboBoxItem comIteam = new RadComboBoxItem();
                /// give every item of object created value text and and more attribute more
                /// in comboBox i created
                comIteam.Value = objiteam.ID.ToString();
                comIteam.Text = Convert.ToString(objiteam.SalesPrice);
                comIteam.Attributes.Add("ProductCode", objiteam.CodeNumber);
                comIteam.Attributes.Add("ProductName", objiteam.Name.ToString());
                // if i want get all data as value in code behind
                //comIteam.Value += ":" + objiteam.Name;
                // add the object of combo item i create to the comboBox i want in page
                RedComboItem.Items.Add(comIteam);
                // bind the data after i add all value i want in item i create of combo box item
                comIteam.DataBind();
            }
        }

and in clinte side i need to access the value and text of item combo box
i try all what i find in site like (get_value , get_text) but it is not work with me get the empty massage or get null
so i need t know h i can access the combo box text and value in client side 

thanks
Ahmed Salem
 

1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 1
answered on 16 Jul 2014, 05:33 AM
Hi Ahmed,

Please try the below sample code snippet which works fine at my end.

ASCX:
<telerik:RadComboBox ID="RedComboItem" runat="server" AppendDataBoundItems="True"
    DataSourceID="sqldsOrderDetails" Width="545px" HighlightTemplatedItems="true"
    DropDownWidth="565px" EmptyMessage="Choose a Product" DataValueField="OrderID"
    DataTextField="ProductID" OnClientDropDownClosing="OnClientDropDownClosed">
    <HeaderTemplate>
        <table cellspacing="0" cellpadding="0">
            <tr>
                <td id="Td1" runat="server" style="width: 100px;">
                    OrderID
                </td>
                <td id="Td2" runat="server" style="width: 100px;">
                    ProductID
                </td>
            </tr>
        </table>
    </HeaderTemplate>
    <ItemTemplate>
        <table cellspacing="0" cellpadding="0">
            <tr>
                <td style="width: 100px;">
                    <asp:Label ID="lblProductPrice" runat="server"><%# DataBinder.Eval(Container.DataItem, "OrderID")%></asp:Label>
                </td>
                <td style="width: 100px;">
                    <asp:Label ID="lblproductCode" runat="server"><%# DataBinder.Eval(Container.DataItem, "ProductID")%></asp:Label>
                </td>
            </tr>
        </table>
    </ItemTemplate>
</telerik:RadComboBox>

JavaScript:
function OnClientDropDownClosed(sender, args) {
    var radcombobox = $find('<%=RedComboItem.ClientID %>');
    alert("Text " + radcombobox.get_text());
    alert("Value " + radcombobox.get_value());
}

Thanks,
Shinu.
Tags
ComboBox
Asked by
Ahmed
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 1
Share this question
or