Pregunta

I have a label within a <table> in a repeater. I have an HttpHandler named "NameShow.ashx" to return the "name" as "text/plain" by passing an "id" to the handler.

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

Please help in finding where I am doing mistake.

Here is my Haldler code.

using System; using System.Web;

public class 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:

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();
    }
}
¿Fue útil?

Solución

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:

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>

Otros consejos

<%# 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! 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?

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top