2014-01-25 28 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填充,但在按鈕單擊事件中提交後的代碼中,值不存在。如果我再次提交,則可以在後面的代碼中獲取這些值。 –