简体   繁体   English

如何将Javascript值传递给C#中的方法

[英]How to pass Javascript Value to a method in C#

Here is my radio button list 这是我的单选按钮列表

<asp:RadioButtonList ID="geo_radio" runat="server" RepeatDirection="Vertical" RepeatLayout="Table" >                             
    <asp:ListItem Value="state">State</asp:ListItem>
    <asp:ListItem Value="county">County</asp:ListItem>
    <asp:ListItem Value="county subdivision">County Subdivision</asp:ListItem>
    <asp:ListItem Value="place">Place</asp:ListItem>
    <asp:ListItem Value="micropolitan">Micropolitan</asp:ListItem>
    <asp:ListItem Value="combined statistical area">Combined Statistical Area</asp:ListItem>
    <asp:ListItem Value="congressional districts">Congressional Districts</asp:ListItem>
    <asp:ListItem Value="Custom Region">Custom Region</asp:ListItem>
</asp:RadioButtonList>  

I am able to retrieve selected value using 我能够使用

$('#<%=geo_radio.ClientID%>').find('input[type="radio"]').click(function () {
    var selectedValue = $(this).val();
});

In my .aspx page I have a method FetchData(). 在我的.aspx页面中,我有一个FetchData()方法。 I am calling this function on page load 我在页面加载时调用此函数

protected void Page_Load(object sender, EventArgs e)
{
    FetchData();   
} 

How can I send selected radio button value to FetchData() function. 如何将选定的单选按钮值发送到FetchData()函数。

You can use a hidden field, set the value of asp hidden field, or you can use request object to retrieve the value. 您可以使用隐藏字段,设置asp隐藏字段的值,也可以使用请求对象检索该值。

The Hidden Field class 隐藏的领域类

add selectedValue to hidden field and get hidden field value to code behind and pass it to your function 将selectedValue添加到隐藏字段中并获取隐藏字段值以在后面进行编码,并将其传递给函数

$("#hid").val(selectedValue);

hid is the id or your hidden feild 隐藏是ID或您隐藏的字段

 FetchData(hid.value);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM