2015-07-03 154 views
0

設置asp:hiddenfield的值。但是當我訪問.aspx.cs頁面中hiddenfield的值時,它顯示空字符串。如何訪問C#中的隱藏字段值?我使用jQuery設置隱藏字段的值使用jquery

HTML

<asp:HiddenField runat="server" ID="hdnLat"/> 
<asp:HiddenField runat="server" ID="hdnLng" /> 

JS

<script type="text/javascript"> 
    function codeAddress() { 
     var address = $(".Hno").val() + " " + $(".Address").val() + "," + $(".City").val(); // Fetch Lattitude and logtittude for Using Google Map 
     //alert(address); 
     var geocoder = new google.maps.Geocoder(); 
     geocoder.geocode({ 'address': address }, function (results, status) { 
      var location = results[0].geometry.location; 
      //alert('LAT: ' + location.lat() + ' LANG: ' + location.lng()); 
      var Lat = $('#<% =hdnLat.ClientID %>').attr('value', location.lat()); 
      var lng = $('#<% =hdnLng.ClientID %>').attr('value', location.lng()); 
      //alert($('#<% =hdnLat.ClientID %>').val() + ","+$('#<% =hdnLng.ClientID %>').val()); 

     }); 
    } 
</script 

這裏是我的按鈕在那裏我打電話jQuery的功能。

<asp:Button ID="btnUpdate" runat="server" CssClass="btn" Text="Submit" CausesValidation="True" ValidationGroup="Register2" OnClientClick="return codeAddress();"></asp:Button> 

我創建btnUpdate_Click事件時頁面預渲染是。

private void InitializeComponent() 
    { 
     this.dlInvQueAnsV.ItemDataBound += new System.Web.UI.WebControls.DataListItemEventHandler(this.dlInvQueAnsV_ItemDataBound); 
     this.dlInvQueAnsE.ItemDataBound += new System.Web.UI.WebControls.DataListItemEventHandler(this.dlInvQueAnsE_ItemDataBound); 
     this.dgInvQue.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dgInvQue_ItemDataBound); 
     this.btnBack.Click += new System.EventHandler(this.btnBack_Click); 
     this.btnUpdate.Click += new System.EventHandler(this.btnUpdate_Click); 
     this.btnRegister.Click += new System.EventHandler(this.btnRegister_Click); 
     this.lbCloseAccount.Click += new System.EventHandler(this.lbCloseAccount_Click); 
     this.Load += new System.EventHandler(this.Page_Load); 
     this.ddlAdminComment.SelectedIndexChanged += new System.EventHandler(this.ddlAdminComment_SelectedIndexChanged); 
     this.btnSaveNote.Click += new System.EventHandler(btnSaveNote_Click); 
     this.btnSendCommentSMS.Click += new System.EventHandler(btnSendCommentSMS_Click); 
     this.btnSendCommentEmail.Click += new System.EventHandler(btnSendCommentEmail_Click); 
    } 
+0

你能張貼您的HTML代碼? – Mivaweb

+0

這裏是我的html代碼

+0

你如何訪問該字段的值?請發佈您的C#代碼。更好,如果你一次提供所有的細節... – Paolo

回答

1

要設置隱藏字段的值,不使用attr()功能,但只使用val()

$(document).ready(function() { 
    $('input[id$=hdnLat]').val(location.lat()); // Take the input where id ends with hdnLat 
    $('input[id$=hdnLng]').val(location.lng()); // Take the input where id ends with hdnLng 
}); 

ASP.NET

<asp:Button runat="server" ID="btnUpdate" Text="Maps" OnClick="btnUpdate_Click" /> 

後面的C#代碼:

protected void btnUpdate_Click(object sender, EventArgs e) 
{ 
    string lat = this.hdnLat.Value; 
    string lng = this.hdnLng.Value; 
} 

JS功能

<script type="text/javascript"> 
    function codeAddress() { 

     // other code 

     return true; 
    } 
</script 
+0

好吧..我們如何訪問隱藏字段在c#端的價值 –

+0

查看更新以上! – Mivaweb

+0

我用這個,但它不起作用。實際上,我需要點擊按鈕時的值 –