0

I coded in Image.aspx.cs file as

public partial class Necklace : System.Web.UI.Page
{
    protected int PageId;
    protected void Page_Load(object sender, EventArgs e)
    {

        PageId = 12;
    }
}

AND I coded in Image.aspx file as

<div>
<asp:image ToolTip = "ASP Image Control" ID="Image1" runat="server" ImageUrl ="ImageCSharp.aspx?ImageID=<%= PageId %>" ></asp:image></div>

And I coded in ImageCSharp.aspx.cs file as

protected void Page_Load(object sender, EventArgs e)
    {

        if (Request.QueryString["ImageID"] != null)
        {
            string strQuery = "select ImageName , ImageData from ImageTable where id=@id"; //,Category
            SqlCommand cmd = new SqlCommand(strQuery);
            cmd.Parameters.Add("@id", SqlDbType.Int).Value = Convert.ToInt32(Request.QueryString["ImageID"]);
            DataTable dt = GetData(cmd);
            if (dt != null)
            {
                Byte[] bytes = (Byte[])dt.Rows[0]["ImageData"];
                Response.Buffer = true;
                Response.Charset = "";
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                //Response.ContentType = dt.Rows[0]["Category"].ToString();
                Response.AddHeader("content-disposition", "attachment;filename=" + dt.Rows[0]["ImageName"].ToString());
                Response.BinaryWrite(bytes);
                Response.Flush();
                Response.End();
            }
        }
    }
    private DataTable GetData(SqlCommand cmd)
    {
        DataTable dt = new DataTable();
        String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
        SqlConnection con = new SqlConnection(strConnString);
        SqlDataAdapter sda = new SqlDataAdapter();
        cmd.CommandType = CommandType.Text;
        cmd.Connection = con;
        try
        {
            con.Open();
            sda.SelectCommand = cmd;
            sda.Fill(dt);
            return dt;
        }
        catch
        {
            return null;
        }
        finally
        {
            con.Close();
            sda.Dispose();
            con.Dispose();
        }
    }
}

Please help me how to code ImageUrl ="ImageCSharp.aspx?ImageID=<%= PageId %>" correctly... PageId should be variable.

1 Answer 1

1

Just set it in code behind:

protected int PageId;

protected void Page_Load(object sender, EventArgs e)
{
   PageId = 12; // some value 
   Image1.ImageUrl ="ImageCSharp.aspx?ImageID=" + PageId;
}
Sign up to request clarification or add additional context in comments.

9 Comments

Can you help me? I want to loop Image id as for (int i = 1; i <= 10; i++) { **Image1**.ImageUrl = "ImageCSharp.aspx?ImageID=" + PageId; }
what do you mean by loop image id? you have a couple of images? are count of them are the same?
Yes I have 10 images. I can change PageId value. But I don't know hot to use loop to change image id.(Image1.ImageUrl)
If count of them is static, then you can just set Image1.ImageUrl = "ImageCSharp.aspx?ImageID=" + PageID1, Image2.ImageUrl = "ImageCSharp.aspx?ImageID=" + PageID2, Image3.ImageUrl = "ImageCSharp.aspx?ImageID=" + PageID3, etc. Or if they are dynamic, then you can create them dynamically with asp:Repeater
for (int i = 1; i <= 10; i++) { PageId == i; Image[i].ImageUrl = "ImageCSharp.aspx?ImageID=" + PageId; } I want like this.help me
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.