简体   繁体   English

尝试从Ajax静态WebMethod获取asp.net(C#)文本框文本时出错

[英]Error while trying to get asp.net(C#) textbox text from ajax static WebMethod

I am using an ajax htmleditor in asp.net web application so i am trying to get the text the user has entered in the editor then i will send that text back to the client javascript function that will show the text in a div. 我在asp.net Web应用程序中使用ajax htmleditor,因此我试图获取用户在编辑器中输入的文本,然后将其发送回客户端javascript函数,该函数将在div中显示该文本。 But I am getting this error "Object reference not set to an instance of an object." 但是我收到此错误“对象引用未设置为对象的实例”。

Firstly i tried to access the text of textbox linked with htmleditorextender through javascript but it was not working for me so i moved to ajax webmethod but this time also i am facing a problem. 首先,我尝试通过javascript访问与htmleditorextender链接的文本框的文本,但它对我不起作用,因此我转到了ajax webmethod,但这次我也遇到了问题。 Please Help me. 请帮我。

    [System.Web.Services.WebMethod]
    public static string seteditor()
    {
        String x="";
        try
        {
            Content c = new Content();
            x = c.txteditor.Text;
        }
        catch (Exception ex) { x=ex.Message; }
        return x;
    }

Here, txteditor is the ID of asp:textbox which is linked with ajaxcontroltoolkit htmleditorextender. 在这里,txteditor是与ajaxcontroltoolkit htmleditorextender链接的asp:textbox的ID。

You cannot get your aspx controls inside a static method. 您无法在static方法中获取aspx控件。 If you are Calling a static method from jquery means the Page and its Controls don't even exist. 如果要从jquery调用静态方法,则意味着Page及其控件甚至不存在。 You need to look another workaround for your problem. 您需要寻找另一个解决方法来解决您的问题。

EDIT: 编辑:

I always pass my control values to page methods like this: 我总是将控制值传递给这样的页面方法:

Assume I have two text controls: txtGroupName and txtGroupLevel

...My JS with Jquery will be : ...我的带有jQuery的JS将是:

var grpName = $("#<%=txtGroupName.ClientID%>").val();
var grpLevel = $("#<%= txtGroupLevel.ClientID %>").val();

data: "{'groupName':'" + grpName + "','groupLevel':'" +   grpLevel + "'}",

Where groupName and groupRights are my webmethod parameters. 其中,groupName和groupRights是我的webmethod参数。

EDIT2: 编辑2:

Include your script like this: 包括这样的脚本:

<script type="text/javascript" src="<%= ResolveUrl("~/Scripts/jquery-1.4.1.js") %>"></script>  

I suggest you to use the latest jquery version. 我建议您使用最新的jquery版本。

Web methods do not interact with the page object or the control hierarchy like this. Web方法不会与页面对象或控件层次结构进行交互。 That's why they're static in the first place. 这就是为什么它们首先是静态的。 You need to pass the text from the client as a parameter to the web method, not read it from the textbox. 您需要将客户端的文本作为参数传递给Web方法,而不是从文本框中读取文本。

This issue was torturing me from last 18 hours continuously 这个问题从过去的18个小时开始一直困扰着我
First I tried javascript than webmethod and than on user1042031's suggestion I tried jquery and than again I tried javascript and look how much easily it can be done with a single line of code. 首先,我尝试使用javascript而不是webmethod,并且尝试使用user1042031的建议尝试jquery,然后再次尝试使用javascript,看看用单行代码可以轻松完成多少次。

var a = document.getElementById('<%= txteditor.ClientID %>').value;

read this stackoverflow article Getting Textbox value in Javascript 阅读此stackoverflow文章使用Javascript获取文本框值

I apologize to everyone who responded me in this question but i have not found that article in my initial search. 我向在这个问题上回答我的每个人表示歉意,但我在最初的搜索中没有找到该文章。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM