2012-09-30 49 views
1

我想從後面的代碼訪問我的隱藏字段值,但我得到的是空值,我在這裏做的是從Javascript函數設置2個隱藏字段值然後通過asp:button點擊進行回發。不知道我在這裏做錯了什麼,請親切協助:asp.net +在代碼隱藏中訪問在JS上分配的HiddenField值

謝謝。

<form id="Form1" class="form" runat="server"> 

      <p class="SearchAddress"> 
       <asp:TextBox ID="searchTextField" runat="server" /> 
       <label for="searchTextField"> 
        Location</label> 
      </p> 
      <p class="Date"> 
       <asp:TextBox ID="txtDate" runat="server">18/10/2012</asp:TextBox> 
       <label for="txtDate"> 
        Date</label> 
      </p> 
      <p class="TimeFrom"> 
       <asp:TextBox ID="TimeFrom" runat="server">18:00</asp:TextBox> 
       <label for="txtTimeFrom"> 
        Time From</label> 
      </p> 
      <p class="TimeTo"> 
       <asp:TextBox ID="TimeTo" runat="server">19:00</asp:TextBox> 
       <label for="txtTimeTo"> 
        Time To</label> 
      </p> 

      <p class="submit"> 
       <asp:Button ID="btnCalculateCoordinates" runat="server" Text="Post It!" 
        onClientclick="calculateCoordinates();" onclick="btnCalculateCoordinates_Click" 
      /> 
      </p> 

      <asp:ScriptManager ID="ScriptManager1" runat="server" /> 
      <asp:UpdatePanel ID="uppnlLatLong" runat="server" UpdateMode="Conditional"> 
      <ContentTemplate> 
         <asp:HiddenField ID="txtLatitude" runat="server" /> 
         <asp:HiddenField ID="txtLongitude" runat="server" /> 
      </ContentTemplate> 
      </asp:UpdatePanel> 
      </form> 

     function calculateCoordinates() { 
        var txtAddress1 = document.getElementById('<%=searchTextField.ClientID%>'); 
        var txtLat = document.getElementById('<%=txtLatitude.ClientID%>'); 
        var txtLng = document.getElementById('<%=txtLongitude.ClientID%>'); 

        var address = txtAddress1.value + ', '; 

        var geocoder; 
        geocoder = new google.maps.Geocoder(); 

        geocoder.geocode({ address: address }, function (results, status) { 
         if (status == google.maps.GeocoderStatus.OK) { 
          var location = results[0].geometry.location; 
          txtLat.value = location.lat(); //SETTING THE HIDDENFIELD VALUE 
          txtLng.value = location.lng(); //SETTING THE HIDDENFIELD VALUE 
         } 
         else 
          alert('Opps, sorry but we are unable to locate' + $(txtAddress1).val()); 
        }); 
      } 
     </script> 

回答

1

你需要把命令生成的事件更新面板以及獲取隱藏。字段值。您需要將所有控件放入參與ajax調用的更新面板中。

<form id="Form1" class="form" runat="server"> 
     <asp:ScriptManager ID="ScriptManager1" runat="server" /> 
     <asp:UpdatePanel ID="uppnlLatLong" runat="server" UpdateMode="Conditional"> 
     <ContentTemplate> 
       <asp:HiddenField ID="txtLatitude" runat="server" /> 
       <asp:HiddenField ID="txtLongitude" runat="server" /> 
       <asp:Button ID="btnCalculateCoordinates" runat="server" Text="Post It!" onClientclick="calculateCoordinates();" onclick="btnCalculateCoordinates_Click" /> 


您可以使用AJAX事件begin_request的ASP.NET的調用AJAX方法調用的推移服務器之前。

Sys.WebForms.PageRequestManager.getInstance().remove_beginRequest(beginRequestHandler); 

function BeginRequestHandler(sender, args) 
{ 
     calculateCoordinates(); 
} 
+0

ScriptManager和UpdatePanel應該位於表單內。 – k80sg

+0

是的,我已經更新了我的答案。 – Adil

+0

這不起作用,我想回發運行我的服務器方法,因此無法抓住我的HiddenField值。我做的是成功設置值後,從我的Javascript中調用按鈕單擊。 – k80sg

相關問題