all groups > asp.net building controls > january 2004 >
You're in the

asp.net building controls

group:

How to add postback function in Custom Server Controls


How to add postback function in Custom Server Controls ton
1/29/2004 1:38:49 PM
asp.net building controls:
In the procedure CreateChildControls I add several controls like check and
textboxes, button link and so on. How can I get the postback respons on the
button link:
The functions:
lblButtonLink_Command and lblButtonLink_Click will not be executed

Please help

Ton


Re: How to add postback function in Custom Server Controls Alessandro Zifiglio
1/29/2004 6:37:54 PM
ton,
what do you want to do with the caputred event ?
If all you need to do is capture it and take action in your control itself
then you can easily do this :
1.In createchildcontrols as you add your control that posts back, a button
control for example :
Dim button1 As New Button()
button1.Text = "Submit"
Controls.Add(button1)
'Notice the event handler defined below
AddHandler button1.Click, AddressOf Me.ButtonClicked

now define the buttonclicked sub in your control and take action there,
everytime the button is clicked this will fire :
Private Sub ButtonClicked(sender As [Object], e As EventArgs)
'What to do when button is clicked
End Sub
Should work for you, however I havent tested, but theorically this is all
you need if this is your requirement. This is useful if you need to take
action without exposing the event to the container page, giving the
consumers of your control no control, as this event is not exposed to them.

for a complete example that digs deeper into this you might want to look at
the following link :
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconcompositecontrolsample.asp


Instead if you need to expose this event to the page where your control
sits, and exposed to the end users of your control then you will need to
bubble it up .
Here is a very nice example of how this can be done for composite controls
on MSDN.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconeventbubblingcontrolsample.asp

[quoted text, click to view]

Re: How to add postback function in Custom Server Controls ton
1/29/2004 8:47:37 PM
Thanks for you reply

I've used the code you've send me. It did not work. I made my control a lot
simpeler and the result was the same.
It looked as if the button click event were not handled by te procedure of
the addresoff statement:

The code looks now like: (And I'm sure that the createchildcontrols is
called instaed of the the buttonclick procedure)

Protected Overrides Sub CreateChildControls()
If Me.Page.IsPostBack Then ' this is what runs after the button click
''Exit Sub '' (if I use Exit sub, no control is displayed)

End If

But = New Button

But.Text = "Submit"

But.ID = ClientID & "X"

Controls.Add(But)

AddHandler But.Click, AddressOf Me.ButtonClick

ChildControlsCreated = True

end Sub

Private Sub ButtonClick(ByVal sender As Object, ByVal e As System.EventArgs)

But = FindControl(ClientID & "X")

But.Text = "xxxx"

Controls.Add(But)

End Sub

I've dimensioned the button But as a private control, did not help
íve done this with the withevents keyword, no difference
and i've dimensioned them in the procedures createchildrecords and
buttonclick

no differences

Ton

"Alessandro Zifiglio" <alessandrozifiglio@NO-SPAM-hotmail.com> schreef in
bericht news:9DbSb.1271$HO2.677@news.edisontel.com...
[quoted text, click to view]

Re: How to add postback function in Custom Server Controls Alessandro Zifiglio
1/29/2004 9:36:50 PM
Should fire. It works for me. However i have noticed that you are trying to
add controls in your button click. This will not work for you because during
postback those controls wont be recreated. so set a flag in viewstate as you
add the control.
Also I have noticed that you are trying to retrieve the same button, the one
that fired the event in its event click and add it to the controls
collection. This is not what what you need to do, but simply have the same
code run after postback. Whatever code you have in your createchildcontrols
method will run after postback, so let it all run as is and remove the
ispostback check in there --If that code is not run your event will never
fire, this is because the viewstate of the control is restored when the
control is reloaded into its container.

Private Sub ButtonClick(ByVal sender As Object, ByVal e As System.EventArgs)
Dim MyButton As New Button()
MyButton = CType(sender, Button)
context.response.write("Click event for " & MyButton.id & "Fired")
End Sub

http://samples.gotdotnet.com/quickstart/util/srcview.aspx?path=/quickstart/aspplus/samples/webforms/ctrlauth/composition/Composition2.src&file=VB\Composition2.vb&font=3

