如何从aspx.cs文件中设置变量值aspx文件在asp.net? [英] how to set variable value from aspx.cs file to aspx file in asp.net?

查看:198
本文介绍了如何从aspx.cs文件中设置变量值aspx文件在asp.net?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I codeD为

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

        PageId = 12;
    }
}

和我Image.aspx文件codeD为

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>

和我ImageCSharp.aspx.cs文件codeD为

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();
        }
    }
}

请帮助我如何code的的ImageUrl =ImageCSharp.aspx ImageID = &LT;%=的PageId%> 的正确...的PageId应变量。

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

推荐答案

只需设置它的背后code:

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;
}

这篇关于如何从aspx.cs文件中设置变量值aspx文件在asp.net?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