Reputation:
I have tryed access values from controls in page1 to page2 using cross page postback like following:
My page1(Default.aspx) has a LinkButton where I store some information when the page is first loaded:
<asp:LinkButton ID="btnNoticia"
runat="server" Text="Leia ++"
CommandName="NoticiaID"
CommandArgument='<%# Eval("NoticiaID")%>'
EnableViewState="True"
PostBackUrl="Noticias.aspx"
/>
In my page2(Noticias.aspx) I'm recovering the values from "btnNoticia" like that:
LinkButton btnLeiaMaisDefault = (LinkButton)Page.PreviousPage.FindControl("btnNoticia");
But it can't find the control posted by the previous page. I'm getting a null value for "btnLeiaMaisDefault".
Some idea?
PS: LinkButton ID="btnNoticia" in page1 is inside an UpdatePanel.
Thank you
Josi
Upvotes: 0
Views: 1735
Reputation: 1007
Navigate your url like
PostBackUrl="~/Default.aspx"
Code as
protected void Page_Load(object sender, 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;
}
I tried ....It works...
Upvotes: 1
Reputation: 81721
You can't simply use FindControl like that. Because your control may be under another control so you need a recursive function to iterate all the controls and their descendants to get the specified control.
You can put your linkbutton control under a panel control and access it with this way :
LinkButton btnLeiaMaisDefault = (LinkButton)Page.PreviousPage.Panel1.FindControl("btnNoticia");
or other way is using recursive function :
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;
}
Upvotes: 1
Reputation: 1768
Try this code...it is working on my side.......
if (Page.PreviousPage!=null)
{
LinkButton btnLeiaMaisDefault = (LinkButton)Page.PreviousPage.FindControl("btnNoticia");
}
hope it helps.
Upvotes: -1