我怎么能在ajax方法中达到textBox值?

时间:2011-12-29 13:46:27

标签: c# javascript asp.net ajax

我有两个问题。第一个,当我点击登录按钮时,localhost想让我给予允许。但它一秒钟就突然消失了。如果我双击,它总是像这样显示在底部。然后运行Ajax方法。我想一键显示这个窗口。

我的第二个问题,我想在ajax方法中达到txtUserName。我不知道怎么做。

error

代码背后:

[System.Web.Services.WebMethod]
    public static string SendLocation(object Latitude, object Longitude)
    {
        Core dal_ = new Core();
        Model.Team obj_ = new Model.Team();

        //obj_.UserName = ?????
        obj_.Latitude = Convert.ToDecimal(Latitude);
        obj_.Longitude = Convert.ToDecimal(Longitude);

        dal_.UpdateTeamCoordinat(obj_);
        return "";
    }

JavaScript(在asp.x中):

   <script src="../Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script src="js/yqlgeo.js"></script>
<script type="text/javascript">

    function initiate_geolocation() {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(handle_geolocation_query, handle_errors);
        }
        else {
            yqlgeo.get('visitor', normalize_yql_response);
        }
    }

    function handle_errors(error) {
        switch (error.code) {
            case error.PERMISSION_DENIED: alert("user did not share geolocation data");
                break;

            case error.POSITION_UNAVAILABLE: alert("could not detect current position");
                break;

            case error.TIMEOUT: alert("retrieving position timedout");
                break;

            default: alert("unknown error");
                break;
        }
    }

    function normalize_yql_response(response) {
        if (response.error) {
            var error = { code: 0 };
            handle_error(error);
            return;
        }

        var position = {
            coords:
        {
            latitude: response.place.centroid.latitude,
            longitude: response.place.centroid.longitude
        },
            address:
        {
            city: response.place.locality2.content,
            region: response.place.admin1.content,
            country: response.place.country.content
        }
        };
        handle_geolocation_query(position);     
    }

    function handle_geolocation_query(position) {
        PageMethods.SendLocation(position.coords.latitude, position.coords.longitude);
    }
</script>

Asp.x:

  <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
  <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>
  <asp:Button ID="LoginButton" runat="server" Text="Log In"    OnClientClick="initiate_geolocation()"
                OnClick="LoginButton_Click" />

2 个答案:

答案 0 :(得分:1)

您可以将ajax调用更新为:

PageMethods.SendLocation($("#txtUserName").val(), position.coords.latitude, position.coords.longitude);

您还需要将其作为参数添加到您的webmethod。

答案 1 :(得分:0)

尝试在webMethod中选择一个元素?我认为这是不可能的

我主要使用WebMethods作为FireAndForget类型函数。

如果您想在激活该函数后选择文本框,那么我将使用javaScript

选择它
function handle_geolocation_query(position) {
    PageMethods.SendLocation(position.coords.latitude, position.coords.longitude);

   //If have JQuery
   var textbox = $('#' + <%=txtUserName.ClientID %>);

   //otherwise
   document.getElementById(<%=txtUserName.ClientID %>);    
}

取决于你想要对元素做什么取决于是否要打扰JQuery根。

相关问题