ComboBox on load demand and Getting SelectedValue from second comboBox

http://www.x2x1.com/show/8048425.aspx

Question

I’m trying to populate one Telerik AJAX radComboBox from the results of another i.e.

  1. comboBox1 – autocompletes and user selects an item
  2. comboBox2 – user selects. Loads on demand. Uses the selected value from comboBox1 to populate itself.

The problem is that I can’t get the selected value of combobox1

Markup

<telerik:RadComboBox ID="comboBox1" runat="server" 
                 EnableLoadOnDemand="True" 
                 MarkFirstMatch="False" 
                 onitemsrequested="comboBox1_ItemsRequested" >
            </telerik:RadComboBox>

   <telerik:RadComboBox ID="comboBox2" runat="server" 
                 EnableLoadOnDemand="True" 
                 MarkFirstMatch="False" 
                 onitemsrequested="comboBox2_ItemsRequested" >
            </telerik:RadComboBox>

C#

protected void comboBox1_ItemsRequested(object sender, Telerik.Web.UI.RadComboBoxItemsRequestedEventArgs e)
{
    //.. populate this combo
}

protected void comboBox2_ItemsRequested(object sender, Telerik.Web.UI.RadComboBoxItemsRequestedEventArgs e)
{
    string test = comboBox1.SelectedValue;
    //.. test is empty. Why?? 
}

Frustratingly I can’t get the selected value.The problem might be that the page isn’t actually posting back (must be part of the issue) so that selected value has no opportunity to be set. So I have written the code to get around this

Markup

   <telerik:RadComboBox ID="comboBox1" runat="server" 
                 EnableLoadOnDemand="True" 
                 MarkFirstMatch="False" 
                 onitemsrequested="comboBox1_ItemsRequested" 
  onclientselectedindexchanged="OnClientSelectedIndexChanged">
            </telerik:RadComboBox>
   <asp:HiddenField runat="server" ID="hidClientId" />

   <telerik:RadComboBox ID="comboBox2" runat="server" 
                 EnableLoadOnDemand="True" 
                 MarkFirstMatch="False" 

                 onitemsrequested="comboBox2_ItemsRequested" >
            </telerik:RadComboBox>

JQuery

function OnClientSelectedIndexChanged(sender, eventArgs) {

         var item = eventArgs.get_item();
        var value = item.get_value();
        $("[ID$='hidClientId']").val(value);
}

C#

protected void comboBox2_ItemsRequested(object sender, Telerik.Web.UI.RadComboBoxItemsRequestedEventArgs e)
{
    string test = hidClientId. Value;
    //.. test is empty. Why?? 
}

This would seem to me to have bypassed the postback issue but it still doesn’t work.

Does anyone know how to get the value of one radComboBox from another? Any help greatly appreciated


Resolution

The reason you cannot access the other controls on the page, is because the RadComboBox performs anAsync request for the items and as such the other controls on the page are not accessible.

Try handling the OnClientItemsRequesting event, making use of the context object (that is passed to the server-side code) to send the selected value of the first combo.

Markup

<telerik:RadCodeBlock ID="RadCodeBlock" runat="server">

    <script type="text/javascript">

        function OnClientItemsRequesting(sender, eventArgs) {

            var comboBox1 = $find('<%= comboBox1.ClientID %>');
            var value = comboBox1.get_value();

            var context = eventArgs.get_context();
            context["ComboBox1Value"] = value;
        }

    </script>

</telerik:RadCodeBlock>

<telerik:RadComboBox ID="comboBox1" runat="server" 
    MarkFirstMatch="False">
    <Items>
        <telerik:RadComboBoxItem Text="Item 1" Value="0" />
        <telerik:RadComboBoxItem Text="Item 2" Value="1" />
    </Items>
</telerik:RadComboBox>

<telerik:RadComboBox ID="comboBox2" runat="server" 
            EnableLoadOnDemand="True" 
            MarkFirstMatch="False" 
            onitemsrequested="comboBox2_ItemsRequested"
            OnClientItemsRequesting="OnClientItemsRequesting">
</telerik:RadComboBox>            

Code behind

protected void comboBox2_ItemsRequested(object sender, Telerik.Web.UI.RadComboBoxItemsRequestedEventArgs e)
{
    string selectedValue = e.Context["ComboBox1Value"].ToString();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值