Last post May 26, 2011 01:45 AM by kanthaa
May 19, 2011 08:48 AM|Rouchie|LINK
I've created a custom control whereby I have a number of Panels (defined by the user) stored in a collection. When rendered, I need each Panel to render a LinkButton with a CommandArgument.
When I try this, there are two problems:
Can anyone help me out? I've Google this and found lots of similar posts but with no answer.
Here's my render code which currently isn't doing what it should...
Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)
If MyPanelsCollection IsNot Nothing AndAlso MyPanelsCollection.Count > 0 Then
For Each p As Panel In MyPanelsCollection
Dim lb As New LinkButton
lb.CommandArgument = MyPanelsCollection.IndexOf(p).ToString
AddHandler lb.Command, AddressOf LinkButtonCommandSub
lb.Text = "Link " & MyPanelsCollection.Indexof(p).toString ' text set to Link 0, Link 1 etc'
I'm not implementing IPostBackEventHandler or IPostBackDataHandler as I don't need to bubble the event up into the Page (it can be handled solely by the custom control). Not sure if that is correct or not however...
May 19, 2011 10:14 AM|bmains|LINK
by the ID, callis its RaisePostBackEvent method with the command name info. So you have to implement IPostBackEventHandler; i don't think the handler approach will work.
To do this, you may have better luck instead writing the link directly. You can render the postback script using the ClientScriptManager.GetPostBackClientHyperlink. Here is a good example: http://msdn.microsoft.com/en-us/library/ms153108.aspx
May 20, 2011 10:58 AM|Rouchie|LINK
Thanks for your post. I've been doing quite a bit of debugging and testing for this (I mean an absolute TON of debugging - who says the .NET framework is intuitive...!).
I read elsewhere that all controls should be created within the Init() phase. I did this, then used Me.FindControl("...") at the
Render() stage, which worked really well. Also during Init() I could use the
AddHandler() command to wire them up to a PostBack sub, which also works well.
My question now however is this... Given that my approach does not implement IPostBackEventHandler, but seems to do achieve exactly the same as IPostBackEventHandler does, what is the point of using IPostBackEventHandler? If I implement IPostBackEventHandler
into my control, then Visual Studio automatically adds a new sub, RaisePostBackEvent
into my code, but what does this do exactly, and is it any better to use this than my existing approach?
May 24, 2011 12:11 AM|kanthaa|LINK
As i can understand the problem , you have some collection values and you need those many linkbuttons , so try to write in render like below
instead of link button , please see how the link button is rendering , so try to add <a with href="...." > collectontext</a> .
i think it will help , plese try to add href if any url or some thing exists , don't append href related text is there .
try this...- :)
May 24, 2011 03:30 AM|Rouchie|LINK
You've misunderstood my problem. I would like to know why I should implement IPostBackEventHandler instead of using AddHandler for LinkButtons?
AddHandler works perfectly well, but I can't find any information on why IPostBackEventHandler is the best approach...?
May 24, 2011 10:29 PM|Decker Dong - MSFT|LINK
As far as I know, "IPostBackEventHandler" 's most advantage is to raise the event at server-side, which means that when you submit the button by clicking it, it can pass parameters into the method
public void RaisePostBackEvent(string eventArgument)
//Do to deal with the argument
class="line alt1" style="background-image: none; position: static !important; text-align: left !important; background-color: #ffffff; font-style: normal !important; margin: 0px; outline-color: !important; outline-width: 0px !important; width: auto !important;
bottom: auto !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; float: none !important; height: auto !important; font-size: 1em !important; vertical-align: baseline !important; right: auto !important;
font-weight: normal !important; top: auto !important; left: auto !important; background-origin: initial; background-clip: initial; border-width: 0px !important; padding: 0px !important;">Thus you can process with the eventargument to specify something to do</div>
May 25, 2011 12:33 AM|kanthaa|LINK
You need to know why IPostBackDataHandler...?
Let say if you are creating custom control by inheriting any existing control (ex: textbox) You no need to worry about how to maintan the value during postback.
Let say if you are inheriting from base class (Control or CompositeDataBoundControl..) then you need to make sure while postback you need to maintain the data.
If your control inheriting from IPostbackDatahandler then you need to implement LoadPostData and if you want to raise any event then RaisePostDataChanged event.
So in LoadPostaData event you have two parameters like which one causes the postback and postcollection(collection of controls from browser before requesthappes) then you need to verify that value is there are not?
this will be help full while dealing with CheckBox and RadioButton ( because unched or unselected controls won't maintain in postcollection)
Let say i have changed my value in textbox so my resposnibility is i need to maintain that value in postback and the same time if i want textchanged event then you need to return true from LoadPostData method then it will come to RaisePostDataChanged event
Please make sure if you want ot work above these two methods then you need to write
Page.RegisterRequiresPostBack(this); in OnInIt
I hope you can understand.
May 25, 2011 04:39 AM|Rouchie|LINK
From your explanations, I think I understand what you're saying, but I think I am still missing something...! Am I correct in thinking that if I was creating a new control that did
not use existing controls that can postback (e.g. LinkButtons) then it would be essential to implement IPostBackEventHandler?
Let me tell you what I have so far (which works perfectly) then please advise on whether IPostBackEventHandler will make things any better? My control simulates a tabbed layout, with Panels holding content, and LinkButtons being used to show a respective
The basic code to create the LinkButton is as follows:
Protected Overrides Sub CreateChildControls()
' add panels (removed) ...
' create linkbuttons with postback functionality to use in tabs
For Each p As Panel In Panels
Dim l As New LinkButton
l.ID = "TabLink_" & Panels.IndexOf(p)
l.CommandArgument = Panels.IndexOf(p).ToString
l.Text = p.LinkTabTitle
AddHandler l.Command, AddressOf PanelIndexChanging
Public Delegate Sub IndexChangedHandler(ByVal sender As Object, ByVal e As CommandEventArgs)
Public Event IndexChanged As IndexChangedHandler
Protected Sub PanelIndexChanging(sender As Object, e As CommandEventArgs)
Me.ChosenIndex = Convert.ToInt16(e.CommandArgument)
RaiseEvent IndexChanged(sender, e)
May 26, 2011 01:45 AM|kanthaa|LINK
IPostBackDataHandler - for mainitaining the state of your custom control.
IPostBackEventHandler is for raising event in postback. Any way LinkButton it self inherited from that Interface.(Defines the method ASP.NET server controls must implement to handle postback events)
For your case no need of inheriting.
Use refelctor and try to see the code for LinkButton you can understand very well.