繁体   English   中英

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

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

我在中继器的<table>中有一个标签。 我有一个名为“ NameShow.ashx”的HttpHandler,通过将“ id”传递给处理程序来将“名称”作为“文本/纯文本”返回。

我想检索“名称”(类似于从处理程序中检索“图像”)。

这是我的代码:

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

我得到此标签的文本为->> NameShow.ashx?id = 123

请帮助找出我在哪里做错了。

这是我的Haldler代码。

使用系统; 使用System.Web;

公共类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;
    }
}

}

这是我的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();
    }
}

如果您对使用HTTP处理程序的想法不满意,那么我建议在.aspx页的代码中创建一个方法,该方法执行与处理程序相同的逻辑,减去内容类型,例如:

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

现在,您可以在标记中使用此方法,如下所示:

<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"))) %>

这样的事情可能会起作用,尽管您可能需要重新考虑您的方法,因为您将对中继器中的每个项目执行一个http请求-这样做的伸缩性不好! 看起来这一切都在一个应用程序/网站中,所以您不能在此页面的代码背后调用查找名称的代码吗?

暂无
暂无

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

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