This is another example :
http://samples.gotdotnet.com/quickstart/util/srcview.aspx?path=/quickstart/aspplus/samples/webforms/ctrlauth/composition/Composition3.src&file=VB\Composition3.vb&font=3


[quoted text, click to view]

Re: How to add postback function in Custom Server Controls Alessandro Zifiglio
1/30/2004 10:15:33 AM
ton, Look at the post "No events are fired in child controls". Jeffery has
just pointed out how useful Implementing the InamingContainer interface
really is, especially when you need events in child controls routed. This is
why your events werent firing.

Iimplement this interface, and do not try passing an explicit id to your
control if you dont need it.

At the moment i myself am having problems passing an explict id to child
controls and i'm working on a workaround. Will postback when I do find a
way. One good reason to want to pass an explicit id is when using a
validator control and as you already know you need to pass your controls id
to its "controlToValidate" property.

If this is not your case then i dont see why it is important to pass an
explicit ID

"Alessandro Zifiglio" <alessandrozifiglio@NO-SPAM-hotmail.com> wrote in
message news:ZeeSb.1334$HO2.690@news.edisontel.com...
[quoted text, click to view]

Re: How to add postback function in Custom Server Controls Alessandro Zifiglio
1/30/2004 10:57:47 AM
oops, no workaround. That InamingContainer Interface had me a little
confused ;P
Since this interface already makes control ids unique you just need to pass
your id as is like

button1.id = "button1"
textbox1.id = "textbox1"
and so forth.
When trying to reference textbox1 for example just simply use its id
"Alessandro Zifiglio" <alessandrozifiglio@NO-SPAM-hotmail.com> wrote in
message news:cmpSb.1494$HO2.558@news.edisontel.com...
[quoted text, click to view]
Re: How to add postback function in Custom Server Controls ton
2/2/2004 11:25:41 AM
thank you
the event is fired now but what I had in my does not occur:
What I do is the following:
- I show a record contains of 10 fields. The properties are:
Tablename, RecordID, ShowFields (just a list of the fields to show)
now pressing a button should replace the existing recordcard by another
record card. For example a record contains a Contact. One of the field is
the (database)-link to the Customer. To click on that buttonlink, should
replace the form with for example 10 fields of the customer. You can say it
is a zoom function to the toplevel of the database.
For me this is inportant because it let me navigate in the database.
Another example:
when I want to modify the link I want to replace the control with a search
screen to show a list of customers, where the user can select one. Or to add
a complete knew one.

The question is: "Is this possible". I do not want the webdesigner to add
several controls on the form or to add several forms to the project to
handle these evenst.

Ton


"Alessandro Zifiglio" <alessandrozifiglio@NO-SPAM-hotmail.com> schreef in
bericht news:PZpSb.1500$HO2.700@news.edisontel.com...
[quoted text, click to view]
Re: How to add postback function in Custom Server Controls Alessandro Zifiglio
2/2/2004 1:51:29 PM
ton, Can you explain that again. Its not very clear what you want to do ;P


[quoted text, click to view]
Re: How to add postback function in Custom Server Controls ton
2/2/2004 2:45:41 PM
suppose you want to plan an activity. You get a card wich contains several
field like
date, begintime, end time, description, customer, type of activity and so
on
type of activity is a dropdonlist
description a textbox
and the customer fields points to a foreignkey with the customer-table (if
it is filled in I just show the customer name)
off cause i cannot use a drop down list because there will be hunderds or
thoussends of customers in the database
so when it is empty i can press a button to select one from a list (get e
search screen and then look up)
or i can add a new customer, in both situations I want to have a different
from as the replacement of my original activity:
- pick from a list will show me several search fields from the table
customer inclusive a Search button
- the add new customer will give me a recordcard with the important fields
to define a new customer
when the customer is add, or when the customer is selected from the list I
replace this card by the original content where the customer field shows the
name of the customer

So instead of multiple screens i just use the same screens to collect the
information to plan this activity

i hope you can help


Ton



