JSON回发到C#的webmethod添加文本控件 [英] JSON postback to c# webmethod add literal control

查看:206
本文介绍了JSON回发到C#的webmethod添加文本控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习的webMethods和使用JSON,我有以下以下张贴回他们,但它说,它无法找到的WebMethod(404)。不能看到我要去哪里错了,谢谢。

在页面的JavaScript:

<脚本SRC =htt​​p://ajax.googleapis.com/ajax/libs/jquery/1.7.1 /jquery.min.js类型=文/ JavaScript的>< / SCRIPT>
       <脚本类型=文/ JavaScript的>
           $(文件)。就绪(函数(){
               $(。FilterResults)。点击(函数(){
                   变种主题= $(DropDownList1)VAL();
                   变种数= $(DropDownList2)VAL();
                   变种月= $(DropDownList3)VAL();
                   $阿贾克斯({
                       键入:POST,
                       网址:filterresultshold.asmx / filterresults
                       数据:{'ARGS':'+主题+'},
                       的contentType:应用/ JSON的;字符集= UTF-8,
                       数据类型:JSON
                       成功:函数(MSG){
                           //如果从方法返回的东西,它可以通过访问msg.d
                       }
                   });                   //为prevent回传
                   返回false;
               });
           });
< / SCRIPT>

在ASCX:

<表ID =form1的=服务器>
 < ASP:的ScriptManager ID =ScriptManager1=服务器的EnablePageMethods =真/>
    < D​​IV>
        < ASP:占位符ID =PLACEHOLDER1=服务器>< ASP:文字ID =Literal1文本=文本显示模式=直通=服务器/>< / ASP:占位符>
        < ASP:DropDownList的ID =DropDownList1级=DropDownList1=服务器>< / ASP:DropDownList的>
        < ASP:DropDownList的ID =DropDownList2级=DropDownList2=服务器>< / ASP:DropDownList的>
        < ASP:DropDownList的ID =DropDownList3级=DropDownList3=服务器>< / ASP:DropDownList的>
        < ASP:按钮的ID =FilterResults级=FilterResults=服务器文本=填充的DropDownList/>
    < / DIV>
< /表及GT;

在code背后:

