0

我正在将查询字符串从数据库传递给我的通用处理程序以进行图像转换。我的设计器页面出现以下问题

       <asp:TemplateField>
        <ItemTemplate>
      <asp:Image ID="Image1" runat="server" ImageUrl='<%# ImageHandler.ashx?IMG_ID=Eval("Image_ID")+ &IMG_type_ID=Eval("Type_ID_FK") %>' />
          </ItemTemplate>
       </asp:TemplateField>

我在 ImageUrl 上遇到问题。

下面是我的主要设计师代码

 <form id="form1" runat="server">
<div style="width: 960px; height: 100%; padding: 0; margin: 0; font-size: 0.875em;
    line-height: 1.785em;">
    <div>
        <asp:Panel ID="Panel1" runat="server">
            <h1>
                Welcome To Blue Marine! Please login with your account</h1>
            <br />
            <h2>
                Login Area
            </h2>
            <div>
                Username
                <asp:TextBox ID="txt_username" runat="server" ControlToValidate=" txt_username"></asp:TextBox><span>*</span><asp:RequiredFieldValidator
                    ID="RequiredFieldValidator1" runat="server" ErrorMessage="Please Enter Valid Entry"
                    ControlToValidate="txt_username"></asp:RequiredFieldValidator>
                <br />
                Password
                <asp:TextBox ID="txt_password" runat="server"></asp:TextBox><span>*</span><asp:RequiredFieldValidator
                    ID="RequiredFieldValidator2" runat="server" ControlToValidate="txt_password"
                    ErrorMessage="Please Enter Valid Entry"></asp:RequiredFieldValidator>
                <br />
                <div style="margin-left: 90px">
                    <asp:Button ID="btn_Submit" runat="server" Text="Submit" OnClick="btn_Submit_Click" />
                </div>
        </asp:Panel>
    </div>
    <div>
        <asp:Panel ID="Panel2" runat="server" Visible="False">
            <div style="text-align: right">
                Hello!
                <asp:Label ID="lb_username" runat="server" Text="Label"></asp:Label>
                <br />
                <div>
                    <asp:LinkButton ID="Linkbtn_signout" runat="server" OnClick="Linkbtn_signout_Click">SignOut</asp:LinkButton>
                </div>
            </div>
            <div style="text-align: left">
                <h1>
                    Image Upload Section</h1>
                <p>
                    You can upload Images here One at a time Format Supported JPG,BMP,PNG
                    <br />
                </p>
                &nbsp;<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
                <asp:FileUpload ID="FileUpload1" runat="server" /><br />
                <asp:SqlDataSource ID="SqlDataSource_ImageType" runat="server" ConnectionString="<%$ ConnectionStrings:db_string2 %>"
                    SelectCommand="SELECT [Type_ID_PK], [TypeName] FROM [ImageType]"></asp:SqlDataSource>
                <br />
                Select Image Type&nbsp;
                <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource_ImageType"
                    DataTextField="Type_ID_PK" DataValueField="Type_ID_PK">
                </asp:DropDownList>
                <span>*</span>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Select Image Type"
                    ControlToValidate="DropDownList1"></asp:RequiredFieldValidator>
                <br />
                <asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="Upload" /><br />
                <br />
                <asp:Label ID="lblMessage" runat="server"></asp:Label><br />
                <br />
                <br />
                <div style="text-align: center; margin-left: 170px">
                    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource_DbImage"
                        EnableModelValidation="True" AllowPaging="True" AllowSorting="True" Width="566px"
                        DataKeyNames="Image_ID,Type_ID_FK">
                        <Columns>
                            <asp:BoundField DataField="Image_ID" HeaderText="Image_ID" InsertVisible="False"
                                ReadOnly="True" SortExpression="Image_ID" />
                            <asp:BoundField DataField="Type_ID_FK" HeaderText="Type_ID_FK" SortExpression="Type_ID_FK"
                                ReadOnly="True" />
                            <asp:BoundField DataField="ImageName" HeaderText="ImageName" SortExpression="ImageName" />
                            <asp:TemplateField>
                                <ItemTemplate>
                                    <asp:Image ID="Image1" runat="server" ImageUrl='<%# ImageHandler.ashx?IMG_ID=Eval("Image_ID")+ &IMG_type_ID=Eval("Type_ID_FK") %>' />
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>
                </div>
                <asp:SqlDataSource ID="SqlDataSource_DbImage" runat="server" ConnectionString="<%$ ConnectionStrings:db_string2 %>"
                    SelectCommand="SELECT * FROM [Image_Link]"></asp:SqlDataSource>
            </div>
        </asp:Panel>
    </div>
