Telerik AJAX radComboBox. Getting SelectedValue from second comboBox

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-24 00:22:03

问题


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


回答1:


The reason you cannot access the other controls on the page, is because the RadComboBox performs an Async 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();
}

Hope this helps.



来源:https://stackoverflow.com/questions/8048425/telerik-ajax-radcombobox-getting-selectedvalue-from-second-combobox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!