"Alessandro Zifiglio" <alessandrozifiglio@NO-SPAM-hotmail.com> schreef in
bericht news:HOrTb.2542$HO2.1155@news.edisontel.com...
[quoted text, click to view]
http://samples.gotdotnet.com/quickstart/util/srcview.aspx?path=/quickstart/a
spplus/samples/webforms/ctrlauth/composition/Composition2.src&file=VB\Compos
ition2.vb&font=3
[quoted text, click to view]
http://samples.gotdotnet.com/quickstart/util/srcview.aspx?path=/quickstart/a
spplus/samples/webforms/ctrlauth/composition/Composition3.src&file=VB\Compos
ition3.vb&font=3
[quoted text, click to view]
Re: How to add postback function in Custom Server Controls Arthur Mnev
2/2/2004 2:55:11 PM
My experience to you:

1. You control must implement INamingContainer to create its own namespace
for event routing
2. you override CreateChildControl and construct them according to your
rules. It might be tricky you must remember that

Even though ASP.NET supports events it is still disconnected architecture
therefore once a server sends you a page it completely forgets about you
therefore, when you click on a button (or generate other event) what must
happen is:
1 - data sent to the server
2 - server constructs control tree (it MUST construct the same control
tree as you had when you outputted the controls /previous state)
3 - It checks which control supposed to receive an event (__EVENTTARGET
post variable contains the control name)
4 - An event is raised on behalf of control
5 - You capture the event

3. Clear the control list (Controls.Clear())
4. Load Conditions (or Cache) of your previous request (ViewState could be
used to store them)
5. Build Controls based on previous conditions (or cache) - by the time you
get out of the CreateChildControls() your ChildControlsCreated flag will be
set to true by the framework
6. Event delegate will get executed
7. Make sure your event handler alters the Control Condition and calls
(this.ChildControlsCreated = false). what that will do is place an
indication flag that controls have not been created, that will case the
framework to re-run CreateChildControls;
8. When CreateChildControls re-runs it Clears it's previous control list,
loads up Build Conditions and Generates the controls. Except the build
conditions are based now on your different Build conditions.


It is like you will be running the page twice - once to build an old state,
figure out what event to raise and raise that event. Once raised you will
re-run everything to determine second state and alter your presentation.

If that is not enough think about this. When an event is mapped to the
hierarchy, the hierarchy must be in the same exact order as it has been
before, otherwise an event will get mapped to the wrong control. This is
where you get CorruptedViewState error messages. Consider this:

User John (ctl1)
User Jane (ctl2)
User Jim (ctl3)

Each of the users is a button

You click on Jane, however, between the time you got the page and the time
you clicked user Jane was deleted, a server will construct a list
User John (ctl1)
User Jim (ctl2)

Check which control you were trying to route an event to - that would be
ctl2; execute OnClick event delegate for User Jim. -

- Hope this helps


In summary - if you can avoid doing it do so - I had a catalog that was
working the exact same way; after 3 months of production I changed it to
redirect itself to its own page with Get Parameters instead of events. I did
it because every load of a page resulted in 2X work for the server (previous
state + current state).
it also removed a lot of complexity. I also know of a few that created
every! control on the page they might need and then made them visible or hid
them. Personally I don't like that style.

- Arthur

[quoted text, click to view]

Re: How to add postback function in Custom Server Controls Alessandro Zifiglio
2/2/2004 3:21:28 PM
still not clear to me. Instead of defining a scenario can you simplify it
and explain it in terms of custom control building what is not working for
you. Is your requirement to build a databound custom server control ? Are
you looking into building a custom control that has similar functionality to
the datagrid, datalist, repeater and so on --what have you been able to
implement so far. Which parts are working and where are you stuck.

[quoted text, click to view]
Re: How to add postback function in Custom Server Controls ton
2/2/2004 3:45:12 PM
what i want is that when the event of the button fires I would like to show
a complete other card, not the 10 fields i've showen earlier but because of
the click i would see details depending on the click this could be 5 fields
from another record and another table. So existing added controls away, new
controls inplace

ton
"Alessandro Zifiglio" <alessandrozifiglio@NO-SPAM-hotmail.com> schreef in
bericht news:27tTb.2569$HO2.1990@news.edisontel.com...
[quoted text, click to view]
AddThis Social Bookmark Button