locked
How to create LinkButtons in a custom control so they post back RRS feed

  • Question

  • User1090916182 posted

    Hi

    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:

    1. The LinkButton simply renders as a normal <a></a> on the page, with no javascript added to cause the postback
    2. Because of the above, no postback occurs so I can't even test whether the CommandArgument is getting posted back!

    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
    		writer.RenderBeginTag(HtmlTextWriterTag.Div)
    		For Each p As Panel In MyPanelsCollection
    			writer.RenderBeginTag(HtmlTextWriterTag.Div)
    			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'
    			lb.RenderControl(writer)
    			writer.RenderEndTag()
    		Next
    		writer.RenderEndTag()
    	End If
    End Sub

    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...

     

    Thursday, May 19, 2011 8:48 AM

Answers

  • User-294030300 posted

    Hey,

    IPostBackEventHandler is not for bubbling up to the page; it's for handing an event within one of its controls.  For instance, if you write client-side JavaScript postback: __doPostBack('<control unique id>', '<command name>');, the framework finds the control 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

    HTH.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 19, 2011 10:14 AM
  • User3866881 posted

    Hello :)

    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

            }

    Thus you can process with the eventargument to specify something to do

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 24, 2011 10:29 PM
  • User-146703869 posted

    ok.

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 26, 2011 1:45 AM

All replies

  • User-294030300 posted

    Hey,

    IPostBackEventHandler is not for bubbling up to the page; it's for handing an event within one of its controls.  For instance, if you write client-side JavaScript postback: __doPostBack('<control unique id>', '<command name>');, the framework finds the control 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

    HTH.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 19, 2011 10:14 AM
  • User1090916182 posted

    Hi

    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?

    Friday, May 20, 2011 10:58 AM
  • User-146703869 posted

    HI,

    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="...." > collecton[0]text</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...- :)

     

    Tuesday, May 24, 2011 12:11 AM
  • User1090916182 posted

    Hi Kanthaa

    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...?

    Tuesday, May 24, 2011 3:30 AM
  • User3866881 posted

    Hello :)

    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

            }

    Thus you can process with the eventargument to specify something to do

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 24, 2011 10:29 PM
  • User-146703869 posted

    HI ,

    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 method.

     I hope you can understand.

     

    Wednesday, May 25, 2011 12:33 AM
  • User1090916182 posted

    Hi

    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 tab link:

    The basic code to create the LinkButton is as follows:

    Protected Overrides Sub CreateChildControls()
    	Me.Controls.Clear()
    	' 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
    		Me.Controls.Add(l)
    	Next
    End Sub
    
    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)
    End Sub
    The above code works very well.  It creates the LinkButtons, causes postbacks, catches postback data, and raises and event to the page.  Could you please tell me if IPostBackEventHandler is needed, or if IPostBackEventHandler is a better/more secure/more reliable way to achieve the same?  I found an explanation of IPostBackEventHandler at Microsoft, but it seems to work virtually in the same way as my code...!

    Wednesday, May 25, 2011 4:39 AM
  • User-146703869 posted

    ok.

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 26, 2011 1:45 AM