Custom Controls

  • Thread starter Thread starter One Handed Man \( OHM - Terry Burns \)
  • Start date Start date
O

One Handed Man \( OHM - Terry Burns \)

When using a custom control. In order to check and see if values have
changed one has to implement the IPostBackDataCollection interface. The
values returned for the control seem to be simply a string with comma
delimited values.

For example, if I were to render two text boxes. One with the Value Terry
and the Other with the Value 'Burns' I would get the following value

Terry,Burns.

I need this to be more like this

Forname,Terry,Surname,Burns. So I get get this into a key/Pair collection.

Is there something Im missing here, as I'm not sure how I can do this. I
tried rendering and ID name to see if this would generate this effect, but
it caused the event not to fire.

Any Ideas would be welcome.
 
I resolved this by using Hidden Input tags, but there must be an easier
slicker way to do this?
 
Only the person who developed the custom Control can answer this question.
We don't have the source code.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
Well that would be me.

My question is one of concept, not regarding the particular control in
question.
 
Well that would be me.
My question is one of concept, not regarding the particular control in
question.

In that case, can you tell us what you mean by "the values returned by the
Controls"? A Control is a class, and doesn't return a value. I'm sure you
mean something by this, but I can't figure out what.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
OK, lets try this one more time.

A custom control allows you to present itself any way you choose rendering
HTML. In the custom control I was writing, I simply wanted a number of text
boxes to return their data and persist in viewstate.

In order to facilitate this your control must implement the
IPostBackDataCollection interface. However, the following function returns
all the postback data from the web forms and allows you to get the data
specifically for your custom control.
Public Function LoadPostData(ByVal postDataKey As String, ByVal
postCollection As System.Collections.Specialized.NameValueCollection) As
Boolean Implements System.Web.UI.IPostBackDataHandler.LoadPostData

BUT. . . If you examine the returned value for your cc, this is simply a
number of string values for the elements of the control .

My question was this. Does a slicker way exist to get these values in a
value/pair collection or do we have to resort to adding hidden html fields
so that we can store the FieldName as well as the values. remember, ASP.NET
does not know about the controls within the CC.

Hope this explains a aliitle better
 
Actually, that function returns a boolean value, either True or False.
Again, I'm having some trouble understanding your question. However, I have
written some articles on developing custom Server Controls, which may help:

http://www.takempis.com/aspnet_fundamentals.asp
http://www.takempis.com/aspnet_controls.asp
http://www.takempis.com/aspnet_anatomy1.asp
http://www.takempis.com/aspnet_anatomy2.asp
http://www.takempis.com/aspnet_anatomy3.asp

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
No no, I'm not refering to the Return value of the function , I'm refering
to the values passed TO the function which contain the data for the control.

postCollection(PostDataKey) is passed, the key is the ID of the control and
the data value in the collection is a string containing the HTML element
values. So if you have two text boxes with values of Me and You, then
returned is "Me,You"

Now do you see what Im driving at ?
 
If you have 2 textboxes coming into the PostDataCollection as a single
comma-delimited item, they must have the same "name" attribute on the
client. The HTML form posts the data in the form as name/value pairs, using
the "name" attribute of the client-side form field to link the data with the
form field on the client.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
kevin,
I do appreciate your help here, but we seem to have some kind of
disconnect. Either I am functionally inoperative or we simply dont
understand each other.

PostDataCollection(PostDataKey) returns the CustomControl data. This is just
a string of concatenated strings.
 
The PostDataCollection is simply a Collection of the form field data posted
by the client form. HTML Form data is posted using name=value pairs. If a
single entry (PostDataCollection(PostDataKey) is a single entry) contains
more than one value, delimited by one or more commas, it contains the values
of more than one form field having the same name attribute on the client.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
Hi Kevin, Thanks for coming back on this one.

Yes, correct !

Now, we have arrived at the understanding, the question is that I was hoping
that this data could be represented as value/key pairs. To better illustrate
what I mean, lets say we take this string and split it and make use of its
elements in our code on the host form.

What if we need to change the layout of our custom control ?, this means we
have to go back and re-jig the code. If however, we could arrange things so
that the ID of the Field allways preceeded the value, we could create a
collection of key/Value pairs in their own right, thus freeing us from the
concerns about layout and modifications.

And thats what I'm after !
 