</div>
</form>

我背后的代码:

  public partial class Admin : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session.Count > 0)
        {
            try
            {
                string id = Convert.ToString(Session["UserID"]);
                if (id != "")
                {
                    if (!IsPostBack)
                    {
                        Panel1.Visible = false;
                        Panel2.Visible = true;
                    }
                    else
                    {
                        Panel1.Visible = false;
                        Panel2.Visible = true;
                    }
                }
            }
            catch (NullReferenceException n)
            {

            }


        }
        else
        {
            if (!IsPostBack)
            {
                Panel1.Visible = true;
            }
        }
    }

    protected void btn_Submit_Click(object sender, EventArgs e)
    {
        Session.Clear();
        bool result = Check_User_Login(txt_username.Text, txt_password.Text);
        if (result == true)
        {
            Panel1.Visible = false;
            Panel2.Visible = true;
        }
        else
        {
            Response.Write("<script>alert('Admin Account Does not exist!');</script>");
        }





    }


    public bool Check_User_Login(string para_name, string para_password)
    {
        string db_username, dp_password;
        Web_Eng.DatabaseConection.comm = new SqlCommand("Select Top  1 a.Admin_ID From [Admin] a  Order By a.Admin_ID Desc;", Web_Eng.DatabaseConection.connect());
        int id = (int)Web_Eng.DatabaseConection.comm.ExecuteScalar();
        for (int i = 1; i <= id; i++)
        {
            Web_Eng.DatabaseConection.comm.CommandText = "select a.Admin_Name,a.[Password] from [Admin] a where a.Admin_ID=" + i + ";";

            #region use to retrieve whole row from database

            Web_Eng.DatabaseConection.entrieRow_Reader = Web_Eng.DatabaseConection.comm.ExecuteReader();
            while (Web_Eng.DatabaseConection.entrieRow_Reader.Read())
            {

                db_username = Web_Eng.DatabaseConection.entrieRow_Reader["Admin_Name"].ToString();
                dp_password = Web_Eng.DatabaseConection.entrieRow_Reader["Password"].ToString();

                if (db_username == para_name && dp_password == para_password)
                {

                    Session["UserID"] = i;
                    lb_username.Text = db_username;
                    Web_Eng.DatabaseConection.entrieRow_Reader.Close();
                    return true;

                }

            }
            Web_Eng.DatabaseConection.entrieRow_Reader.Close();


            #endregion
        }
        return false;
    }

    protected void Linkbtn_signout_Click(object sender, EventArgs e)
    {
        Session.Clear();
        Page.Response.Redirect("Index.aspx");
    }

    protected void btnUpload_Click(object sender, EventArgs e)
    {
        string strImageName = txtName.Text.ToString();
        if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
        {
            byte[] imageSize = new byte[FileUpload1.PostedFile.ContentLength];
            HttpPostedFile uploadedImage = FileUpload1.PostedFile;
            uploadedImage.InputStream.Read(imageSize, 0, (int)FileUpload1.PostedFile.ContentLength);


            // Create SQL Command 
            int id=Convert.ToInt32(DropDownList1.SelectedItem.Value);
            int result = AddImage_DB(strImageName, id, imageSize);
            if (result > 0)
                lblMessage.Text = "File Uploaded";
            GridView1.DataBind();
        }
    }



    public int AddImage_DB(string para_imagename, int para_typeID, byte[] para_image)
    {
        try
        {
            //DatabaseConection.cmd.con;
            Web_Eng.DatabaseConection.cmd.Connection = Web_Eng.DatabaseConection.connect();
            Web_Eng.DatabaseConection.cmd = new SqlCommand("Insert_Image_Table", Web_Eng.DatabaseConection.connect());
            Web_Eng.DatabaseConection.cmd.CommandType = CommandType.StoredProcedure;
            Web_Eng.DatabaseConection.cmd.Parameters.Add(new SqlParameter("@typeID", SqlDbType.Int)).Value = para_typeID;
            Web_Eng.DatabaseConection.cmd.Parameters.Add(new SqlParameter("@ImageName", SqlDbType.NVarChar)).Value = para_imagename;
            Web_Eng.DatabaseConection.cmd.Parameters.Add(new SqlParameter("@convertedImage", SqlDbType.Image, para_image.Length)).Value = para_image;
            Web_Eng.DatabaseConection.cmd.ExecuteNonQuery();
            return 1;
        }
        catch (Exception er)
        {

            Response.Write("<script>alert('Error');</script>");
            return 0;
        }
    }
}

