Web Custom Control: getting a updated value on Load event after a postback

  • Thread starter Thread starter ronchese
  • Start date Start date
R

ronchese

Hello.

I wrote a Web Custom Control that have a property named SelectedValue. This SelectedValue property is set after the user clicks a LinkButton control, by its Click() event.

I did some tests, and I noticed that my handled LinkButton_Click() event is fired only after the Page_Load() event, due to page and control lifecicles.

The result of this is, when in Page_Load(), I can only catch the SelectedValue from a previous postback (out of sync).

BUT.... I know and yet did a test with a RadioButtonList. I can retrieve the updated SelectedValue in the Page_Load event with no problem!!

Anyone know the trick so I can update my SelectedValue before the Page_Load() event, soon after the user clicks a LinkButton?



Example (just relevant pieces - better viewed in RichText format):

WebCustomControl:
'retrieves from/sets to viewstate. This property is started with zero ("0")
<DefaultValue(False), Browsable(False)> _
Public Property SelectedValue() As Integer
Get
Return CInt(ViewStateClass.Retrieve(ViewState, "SelectedValue", "0"))
End Get
Set(ByVal value As Integer)
ViewStateClass.Save(ViewState, "SelectedValue", CInt(value))
End Set
End Property

'this is where I set the SelectedValue (this event was previously attached via AddHandler)
Private Sub LinkButtonClick(ByVal sender As Object, ByVal e As System.EventArgs)
Me.SelectedValue = lnk.CommandArgument
End Sub

Page:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Label1.Text = MyControl.SelectedValue '<= on first postback, it returns "0", even
'if the user clicks a LinkButton that returns 20. On second postback, that 20 will
'be returned. Or shortly, it is out of sync.
End Sub
 
"ronchese" <info(a)carsoftnet.com.br> wrote in message
Hello.

I wrote a Web Custom Control that have a property named SelectedValue. This
SelectedValue property is set after the user clicks a LinkButton control, by
its Click() event.

I did some tests, and I noticed that my handled LinkButton_Click() event is
fired only after the Page_Load() event, due to page and control lifecicles.

The result of this is, when in Page_Load(), I can only catch the
SelectedValue from a previous postback (out of sync).

BUT.... I know and yet did a test with a RadioButtonList. I can retrieve the
updated SelectedValue in the Page_Load event with no problem!!

Anyone know the trick so I can update my SelectedValue before the
Page_Load() event, soon after the user clicks a LinkButton?

http://www.15seconds.com/issue/020102.htm

I don't know. Maybe using a Session variable and understanding the ASP.NET
Page Life Cycle and where you can possibly intercept things.
 
Back
Top