使用jquery时未在服务器端获取客户端值

时间:2014-11-03 05:18:21

标签: jquery asp.net telerik radcombobox

我想在我的应用程序中避免回发。我正在使用telerik radcombobox。当用户从radcombo框中选择值时,所选值应出现在服务器端代码中,以便为另一个radcombo框进行数据绑定。我在telerik radcombo框下拉列表中显示值的客户端代码是:

    <input type="hidden" id="tCodh" runat="server" />
    <telerik:RadComboBox ID="tCod" runat="server" style="width: 185px;" OnClientSelectedIndexChanged="OnClientSelectedIndexChanged" >
    <DefaultItem Text="Select" Value="" />
        <Items>                                                
            <telerik:RadComboBoxItem Text="Description" Value="Description" />
            <telerik:RadComboBoxItem Text="Account" Value="Account" />
            <telerik:RadComboBoxItem Text="Price" Value="Price" />
            <telerik:RadComboBoxItem Text="Quantity" Value="Quantity" />                                              
        </Items>
     </telerik:RadComboBox>

       function OnClientSelectedIndexChanged(sender, eventArgs) {            
               var item = eventArgs.get_item();           
                document.getElementById('<%=tCod.ClientID%>').value = item.get_value();                  
            }

这里我使用隐藏字段来存储用户在radcombo框中选择的值。隐藏字段上收到的值将在服务器端发送。 将值绑定到serverside变量的代码是......

   public  List<TelerikCombo> TCombo_Values()
   {
       string value =   tCodh.Value;        
   }

    But selected value is not shown. Instead error, object reference is not set to instance of an object is shown. Any help?
    Thanks in advance.

1 个答案:

答案 0 :(得分:0)

确保正确设置隐藏字段值,缺少&#34; h&#34;在其ID的末尾。

以下是您的代码,此修复程序对我有用:

    <input type="hidden" id="tCodh" runat="server" />
    <telerik:RadComboBox ID="tCod" runat="server" Style="width: 185px;" OnClientSelectedIndexChanged="OnClientSelectedIndexChanged">
        <DefaultItem Text="Select" Value="" />
        <Items>
            <telerik:RadComboBoxItem Text="Description" Value="Description" />
            <telerik:RadComboBoxItem Text="Account" Value="Account" />
            <telerik:RadComboBoxItem Text="Price" Value="Price" />
            <telerik:RadComboBoxItem Text="Quantity" Value="Quantity" />
        </Items>
    </telerik:RadComboBox>
    <asp:Button ID="Button1" Text="get values on server" OnClick="Button1_Click" runat="server" />
    <script type="text/javascript">
        function OnClientSelectedIndexChanged(sender, eventArgs) {
            var item = eventArgs.get_item();
            document.getElementById('<%=tCodh.ClientID%>').value = item.get_value();
        }
    </script>

以及使用该数据的示例服务器处理程序:

protected void Button1_Click(object sender, EventArgs e)
{
    string data = string.Format("data in hidden input: {0}<br />data from combo: {1}", tCodh.Value, tCod.SelectedValue);
    Response.Write(data);
}

以下是使用级联组合的演示:http://demos.telerik.com/aspnet-ajax/combobox/examples/functionality/multiplecomboboxes/defaultcs.aspx