如何将参数从一个页面传递到另一个页面?

时间:2012-01-10 01:11:44

标签: c# asp.net events

我有以下代码:

public partial class _Default : System.Web.UI.Page
{
      List<GlassesCollection> gc= BL.Example.GetCategory() ;

    protected void Page_Load(object sender, EventArgs e)
    {
        rpt1.DataSource = gc;
        rpt1.DataBind();
    }

    protected void rpt1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {

     Button btn = (Button)e.Item.FindControl("btn1");
     btn.CommandArgument = DataBinder.Eval(e.Item.DataItem,"CollectionID").ToString();
   }
}    

我想将btn.CommandArgument的内容传递给放置在另一个ASPX.CS文件中的Label事件。 有没有办法实现这个? 提前谢谢!

3 个答案:

答案 0 :(得分:1)

您需要使用Session。将值放入Session并在另一页中读取。

Session["key"]=value;

答案 1 :(得分:0)

您可以使用QueryStrings。例如,您的网址可能如下所示:

string url = String.Format("http://www.example.com/somepage.aspx?labeltext={0}",btn.CommandArgument);

然后,在somepage.aspx中,您可以:

//"labeltext" is the same name we used above as the ID
string lblText = Request.QueryString["labeltext"];
if (lblText != null)
{ 
   myLabel.Text = lblText;
}

如果文字可能不适合传入网址,您可以使用HttpServerUtility.UrlEncode对其进行编码,然后使用 HttpServerUtility.UrlDecode对其进行解码,然后再将其分配给标签。

答案 2 :(得分:0)

使用查询字符串:

Response.Redirect("AnotherPage.aspx?CommandArgument=SomeArgument");

然后阅读AnotherPage.aspx

上的QueryString
string commArgument = Request.QueryString["CommandArgument"];
相关问题