2011-12-23 49 views
0

我想在下面更改我的代碼,以便不必使用查詢字符串。我想設置兩個變量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"; 

回答

0

默認情況下,進行回傳(如按鈕)提交頁面回自己的控制。所以你不需要使用PostBackUrl屬性。

至於持續的變量值:

a)你可以將它們保存在一個隱藏字段:檢查控制。

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

b)你可以使用session變量..

Session["yesID"] = 1; 

C)視圖狀態

ViewState["yesID"] = 1; 

,然後頁面加載時再次閱讀。

0

IMO VeiwState應該最適合你,你正在發佈的數據回到同一頁,並且遠遠超過會議更高效的爲你的具體情況

ViewState["yesID"] = 1; 
ViewState["noID"] = 2; 
相關問題