Hiddenfield值始终在服务器端显示0

时间:2014-04-03 08:39:42

标签: c# javascript jquery asp.net

这里我将值设置为隐藏字段,但是当我从服务器端访问它时,它总是显示0

<input type="hidden" ID="hdnSelectedChurch" runat="server" value="0" />

 $('select').change(function () {
        var optionSelected = $(this).find("option:selected");
        var valueSelected = optionSelected.val(); <-- Selected value correct here.
        //var x = $('#SelectedChurch').val($(this).val());
        var hdnSelectedChurch = ($(this).val());
        LoadFathersToChurch(valueSelected);

    });

在服务器端访问

protected void btnSubmitChild_Click(object sender, EventArgs e)
  {
       int Church = Convert.ToInt32(hdnSelectedChurch.Value);
       // int Churchstr =Convert.ToInt32(Request.Form[hdnSelectedChurch.UniqueID]);
  }

2 个答案:

答案 0 :(得分:1)

您应该使用Control.ClientID获取控件的客户端ID,然后使用.val()设置其值

var hdnSelectedChurch = '<%# hdnSelectedChurch.ClientID %>';   
$('#' + hdnSelectedChurch).val($(this).val());

而不是

var hdnSelectedChurch = ($(this).val());

答案 1 :(得分:0)

我认为你的意思是设置隐藏字段的值(我不认为你这样做):

$('select').change(function () {
   // other stuff here ...
   $("input[id*=hdnSelectedChurch").val($(this).val());
});