我的通用处理程序

 public class ImageHandler : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {

        // Create SQL Command 
        Web_Eng.DatabaseConection.comm = new SqlCommand("Select img.ImageName,img.CompleteImage from Image_Link img where img.Image_ID=@IMG_ID and img.Type_ID_FK=@IMG_type_ID;", Web_Eng.DatabaseConection.connect());
        Web_Eng.DatabaseConection.cmd.CommandType = System.Data.CommandType.Text;

        // Img ID
        SqlParameter ImageID = new SqlParameter("@IMG_ID", System.Data.SqlDbType.Int);
        ImageID.Value = context.Request.QueryString["IMG_ID"];

        // iMG Type ID
        SqlParameter Image_Type_ID = new SqlParameter("@IMG_type_ID", System.Data.SqlDbType.Int);
        Image_Type_ID.Value = context.Request.QueryString["IMG_type_ID"];


        // Adding Parameters
        Web_Eng.DatabaseConection.cmd.Parameters.Add(ImageID);
        Web_Eng.DatabaseConection.cmd.Parameters.Add(Image_Type_ID);

        SqlDataReader dReader = Web_Eng.DatabaseConection.cmd.ExecuteReader();
        dReader.Read();
        context.Response.BinaryWrite((byte[])dReader["CompleteImage"]);
        dReader.Close();
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

不知道我在将查询字符串传递给我的处理程序时做错了什么

提前致谢

4

3 回答 3

0

谢谢大家我已经解决了我自己的问题我正在为可能有同样问题的其他人发布答案,这将对他们有所帮助

只需更换

<asp:Image ID="Image1" runat="server" ImageUrl='<%# ImageHandler.ashx?IMG_ID=Eval("Image_ID")+ &IMG_type_ID=Eval("Type_ID_FK") %>' />

经过

<asp:Image ID="Image1" runat="server" ImageUrl='<%# "ImageHandler.ashx?IMG_ID=" + Eval("Image_ID")+"&IMG_type_ID="+Eval("Type_ID_FK")%>' />
于 2012-12-15T11:37:38.267 回答
0

尝试以下语法:

<asp:Image ID="Image1" runat="server" ImageUrl='ImageHandler.ashx?IMG_ID=<%# Eval("Image_ID")+&IMG_type_ID=Eval("Type_ID_FK") %>' />
于 2012-12-15T01:53:24.927 回答
0
public void ProcessRequest(HttpContext context)
    {
        using (SqlConnection connection = new SqlConnection())
        {
            connection.ConnectionString = "Server=ESLHP280G1P\\SQLEXPRESS; Database=Sample; user id=sa; password=sa@123";
            connection.Open();
            SqlCommand cmd = new SqlCommand("tblImage_Retrieve", connection);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@id", context.Request.QueryString["id"]);
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                byte[] bytes = (byte[])dr["photo"];
                if (bytes.Length > 0 && bytes != null)
                {
                    context.Response.ContentType = "jpg";
                    context.Response.BinaryWrite(bytes);
                }
            }
        }
    }
于 2015-08-03T09:00:34.613 回答