使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;
使用System.Web.Services;///<总结>
///为filterresults摘要说明
///< /总结>
[WebService的空间(namespace =htt​​p://tempuri.org/)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)
//要允许此Web服务从脚本调用,使用ASP.NET AJAX,取消注释以下行。
[System.Web.Script.Services.ScriptService]
公共类filterresultshold:System.Web.Services.WebService {    [System.Web.Services.WebMethod]
    公共无效filterresults(字符串参数)
    {
        字符串[]数据= args.Trim()斯普利特('')。
        串主题=数据[0];
        串数=数据[1];
        串月=数据[2];
        串控制=<一把umbraco:宏别名='pdfarchivelist'RUNAT =服务器的话题='+主题+'数='+数字+'月='+月+'>< /一把umbraco:宏>中;
        // LiteralControl字面=新LiteralControl(对照组);
        //占位符PLACEHOLDER1 =新的占位符();
        //PlaceHolder1.Controls.Add(literal);
    }}

然后在后面的.ascx code:

公共部分类usercontrols_pdfarea:System.Web.UI.UserControl
{
    保护无效的Page_Load(对象发件人,EventArgs的发送)
    {
        如果(!Page.IsPostBack)
        {
            //填充滴
            变种rootNode中= uQuery.GetRootNode();
            DropDownList1.Items.Add(新的ListItem(SELEZIONA NUMERO));
            DropDownList2.Items.Add(新的ListItem(SELEZIONA MESE));
            DropDownList3.Items.Add(新的ListItem(SELEZIONA ARGOMENTO));            //在媒体区域显示在库文件夹密码
            VAR startMedia = uQuery.GetMediaByName(PDF文件)FirstOrDefault()。
            VAR DropList = rootNode.GetDescendantNodes(),其中(X => x.NodeTypeAlias​​ ==文件)。            的foreach(在startMedia.Children VAR项)
            {
                DropDownList1.Items.Add(新的ListItem(item.getProperty(数字)Value.ToString())。); //数
                DropDownList2.Items.Add(新的ListItem(item.getProperty(月)Value.ToString())。); //月
            }            的foreach(; p.GetPropertyAsString(主题))鲜明的())了ToList(在startMedia.Children.Select(P =&GT VAR项目。)
            {
                DropDownList3.Items.Add(新的ListItem(item.ToString()));
            }
        }
    }
}


解决方案

  //要允许此Web服务从脚本调用,使用ASP.NET AJAX,取消注释以下行。
// [System.Web.Script.Services.ScriptService]
公共类filterresultshold:System.Web.Services.WebService {

只是在做什么意见建议

顺便说一句,在 filterresults 不应是静态

I'm learning about webmethods and posting back to them using JSON, I've got the following below, but it says it can't find the webmethod (404). Can't see where I'm going wrong, thanks.

In the page javascript:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
       <script type="text/javascript">
           $(document).ready(function () {
               $(".FilterResults").click(function () {
                   var topic = $(".DropDownList1").val();
                   var number = $(".DropDownList2").val();
                   var month = $(".DropDownList3").val();
                   $.ajax({
                       type: "POST",
                       url: "filterresultshold.asmx/filterresults",
                       data: "{'args': '" + topic + "'}",
                       contentType: "application/json; charset=utf-8",
                       dataType: "json",
                       success: function (msg) {
                           // If you return something from the method, it can be accessed via msg.d                
                       }
                   });

                   // To prevent the postback
                   return false;
               });
           });
</script> 

In the ascx:

<form id="form1" runat="server">
 <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />
    <div>
        <asp:PlaceHolder ID="PlaceHolder1" runat="server"><asp:Literal ID="Literal1" Text="Text to display" mode="PassThrough" runat="server" /></asp:PlaceHolder>
        <asp:DropDownList ID="DropDownList1" class="DropDownList1" runat="server"></asp:DropDownList>
        <asp:DropDownList ID="DropDownList2" class="DropDownList2" runat="server"></asp:DropDownList>
        <asp:DropDownList ID="DropDownList3" class="DropDownList3" runat="server"></asp:DropDownList>
        <asp:Button ID="FilterResults" class="FilterResults" runat="server" Text="Fill DropDownList" />
    </div>
</form>

In the code behind:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

/// <summary>
/// Summary description for filterresults
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class filterresultshold : System.Web.Services.WebService {

    [System.Web.Services.WebMethod]
    public void filterresults(string args)
    {
        string[] data = args.Trim().Split(',');
        string topic = data[0];
        string number = data[1];
        string month = data[2];
        string control = "<umbraco:Macro alias='pdfarchivelist' runat='server' topic='" + topic + "' number='" + number + "' month='" + month + "'></umbraco:Macro>";
        //LiteralControl literal = new LiteralControl(control);
        //PlaceHolder PlaceHolder1 = new PlaceHolder();
        //PlaceHolder1.Controls.Add(literal);
    }

}

Then in the .ascx code behind:

public partial class usercontrols_pdfarea : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {      
        if (!Page.IsPostBack)
        {
            // Populate Drops
            var rootNode = uQuery.GetRootNode();
            DropDownList1.Items.Add(new ListItem("SELEZIONA NUMERO"));
            DropDownList2.Items.Add(new ListItem("SELEZIONA MESE"));
            DropDownList3.Items.Add(new ListItem("SELEZIONA ARGOMENTO"));

            //display the password on the Gallery Folder in the media area
            var startMedia = uQuery.GetMediaByName("pdfs").FirstOrDefault();
            var DropList = rootNode.GetDescendantNodes().Where(x => x.NodeTypeAlias == "File");

            foreach (var item in startMedia.Children)
            {
                DropDownList1.Items.Add(new ListItem(item.getProperty("number").Value.ToString())); //NUMBER
                DropDownList2.Items.Add(new ListItem(item.getProperty("month").Value.ToString())); //MONTH
            }

            foreach (var item in startMedia.Children.Select(p => p.GetPropertyAsString("topic")).Distinct().ToList())
            {
                DropDownList3.Items.Add(new ListItem(item.ToString()));
            }
        }
    }
}

解决方案

// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class filterresultshold : System.Web.Services.WebService {

Just do what suggested in comment

BTW, the filterresults shouldn't be static

这篇关于JSON回发到C#的webmethod添加文本控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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