使用jquery设置asp:hiddenfield的值。但是当我在.aspx.cs页面中访问hiddenfield的值时,它会显示空字符串。
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 :(得分: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
答案 1 :(得分:0)
要在 C# 端访问 hiddenfield 的值,只需使用
string lat = Request.Form["hdnLat"];
string lng = Request.Form["hdnLng"];