简体   繁体   English

如何在转发器控件中通过HTTP处理程序(.ashx)获取标签的文本

[英]How to get label's text by HTTP handler (.ashx) in repeater control

I have a label within a <table> in a repeater. 我在中继器的<table>中有一个标签。 I have an HttpHandler named "NameShow.ashx" to return the "name" as "text/plain" by passing an "id" to the handler. 我有一个名为“ NameShow.ashx”的HttpHandler,通过将“ id”传递给处理程序来将“名称”作为“文本/纯文本”返回。

I want to retrieve the "name" (similar to retrieving "image" from handler). 我想检索“名称”(类似于从处理程序中检索“图像”)。

Here is my code: 这是我的代码:

<asp:Label ID="Label1" runat="server" Text='<%#""NameShow.ashx?id="+Eval("id") %>'>
</asp:Label>

I am getting the text of this label as ->> NameShow.ashx?id=123 我得到此标签的文本为->> NameShow.ashx?id = 123

Please help in finding where I am doing mistake. 请帮助找出我在哪里做错了。

Here is my Haldler code. 这是我的Haldler代码。

using System; 使用系统; using System.Web; 使用System.Web;

public class NameShow : IHttpHandler { 公共类NameShow:IHttpHandler {

public void ProcessRequest (HttpContext context) 
{
    string strid = context.Request.QueryString["id"];
    long pro_id = int.Parse(strid);

    string name = DBHelpername.name(pro_id);

    context.Response.ContentType = "text/plain";
    context.Response.Write(name);
}

public bool IsReusable {
    get {
        return false;
    }
}

} }

Here is my DBHelper code: 这是我的DBHelper代码:

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

/// <summary>
/// Summary description for DBHelpername
/// </summary>
public class DBHelpername
{
    public DBHelpername()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    public static string name(long id)
    {
        SqlConnection connect = new SqlConnection
             ("Data Source=DELL-36B3EF6E9F;Integrated Security=True;Initial Catalog=pool");
        connect.Open();
        SqlCommand sc = 
           new SqlCommand("SELECT name FROM Profile WHERE profile_id=" + id + "", connect);
        SqlDataAdapter da = new SqlDataAdapter(sc);
        DataSet ds = new DataSet();
        da.Fill(ds);
        string nameret = ds.Tables[0].Rows[0][0].ToString();
        return nameret;
        connect.Close();
    }
}

If you are not married to the idea of using an HTTP Handler, then I suggest making a method in your .aspx page's code-behind that does the same logic your handler is doing minus the content-type stuff, like this: 如果您对使用HTTP处理程序的想法不满意,那么我建议在.aspx页的代码中创建一个方法,该方法执行与处理程序相同的逻辑,减去内容类型,例如:

protected string GetName(int pro_id)
{
    return DBHelpername.name(pro_id);
}

Now in your markup you can use this method, like this: 现在,您可以在标记中使用此方法,如下所示:

<asp:Label ID="Label1" runat="server" Text='<%# GetName((int)Eval("id")) %>'>
</asp:Label>
<%# new System.Net.WebClient().DownloadString("http://www.yoursite.com/NameShow.ashx?id="+Eval("id"))) %>

Something like this may work, although you might need to re-think your approach as you are going do a http request for each item in your repeater - and that isn't going to scale well! 这样的事情可能会起作用,尽管您可能需要重新考虑您的方法,因为您将对中继器中的每个项目执行一个http请求-这样做的伸缩性不好! It looks like this is all in one application/website, so can you not call the code that looks up the name in the codebehind of this page? 看起来这一切都在一个应用程序/网站中,所以您不能在此页面的代码背后调用查找名称的代码吗?

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

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