Using class in App_code - Finding controls on the page that uses that class
Hope this will be understandable. Going to simplify the code alot.
I have a Master page, then i have Default.aspx. In this Default page i have the following.
<asp:Content ID="Content2" ContentPlaceHolderID="CPH_Main" Runat="Server">
<asp:Panel ID="Panel1" runat="server">
</asp:Panel>
</asp:Content>
Ok, so i have a class in the App_Code folder.
Lets say that on page_load on default.aspx i start a function that is called "Populate_Content". I start it with giving it two parameters, the Panel which the items should be at and a ID to select from db.
So it goes like this Populate_Content(Panel1, 4)
The function selects from the db and at runtime makes Linkbuttons and adding them to the Panel1, it also gives them a eventhandler for the click event.
The problem is that I'm trying to get the "Populate_Content" function to work in the clickevent that is stored in the App_Code folder. How can i select the control (Panel1) in the class file that is inside the App_Code folder. I can get the id of the selected linkbutton with this approach
//This the class inside App_Code
void description_Click(object sender, EventArgs e)
{
string id = ((LinkButton)sender).ID.ToString();
//Then i need to do this.. but i need to find the "Panel1" controller
//Populate_Content("Panel1", id);
}
But i need to start the "Populate_Content" in the click event also. So i need the to find the "Panel1" controler that is in the default.aspx.
I have tried to use the page property on the sender like so. But no go.
Page def_page = ((LinkButton)sender).Page;
Panel panel1 = (Panel)def_page.FindControl("Panel1");
I though the page property would give me the page where the controller is, i cant find anything about this on google.
Am I doing this wront or is there another way ?
Edit: This the Populate_content
public void Populate_content(Panel cont, string treenode)
{
var query = from n in dc.Nemanet_Navigations
where n.UserId == userGuid && n.Nav_pID.ToString() == treenode
orderby n.Nav_Name
select n;
foreach (var item in query)
{
if (item.Nav_IsFile == true)
{
Panel div =开发者_如何学C new Panel();
div.CssClass = "BulletDiv";
cont.Controls.Add(div);
Image picture = new Image();
picture.ImageUrl = "~/Icons/Nytt_skjal.png";
div.Controls.Add(picture);
div.Controls.Add(new LiteralControl("<br/>"));
LinkButton description = new LinkButton();
description.Text = item.Nav_Name;
description.ID = item.Nav_ID.ToString();
description.Click += new EventHandler(description_Click);
div.Controls.Add(description);
}
else if (item.Nav_IsFile == false)
{
Panel div = new Panel();
div.CssClass = "BulletDiv";
cont.Controls.Add(div);
Image picture = new Image();
picture.ImageUrl = "~/Icons/Ny_mappa.png";
div.Controls.Add(picture);
div.Controls.Add(new LiteralControl("<br/>"));
LinkButton description = new LinkButton();
description.Text = item.Nav_Name;
description.ID = item.Nav_ID.ToString();
description.Click += new EventHandler(description_Click);
div.Controls.Add(description);
}
}
}
精彩评论