【问题标题】:How to pass querystring to Ajax WebService如何将查询字符串传递给 Ajax WebService
【发布时间】:2009-08-31 05:14:29
【问题描述】:

这里我使用 Asp.Net Ajax SlideShowExtender 控件来创建存储在数据库中的图像幻灯片。此控件使用 GetSlides() Web 服务来检索数据库信息。现在我想将一个查询字符串传递给 GetSlides() 网络服务,以便图像像查询字符串中的值一样旋转。我的困难是如何将查询字符串传递给这个特定的网络服务,我尝试使用“HttpContext.Current.Request.QueryString[“id”]”但这不起作用,为什么?有人可以建议如何将查询字符串传递给这个网络服务。

【问题讨论】:

    标签: c# asp.net asp.net-ajax


    【解决方案1】:

    您应该使用 SlideShowExtender 的 ContextKey 功能(请参阅its documentation)。

    如果您的扩展程序被声明为类似于示例:

    <ajaxToolkit:SlideShowExtender ID="SlideShowExtender1" runat="server" 
      TargetControlID="Image1" 
      SlideShowServiceMethod="GetSlides" 
      AutoPlay="true" 
      ImageTitleLabelID="imageTitle"
      ImageDescriptionLabelID="imageDescription"
      NextButtonID="nextButton" 
      PlayButtonText="Play" 
      StopButtonText="Stop" 
      PreviousButtonID="prevButton" 
      PlayButtonID="playButton" 
      Loop="true" />
    

    您的 GetSlides 服务方法是使用 contextKey 参数声明的(小心,区分大小写),如下所示:

    [System.Web.Services.WebMethod]
    [System.Web.Script.Services.ScriptMethod]
    public AjaxControlToolkit.Slide[] GetSlides(string contextKey) 
    {
      // Do something with contextKey here and return the slides.
    }
    

    然后您可以在 SecondPage.aspx 的 Page_Load 中使用类似这样的代码将该 QueryString 值传递给服务方法。

    protected void Page_Load(object sender, EventArgs e) 
    {
      SlideShowExtender1.ContextKey = Request.QueryString["id"];
    }
    

    【讨论】:

    • 物有所值!使用它来传递图像的路径。 +1
    【解决方案2】:

    要传递查询字符串,您可以这样做

    http://yourpath/service.asmx?imageid=3
    

    要从您的网络服务访问查询字符串,您可以这样做

    this.Context.Request.QueryString["imageid"];
    

    【讨论】:

    • 这不是我要问的,我从第一页“Response.Redirect("secondpage.aspx?id=" + path);" 传递查询字符串在 ajax slideshowextender web 方法下的第二页中,我使用以下代码检索“HttpContext.Current.Request.QueryString[“id”]”,但这不起作用。
    【解决方案3】:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-11
    • 1970-01-01
    • 1970-01-01
    • 2020-06-02
    • 2019-02-15
    相关资源
    最近更新 更多