关于从静态方法asp.net 4.0页的访问控制问题 [英] issue regarding access page control from static method asp.net 4.0

查看:157
本文介绍了关于从静态方法asp.net 4.0页的访问控制问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的jQuery叫我的服务器端方法,并从方法,我试图访问文本框的数据。这里是我的示例code

i call my server side method by jquery and from that method i am trying to access textboxes data. here is my sample code

    [WebMethod]
    public static PayPalCart CreatePayPalFields()
    {
        Page CurPage = HttpContext.Current.Handler as Page;
        string tt = ((TextBox)CurPage.FindControl("txtBillAddress1")).Text; 
    }

我得到错误访问从静态方法和错误信息的控制不设置到对象的实例对象引用。然后我搜索谷歌,找出更好的解决方案,然后我得到了扩展方法,如果发现这将遍历控件集合,返回控制。在code是像

i am getting error for accessing the control from static method and the error message is Object reference not set to an instance of an object. then i search google to find out better solution then i got a extension method which will loop through control collection and return control if found. the code is like

public static T BetterFindControl<T>(this Control root, string id) where T : Control
{
    if (root != null)
    {
        if (root.ID == id) return root as T;

        var foundControl = (T)root.FindControl(id);
        if (foundControl != null) return foundControl;

        foreach (Control childControl in root.Controls)
        {
            foundControl = (T)BetterFindControl<T>(childControl, id);
            if (foundControl != null) return foundControl as T;

        }
    }

    return null;
}

我使用上述程序也从静态方法像

i use the above routine also from static method like

    [WebMethod]
    public static PayPalCart CreatePayPalFields()
    {
        Page CurPage = HttpContext.Current.Handler as Page;
        string sData = CurPage.BetterFindControl<TextBox>("txtDeliveryFName").Text; 
    }

但仍没有运气....仍然得到同样的错误从静态方法访问控制和发现的 CurPage 无法控制。请建议我该怎么做。告诉我一条出路,从静态方法访问控制,因为方法是静态的原因,我的jQuery调用该方法.........需要帮助。

but still no luck....still getting the same error for accessing control from static method and found that the CurPage has no control. please suggest me what should i do. tell me a way out to access control from static method because method has to be static reason i am invoking that method by jquery.........need help.

推荐答案

您不能从这个Ajax调用访问该页面,简单,因为该页面不存在任何地方,当这个调用发生的。

You can not access the page from this ajax call, simple because the page is not exist anywhere when this call is happens.

你可以做的是,通过使用JavaScript,让他们并把他们送你喜欢通过Ajax调用来检查的参数。

What you can do is to send the parameters that you like to check via the ajax call, and by using javascript to get them and send them.

要少讲更多关于什么叫事。

To say few more about what call do.

string sData = CurPage.BetterFindControl<TextBox>("txtDeliveryFName").Text;

这是对.FORM后数据的最后调用读什么是ID为txtDeliveryFName控制发送。在您的Ajax调用没有完整的页面后,从另一方面可以控制哪些数据将帖子通过JavaScript将WebMethod。

this is a final call on the .Form post data to read what is send by control with id txtDeliveryFName. On your ajax call there is no post of the full page, and from the other hand you control what data will be post to the webmethod via javascript.

这篇关于关于从静态方法asp.net 4.0页的访问控制问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