I'mnot following you there, Terry. However, I CAN tell you that having a
single Control having multiple form fields in it with the same name
attribute is a mistake, as the rules of HTML is not something Microsoft has
any control over.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
No, they are not the same name. My Senario . . .

--- MY Custom Control -- - -

<Input id=txtName . . . . . value="MyFirstName">
<Input id=txtAddress . . . . .value="MyAddress">

--------------------------------


The output from the control ( value ) in the PostBackCollection(MyControlID)
is

"MyFirstName,MyAddress"
 
The id attribute is not the name attribute.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
OK, typo, by you know what I meant dont you ?

Tell you what Kevin, I'll go figure this for myself. Thanks for your help
anyways !
 
Sorry, OHM, I don't know what anyone means except what they say. Too many
years programming, I suppose. Best of luck to you!

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
The name attribute is used by the client, and is posted back to the server
by the client. The id attribute is used on the server to uniquely identify
the Control. Again, it is a mistake to create a Custom Server Control that
always has the same name attribute on the client, for the very reason that
we've been going round and round about. As this is a Custom Server Control,
you, the developer, are fully in charge of how it renders on the client.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

Jeff Evans said:
I've been following this thread and I think I know exactly what you're
talking about. I have the same problem. To outline my understanding of the
issue here:
You have a custom control, called MyControl. You want to implement
IPostBackDataHandler for this control, so that it make use of LoadPostData
and possibly RaisePostDataChangedEvent. The problem is, these events will
only be called if the runtime finds something in the form with the "name"
attribute matching the UniqueID of this instantiation of the control
MyControl. This could be something like (in my present case) "_ct10".
Fine, but what if we want to post *multiple* values with this control, and
have potentially multiple "postDataKey" values for the same control? Is
this even possible? Is there a better way to handle this than splitting the
returned value for postCollection["_ct10"] by a comma (clearly this is a
problem if submitted values HAVE commas themselves)?
Let's go back to the example you mentioned some time ago with:
<Input id=txtName . . . . . value="MyFirstName">
<Input id=txtAddress . . . . .value="MyAddress">

Now for both of these inputs to have their values properly received in the
LoadPostData of the form, on submit, they would have to be assigned the same
"name" attribute (say, "_ct10"), correct? This is the only way the runtime
knows *this* data was intended for *this* control. But this of course poses
a problem, as we've already seen.
what is the proper way of submitting multiple form values for the same
custom control, and having each one properly resolve itself in LoadPostData?
Is it even possible? (Come to think of it, the only examples I've seen of
this in use are somewhat trivial cases with just one input).
Any help is appreciated, and OHM, I'll let you know if I discover
anything. Thanks!
 
BTW, here is an excellent and comprehensive reference on the Microsoft MSDN
Library, which you can study in case you haven't downloaded the free .Net
SDK:

http://msdn.microsoft.com/library/en-us/cpguide/html/cpcondevelopingwebformscontrols.asp?frame=true

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

Jeff Evans said:
I've been following this thread and I think I know exactly what you're
talking about. I have the same problem. To outline my understanding of the
issue here:
You have a custom control, called MyControl. You want to implement
IPostBackDataHandler for this control, so that it make use of LoadPostData
and possibly RaisePostDataChangedEvent. The problem is, these events will
only be called if the runtime finds something in the form with the "name"
attribute matching the UniqueID of this instantiation of the control
MyControl. This could be something like (in my present case) "_ct10".
Fine, but what if we want to post *multiple* values with this control, and
have potentially multiple "postDataKey" values for the same control? Is
this even possible? Is there a better way to handle this than splitting the
returned value for postCollection["_ct10"] by a comma (clearly this is a
problem if submitted values HAVE commas themselves)?
Let's go back to the example you mentioned some time ago with:
<Input id=txtName . . . . . value="MyFirstName">
<Input id=txtAddress . . . . .value="MyAddress">

Now for both of these inputs to have their values properly received in the
LoadPostData of the form, on submit, they would have to be assigned the same
"name" attribute (say, "_ct10"), correct? This is the only way the runtime
knows *this* data was intended for *this* control. But this of course poses
a problem, as we've already seen.
what is the proper way of submitting multiple form values for the same
custom control, and having each one properly resolve itself in LoadPostData?
Is it even possible? (Come to think of it, the only examples I've seen of
this in use are somewhat trivial cases with just one input).
Any help is appreciated, and OHM, I'll let you know if I discover
anything. Thanks!
 
Back
Top