2014-01-25 30 views
0

我正在寻找一个隐藏的字段,一旦按钮被点击通过javascript,但也回发和访问后面的代码中隐藏的字段从同一按钮单击。运行Javascript更新之前asp按钮回发和访问更新的值

上下文是一个用户将输入一个邮政编码和半径,点击搜索按钮,然后我使用谷歌API来获取邮政编码的纬度和经度,并用这些值填充隐藏的字段,搜索按钮然后执行搜索并填充它,我目前正试图与标准的ASP处理结果:按钮onclick事件

这里是我的JS代码

var geocoder; 
    function initialize() { 
     geocoder = new google.maps.Geocoder(); 
    } 

    function codeAddress() { 
     var address = $('#PostcodeTextBox').val(); 
     geocoder.geocode({ 'address': address }, function (results, status) { 
      if (status == google.maps.GeocoderStatus.OK) { 
       $('#Latitude').val(results[0].geometry.location.lat()); 
       $('#Longitude').val(results[0].geometry.location.lng()); 

       __doPostBack($('#SearchButton').attr('id'), ''); 
      } else { 
       alert('Geocode was not successful for the following reason: ' + status); 
      } 
     }); 
    } 

    google.maps.event.addDomListener(window, 'load', initialize); 

而且我的标记

<asp:HiddenField ID="Longitude" runat="server" ClientIDMode="Static" /> 
<asp:HiddenField ID="Latitude" runat="server" ClientIDMode="Static" /> 
<legend><h3>Supplier Search</h3></legend> 
<label class="control-label" for="inputEmail">Supplier Type</label> 
<asp:DropDownList ID="SupplierTypeDropDownList" runat="server" AppendDataBoundItems="true" > 
    <asp:ListItem Text="All" Value="" /> 
</asp:DropDownList> 
<label class="control-label" for="PostcodeTextBox">Postcode</label> 
<asp:TextBox ID="PostcodeTextBox" runat="server" ClientIDMode="Static" placeholder="Postcode" /> 
<label class="control-label" for="RadiusDropDownList">Radius</label> 
<asp:DropDownList ID="RadiusDropDownList" runat="server" AppendDataBoundItems="true" > 
    <asp:ListItem Text="All" Value="" /> 
    <asp:ListItem Text="1" Value="1" /> 
    <asp:ListItem Text="5" Value="5" /> 
    <asp:ListItem Text="10" Value="10" /> 
    <asp:ListItem Text="20" Value="20" /> 
    <asp:ListItem Text="50" Value="50" /> 
</asp:DropDownList> 
<asp:Button ID="SearchButton" runat="server" Text="Search" CssClass="btn btn-primary btn-large" OnClientClick="codeAddress();" OnClick="SearchButton_Click" UseSubmitBehavior="false" ClientIDMode="Static" /> 

后端代码

private void SearchButton_Click(object sender, EventArgs e) 
{ 
SearchInfo search = new SearchInfo(); 
search.Lat = Latitude.Value; // empty 
search.Lng = Longitude.Value; // empty 
} 

这已经通过基于在线评论很多不同势的版本了,问题是,每次我打SearchButton_Click隐藏字段为空值

任何帮助或想法,将不胜感激

谢谢

回答

0

这可能是一个非常简单的解决方案,但我想不起来,所以我会给你我的“黑客”。

使用CSS来隐藏你的.NET按钮

#SearchButton { display: none } 

添加另一个虚拟按钮在.net中(或标准的HTML)

<button type="button" id="fake-button" class="btn btn-primary btn-large">Search</button> 

然后一些jQuery的:

var geocoder; 
function initialize() { 
    geocoder = new google.maps.Geocoder(); 
} 

function codeAddress() { 
    var address = $('#PostcodeTextBox').val(); 
    geocoder.geocode({ 'address': address }, function (results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
      $('#Latitude').val(results[0].geometry.location.lat()); 
      $('#Longitude').val(results[0].geometry.location.lng()); 

      //alert($('#Latitude').val()); 
      //alert($('#Longitude').val()); 

      $('#SearchButton').click(); //trigger the click event of the submit button 
     } else { 
      alert('Geocode was not successful for the following reason: ' + status); 
     } 
    }); 
} 

google.maps.event.addDomListener(window, 'load', initialize); 

$(document).ready(function(){ 
    $('#fake-button').click(function(event){ 
     event.preventDefault(); 
     // populate your hidden fields here 
     codeAddress(); 
    }) 
}) 
+0

喜,感谢您的回应,我仍然有相同的问题,由JS填充的隐藏字段在搜索按钮单击事件中为空。如果我再次按下搜索按钮,那么值就在那里。 –

+0

@LEEMANDEVILLE我更新了代码示例以更加完整。如果在提交之前隐藏的字段已经填充,您可以结合使用'alert'这一行。 – jammykam

+0

嗨,再次抱歉,我可能不清楚我的解释。我可以看到,并且在您的示例中,隐藏字段由JS填充,但在按钮单击事件中提交后的代码中,值不存在。如果我再次提交,则可以在后面的代码中获取这些值。 –