簡體   English   中英

Asp.Net(C#)跨頁回發

[英]Asp.Net(C#) Cross-Page Postback

我已經嘗試使用跨頁面回發從page1到page2中的控件訪問值,如下所示:

我的page1(Default.aspx)有一個LinkBut​​ton,在第一次加載頁面時,我在其中存儲一些信息:

<asp:LinkButton ID="btnNoticia" 
runat="server" Text="Leia ++" 
CommandName="NoticiaID" 
CommandArgument='<%# Eval("NoticiaID")%>' 
EnableViewState="True" 
PostBackUrl="Noticias.aspx" 
/> 

在我的page2(Noticias.aspx)中,我正在像這樣從“ btnNoticia”中恢復值:

LinkButton btnLeiaMaisDefault = (LinkButton)Page.PreviousPage.FindControl("btnNoticia");

但是找不到上一頁發布的控件。 我正在獲取“ btnLeiaMaisDefault”的空值。

有想法嗎

PS:page1中的LinkBut​​ton ID =“ btnNoticia”在UpdatePanel內部。

謝謝

喬西

像這樣瀏覽您的網址

PostBackUrl =“〜/ Default.aspx”

編碼為

受保護的void Page_Load(對象發送者,System.EventArgs e)

{

    TextBox pvProductID = (TextBox)PreviousPage.FindControl("TextBox1");

    TextBox pvProductName = (TextBox)PreviousPage.FindControl("TextBox2");
    Label1.Text ="You came from: "+ PreviousPage.Title.ToString();        
    Label2.Text = "Product ID: " + pvProductID.Text.ToString();
    Label2.Text += "<br />Product Name: " + pvProductName.Text.ToString();

    string imageSource = "~/Images/" + pvProductID.Text + ".jpg";
    Image1.ImageUrl = imageSource;
    Image1.BorderWidth = 2;
    Image1.BorderColor = System.Drawing.Color.DodgerBlue;
}

我嘗試過....它有效...

您不能像那樣簡單地使用FindControl。 因為您的控件可能在另一個控件下,所以您需要一個遞歸函數來迭代所有控件及其后代以獲取指定的控件。

您可以將linkbutton控件置於面板控件下,並通過以下方式進行訪問:

LinkButton btnLeiaMaisDefault = (LinkButton)Page.PreviousPage.Panel1.FindControl("btnNoticia");

或其他方式正在使用遞歸函數:

private Control FindControlRecursive(Control root, string id) 
{ 
    if (root.ID == id)
    { 
        return root; 
    } 

    foreach (Control c in root.Controls) 
    { 
        Control t = FindControlRecursive(c, id); 
        if (t != null) 
        { 
            return t; 
        } 
    } 

    return null; 
} 

試試這個代碼...它在我這邊工作.......

if (Page.PreviousPage!=null)
        {
            LinkButton btnLeiaMaisDefault = (LinkButton)Page.PreviousPage.FindControl("btnNoticia");
        }

希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM