簡體   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