What I am trying to do is to generate Linkbuttons whenever a user click on the static LinkButtons that are inside my aspx looks like this:
They look like this:
<asp:LinkButton ID="lnkSubWeb" runat="server" OnCommand="lbWeb_Click" />
The method that fires is following:
/// <summary>
/// Handles the Click event of the lbWeb control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="CommandEventArgs"/> instance containing the event data.</param>
protected void lbWeb_Click(object sender, CommandEventArgs e)
{
var url = e.CommandArgument.ToString();
using (var site = new SPSite(url))
{
using (var selectedWeb = site.OpenWeb())
{
ViewState["RelativeUrl"] = selectedWeb.ServerRelativeUrl;
ViewState["WebUrl"] = selectedWeb.Url;
LoadWebItems(selectedWeb);
}
}
}
From what I know the page cycle works like this Page load -> Button click -> Page prerender.
So basicly when the user click on this linkbutton I want generate 5 linkbuttons dynamicly and also add and give them the lbWeb_Click
event, but it aint getting fired. Nothing happens when I click on the generated LinkButtons.
So when I have stored the data to ViewState I use following method to generate these linkbuttons:
private void Page_PreRender(object sender, EventArgs e)
{
RelativeUrl = (string)ViewState["RelativeUrl"];
WebUrl = (string)ViewState["WebUrl"];
if (!string.IsNullOrEmpty(RelativeUrl))
{
LoadBreadCrumb(RelativeUrl, WebUrl);
}
}
The LoadBreadCrumb()
method looks like this:
private void LoadBreadCrumb(string relativeurl, string weburl)
{
var relativePath = relativeurl.Substring(1);
var urlList = weburl.ParseUrl();
var linkList = new List<string>(relativePath.Split('/'));
for (int i = 0; i < linkList.Count; i++)
{
var link = UppercaseFirst(linkList[i]);
var linkButton = new LinkButton
{
Text = link + " > ",
CssClass = "breadcrumb",
CommandArgument = urlList[i]
};
linkButton.Command += lbWeb_Click;
bcHolder.Controls.Add(linkButton);
}
}
So basicly this codes generates the linkbuttons exacly as the way I want but the lbWeb_Click
is not getting fired on those.
In other hand I have tried to put this code in Page_Load and that doesnt work well, its always one step behind. The LinkButtons aint getting displayed after a click event, but its generated after a Page_Load and then the click event on them actually works.
Any kind of help is appreciated
You are creating LinkButton
instances after the page life-cycle passes the point where events are being processed. Hence at the time your event handler should have been called, there in fact no LinkButton
instances in the Page
and thus cannot be invoked.
You should create all dynamic controls (i.e. invoke LoadBreadCrumb
) within the context of the PreInit
event. See the ASP.NET page life-cycle documentation in MSDN.
Side note: They way you build the breadcrumb is a bit suboptimal in respect to URL path construction options in SharePoint. You should take into account that there are managed paths which do not correspond to any site collection (SPSite
) directly, and that there are subsites (SPWeb
nested within a SPSite
's root web site) along the path. Currently, by calling new SPSite(url)
with an argument that is either a managed path or a subsite URL, you will end up with the immediatelly enclosing site collection which in turn denotes a different URL than what's in url
.