将变量传递到同一页面而不使用查询字符串

时间:2011-12-23 22:31:47

标签: c# asp.net

我想更改下面的代码,以便我不必使用查询字符串。我想设置两个变量yesIDnoID。我正在从default.aspx导航到同一页default.aspx。这可能吗?如果是这样的话?

//get query string
if (Request.QueryString["yesId"] != null && Request.QueryString["noId"] != null)
{
    int yesPictureId = Convert.ToInt32(Request.QueryString["yesId"]);
    int noPictureId = Convert.ToInt32(Request.QueryString["noId"]);

    //Set rated image Items to Visible
    RatedImage.Visible = true;
    HyperLink1.Visible = true;
    RatedPicRating.Visible = true;

    //pass ratings to database
    Ratings PassRatings = new Ratings();
    PassRatings.InsertRatings(yesPictureId, 1);
    PassRatings.InsertRatings(noPictureId, 2);

    //Get total yes and nos and Do Calculation 
    Ratings GetNoVotes = new Ratings();
    int DATotalYesVotes = GetNoVotes.GetTotalNOVotes(1, yesPictureId);
    int DaTNoVotes = GetNoVotes.GetTotalNOVotes(2, yesPictureId);
    int DaTotalVotes = DATotalYesVotes + DaTNoVotes;
    double Percentage = ((double)DATotalYesVotes / (double)DaTotalVotes) * 100;
    //Round percentage
    Percentage = Math.Round(Percentage, MidpointRounding.AwayFromZero);

    //Insert New percentage
    Picture UpdatePictureTating = new Picture();
    UpdatePictureTating.UpdateRatings(yesPictureId, (int)Percentage);

    //Create pictue object
    Picture RatedPic = new Picture();
    DataTable DARatedPicture = RatedPic.GetRatedPicByQueryString(yesPictureId);

    //Assign Location and Rating to variables
    foreach (DataRow row in DARatedPicture.Rows)
    {
        // firstRatedPicId = row["PicID"].ToString();
        //secondNoPicId = firstYesPicId;
        //holds member Id for profile link
        int MemberID = (int)row["MemberID"];
        RatedPicnameLabel.Text = row["MemberName"].ToString() + "'s profile";
        HyperLink1.NavigateUrl = "Member.aspx?UserID=" + MemberID;
        RatedPicRating.Text = "Banged Rating: " + row["PicRating"].ToString() + "%";
        RatedImage.ImageUrl = "Pictures/" + row["PicLoc"];
        RatedImage.PostBackUrl = "Member.aspx?UserID=" + MemberID;
    }

}
else
{
    //If we dont have any ratied pictures hide those elements
    RatedImage.Visible = false;
    HyperLink1.Visible = false;
    RatedPicRating.Visible = false;
}

我在下面设置变量。我不想使用查询字符串。

FirstPicLink.PostBackUrl = "default.aspx?yesId=" + firstYesPicId + "&noId=" + firstNoPicId;
SecondPicLink.PostBackUrl = "default.aspx?yesId=" + secondYesPicId + "&noId=" + secondNoPicId;

我在想这样的事情。但是我如何存储这些变量?重新加载页面后,它们不会丢失。

yesID = 1
NoID = 2
FirstPicLink.PostBackUrl = "default.aspx";
SecondPicLink.PostBackUrl = "default.aspx";

3 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

默认情况下,导致回发的控件(如按钮)会将页面提交回自身。所以你不需要使用PostBackUrl属性。

关于持久化变量的价值:

a)您可以将它们存储在隐藏字段中:检查控件。

<asp:HiddenField id="yesID" runat="server" value="1"/>

b)您可以使用会话变量..

Session["yesID"] = 1;

c)Viewstate

ViewState["yesID"] = 1;

然后在页面加载期间再次阅读它们。

答案 2 :(得分:0)

IMO VeiwState应该最适合您,因为您将数据发布回同一页面,并且比您的特定情况的会话效率更高

ViewState["yesID"] = 1;
ViewState["noID"] = 2;