asp.net +在CodeBehind的Postback上访问JS中分配的HiddenField值

时间:2012-09-30 14:11:40

标签: javascript asp.net

我试图从后面的代码访问我的隐藏字段值,但我得到空值,我在这里做的是从Javascript函数设置2个隐藏字段值,之后通过asp:按钮点击回发。不知道我在这里做错了什么,请帮忙:

感谢。

 <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 个答案:

答案 0 :(得分: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" />


     

您可以使用asp .net的ajax事件begin_request在调用服务器之前调用ajax方法。

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

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