Answered by:
LinkButton PostBack handler in custom control

Question
-
User1291940830 posted
Hi,
Pls I'm having a problem with dynamically created LinkButtons in
a WebControl.Actually I'm implementing a Tab control with multiple tabs and I'm using LinkButtons for the Tab headers... u get the picture.There is a TabVIewContainer containing multiple TabView items contained in a TabViewCollection.The TabViewContainer registers for an event raised by each TabView header linkbutton.
So, when a LinkButton for a tab is clicked, its supposed to fire an event that will be handled by the tabcontainer to change the ActiveTabViewIndex i.e. to switch views to another tab.
Problem is the markup generated looks okay( the href=__doPostBack part), but for reasons I cannot explain, the event handler is not been called, both from the TabView control itself and the TabContainer.
Tried implementing IPostBackEventHandler, but did'nt help too.
Any help will be greatly appreciated.
Thanks,
Ozzy
Thursday, July 24, 2008 9:58 AM
Answers
-
User481221548 posted
Hi Ozzy
Assign IDs to your LinkButtons, then that should work.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, July 24, 2008 1:08 PM -
User-1407570774 posted
Hi there,
I have a diffrent way.
1 - Implement IPostbackEventHandler:
1 public class TabControl : CompositeControl, IPostBackEventHandler, INamingContainer
2 {
3 4 #region IPostBackEventHandler Members 5 6 void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
7 {
8 throw new System.NotImplementedException();
9 }
10 11 #endregion 12 13 }
2 - Render Headers:1 public virtual TagPageCollection Pages
2 {
3 get { ... }
4 }
5 6 protected override void RenderContents(HtmlTextWriter writer)
7 {
8 for (int i = 0; i < this.Pages.Count; i++)
9 {
10 writer.AddAttribute(HtmlTextWriterAttribute.Href, Page.ClientScript.GetPostBackClientHyperlink(this, i)); // i is page index
11 writer.RenderBeginTag(HtmlTextWriterTag.A);
12 writer.RenderEndTag();
13 }
14 ...
15 }When you select a tab RaisePostBackEvent will be called. And eventArgument's value is page index.
Hope this helps.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, July 24, 2008 1:29 PM
All replies
-
User481221548 posted
Hi Ozzy and welcome
How are you apply the dynamic controls?
Please provide us some sample code, how do done that.Thursday, July 24, 2008 10:54 AM -
User1291940830 posted
Hi Peter,
The ASP.Net markup for the control looks like this:
<cc:TabViewContainer id="container" runat="server">
<Views>
<cc:TabView runat="server" ID="tab1" HeaderText="Tab 1" >
<!----some content------------!>
</cc:TabView>
<cc:TabView runat="server" ID="tab2" HeaderText="Tab 2">
<!----some content------------!>
</cc:TabView>
</Views>
</cc:TabViewContainer>
----end----
its supposed to display all the headers as LinkButtons
Code from TabView to add LinkButtons
----------------------------------------------------------
protected internal virtual void RenderHeader ( HtmlTextWriter writer )
{
if ( _header != null )
{
_header.HtmlTag = System.Web.UI.HtmlTextWriterTag.Span;if ( Active )
{
_header.CssClass = _owner.ActiveHeaderCssClass;
}
else
{
_header.CssClass = _owner.InActiveHeaderCssClass;
}
LinkButton lnk = new LinkButton();
lnk.Text = HeaderText;
lnk.Attributes ["ViewIndex"] = ViewIndex.ToString(); ;lnk.Click += new EventHandler(OnSelectView);
_header.Controls.Add(lnk);
_header.RenderControl(writer);
}
else
{
_header = new ControlContentPanel();
_header.HtmlTag = System.Web.UI.HtmlTextWriterTag.Span;
if ( Active )
{
_header.CssClass = _owner.ActiveHeaderCssClass;
}
else
{
_header.CssClass = _owner.InActiveHeaderCssClass;
}
_header.RenderControl(writer);
}
}
Handler code from TabView for lnkButton_OnClick
---------------------------------------------------------------------------
protected void OnSelectView ( object sender, EventArgs e )
{EventHandler Handler = Events [_eventViewSelected] as EventHandler;
if ( Handler != null ) Handler(sender, e);
}
void IPostBackEventHandler.RaisePostBackEvent ( string eventArgument )
{LinkButton lnk = new LinkButton();
lnk.Text = HeaderText;
lnk.Attributes ["ViewIndex"] = ViewIndex.ToString(); ;
EventHandler Handler = Events [_eventViewSelected] as EventHandler;
if ( Handler != null ) Handler(lnk, EventArgs.Empty);}
Code to handle TabClick from TabViewContainer
---------------------------------------------------------------------
protected internal void OnViewSelected ( object sender, EventArgs e )
{
LinkButton lnk = sender as LinkButton;
int index =int.Parse( lnk.Attributes ["ViewIndex"]);
ActiveViewIndex = index;
}
The display is fine and the LinkButtons actually posts back, but my handlers are not called.
Thanks for any help,
Ozzy
Thursday, July 24, 2008 11:31 AM -
User481221548 posted
Hi Ozzy
Assign IDs to your LinkButtons, then that should work.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, July 24, 2008 1:08 PM -
User-1407570774 posted
Hi there,
I have a diffrent way.
1 - Implement IPostbackEventHandler:
1 public class TabControl : CompositeControl, IPostBackEventHandler, INamingContainer
2 {
3 4 #region IPostBackEventHandler Members 5 6 void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
7 {
8 throw new System.NotImplementedException();
9 }
10 11 #endregion 12 13 }
2 - Render Headers:1 public virtual TagPageCollection Pages
2 {
3 get { ... }
4 }
5 6 protected override void RenderContents(HtmlTextWriter writer)
7 {
8 for (int i = 0; i < this.Pages.Count; i++)
9 {
10 writer.AddAttribute(HtmlTextWriterAttribute.Href, Page.ClientScript.GetPostBackClientHyperlink(this, i)); // i is page index
11 writer.RenderBeginTag(HtmlTextWriterTag.A);
12 writer.RenderEndTag();
13 }
14 ...
15 }When you select a tab RaisePostBackEvent will be called. And eventArgument's value is page index.
Hope this helps.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, July 24, 2008 1:29 PM