Professional Web Applications Themes

Passing property value to custom control - ASP.NET Building Controls

I have a custom control which uses an XML file in the CreateChildControls event, the XML file is used to determine how many and what composite form elements to add. I can set the path to a different XML file on Page_Load event of the parent page. When I press a button even if contains no code it triggers a postback (which is normal). Even though I am resetting the location value in Page_load event I get a "The path is not of a legal form" error. It is like on the postback the XML value is not being passed ...

  1. #1

    Default Passing property value to custom control

    I have a custom control which uses an XML file in the CreateChildControls event, the XML file is used to determine how many and what composite form elements to add. I can set the path to a different XML file on Page_Load event of the parent page. When I press a button even if contains no code it triggers a postback (which is normal). Even though I am resetting the location value in Page_load event I get a "The path is not of a legal form" error.

    It is like on the postback the XML value is not being passed to the control again. Ideally I want to point the control at different XML files and set this property from various different buttons. Is there somehthing I don't understand about the state in the control. I thought it would reinstantiated after each postback. And I would just be able to set in the Page_load event. When I hardcode the XML path to the XML file it works fine.

    Chris Kennedy Guest

  2. #2

    Default Re: Passing property value to custom control

    Chris Kennedy wrote:
    > I have a custom control which uses an XML file in the
    > CreateChildControls event, the XML file is used to determine how many
    > and what composite form elements to add. I can set the path to a
    > different XML file on Page_Load event of the parent page. When I press a
    > button even if contains no code it triggers a postback (which is
    > normal). Even though I am resetting the location value in Page_load
    > event I get a "The path is not of a legal form" error.
    > //
    > It is like on the postback the XML value is not being passed to the
    > control again.
    Are you setting the path initially in Page_Load like so:

    If Not Page.IsPostBack then
    myControl.XmlPath = ...
    End If

    ???

    If so, are you sure that you're control is saving this value in tbe view
    state? That is, does the property for XmlPath in your control look like:

    Public Property XmlPath as String
    Get
    Dim o as Object = ViewState("XmlPath")
    If o is Nothing then
    Return String.Empty
    Else
    Return o.ToString()
    End If
    End Get
    Set
    ViewState("XmlPath") = Value
    End Set
    End Property

    ???



    --

    Scott Mitchell
    [email]mitchell4guysfromrolla.com[/email]
    [url]http://www.4GuysFromRolla.com[/url]
    [url]http://www.ASPFAQs.com[/url]
    [url]http://www.ASPMessageboard.com[/url]

    * When you think ASP, think 4GuysFromRolla.com!
    Scott Mitchell [MVP] Guest

  3. #3

    Default Re: Passing property value to custom control

    I am a bit of a newbie and this is a trickier bit of .net. What you suggested worked to a certain extent. You were right I wasn't using the viewstate, I thought the INaming container remembered the properties. What happens is
    If Not Page.IsPostBack Then

    CompControl1.Location = "C:\Inetpub\wwwroot\AVTest\XMLFile2.xml"

    End If

    in the parent page. Then in one of the buttons on the page I

    CompControl1.Location = "C:\Inetpub\wwwroot\AVTest\XMLFile1.xml"

    It doesn't do anything on the first click but changes the XML value on the second click, i.e. I can see the different controls created in the createchildcontrols() event based on values in the XML file. I have another button which sets the valu back which when I click it back twice on the second time creates an invalid cast error, like it is being given a null value instead of a string. I have even tried setting the value of a session or viewstate in the buttons and reading them in the page load event.

    If Not Page.IsPostBack Then

    CompControl1.Location = "C:\Inetpub\wwwroot\AVTest\XMLFile2.xml"

    End If

    If Not IsNothing(Ssesion("XMLPath")) Then

    CompControl1.Location = Session("XMLPath")

    End If

    Any ideas. Regards, Chris


    "Scott Mitchell [MVP]" <mitchell4guysfromrolla.com> wrote in message news:GZaoc.7291$gm5.2802newssvr27.news.prodigy.co m...
    > Chris Kennedy wrote:
    > > I have a custom control which uses an XML file in the
    > > CreateChildControls event, the XML file is used to determine how many
    > > and what composite form elements to add. I can set the path to a
    > > different XML file on Page_Load event of the parent page. When I press a
    > > button even if contains no code it triggers a postback (which is
    > > normal). Even though I am resetting the location value in Page_load
    > > event I get a "The path is not of a legal form" error.
    > > //
    > > It is like on the postback the XML value is not being passed to the
    > > control again.
    >
    > Are you setting the path initially in Page_Load like so:
    >
    > If Not Page.IsPostBack then
    > myControl.XmlPath = ...
    > End If
    >
    > ???
    >
    > If so, are you sure that you're control is saving this value in tbe view
    > state? That is, does the property for XmlPath in your control look like:
    >
    > Public Property XmlPath as String
    > Get
    > Dim o as Object = ViewState("XmlPath")
    > If o is Nothing then
    > Return String.Empty
    > Else
    > Return o.ToString()
    > End If
    > End Get
    > Set
    > ViewState("XmlPath") = Value
    > End Set
    > End Property
    >
    > ???
    >
    >
    >
    > --
    >
    > Scott Mitchell
    > [email]mitchell4guysfromrolla.com[/email]
    > [url]http://www.4GuysFromRolla.com[/url]
    > [url]http://www.ASPFAQs.com[/url]
    > [url]http://www.ASPMessageboard.com[/url]
    >
    > * When you think ASP, think 4GuysFromRolla.com!
    Chris Kennedy Guest

  4. #4

    Default Re: Passing property value to custom control

    Just a different question :
    Is it possible to pass directly the XmlDoent object as a property rather
    than the path ?

    I would like to use sth like :
    <namespace:control runat="server" id="id" doent="myXmlDoentObj" />

    Is it possible to pass an object as a property ?



    "Scott Mitchell [MVP]" <mitchell4guysfromrolla.com> a écrit dans le message
    de news:GZaoc.7291$gm5.2802newssvr27.news.prodigy.co m...
    > Chris Kennedy wrote:
    > > I have a custom control which uses an XML file in the
    > > CreateChildControls event, the XML file is used to determine how many
    > > and what composite form elements to add. I can set the path to a
    > > different XML file on Page_Load event of the parent page. When I press a
    > > button even if contains no code it triggers a postback (which is
    > > normal). Even though I am resetting the location value in Page_load
    > > event I get a "The path is not of a legal form" error.
    > > //
    > > It is like on the postback the XML value is not being passed to the
    > > control again.
    >
    > Are you setting the path initially in Page_Load like so:
    >
    > If Not Page.IsPostBack then
    > myControl.XmlPath = ...
    > End If
    >
    > ???
    >
    > If so, are you sure that you're control is saving this value in tbe view
    > state? That is, does the property for XmlPath in your control look like:
    >
    > Public Property XmlPath as String
    > Get
    > Dim o as Object = ViewState("XmlPath")
    > If o is Nothing then
    > Return String.Empty
    > Else
    > Return o.ToString()
    > End If
    > End Get
    > Set
    > ViewState("XmlPath") = Value
    > End Set
    > End Property
    >
    > ???
    >
    >
    >
    > --
    >
    > Scott Mitchell
    > [email]mitchell4guysfromrolla.com[/email]
    > [url]http://www.4GuysFromRolla.com[/url]
    > [url]http://www.ASPFAQs.com[/url]
    > [url]http://www.ASPMessageboard.com[/url]
    >
    > * When you think ASP, think 4GuysFromRolla.com!
    >


    Henri Guest

  5. #5

    Default Re: Passing property value to custom control

    Henri wrote:
    > Just a different question :
    > Is it possible to pass directly the XmlDoent object as a property rather
    > than the path ?
    >
    > I would like to use sth like :
    > <namespace:control runat="server" id="id" doent="myXmlDoentObj" />
    >
    > Is it possible to pass an object as a property ?
    Yes, it's possible, but not declaratively. That is, you could do:

    <namespace:control runat="server" id="id" />

    and then in the code do:

    id.doent = myXmlDoentObj

    Happy Programming!

    --

    Scott Mitchell
    [email]mitchell4guysfromrolla.com[/email]
    [url]http://www.4GuysFromRolla.com[/url]
    [url]http://www.ASPFAQs.com[/url]
    [url]http://www.ASPMessageboard.com[/url]

    * When you think ASP, think 4GuysFromRolla.com!
    Scott Mitchell [MVP] Guest

Similar Threads

  1. Collection Property in web custom control
    By Class in forum ASP.NET
    Replies: 1
    Last Post: January 17th, 01:27 PM
  2. Custom control with complex property type (System.Web.UI.Control[])
    By Jeremy Chapman in forum ASP.NET Building Controls
    Replies: 0
    Last Post: February 15th, 06:00 PM
  3. Keep custom property-value in custom rendered control
    By Rob Joosen in forum ASP.NET Building Controls
    Replies: 0
    Last Post: May 12th, 11:56 AM
  4. Using controls as property of a custom control
    By Tulga Kalayci in forum ASP.NET Building Controls
    Replies: 1
    Last Post: April 8th, 07:41 PM
  5. custom control bindable property
    By MrB in forum ASP.NET Building Controls
    Replies: 1
    Last Post: December 30th, 11:37 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139