Get DropDownList.SelectedValue from Page_PreInit Event

R

Redhairs

Is it possible to get DropDownList.SelectedValue in Page_PreInit() event
during the postback?
 
C

Chad Scharf

Unfortunately I believe the answer is no. Is there a reason you need this
value before the page is initialized?

You could potentially inject the value into a cookie using JavaScript using
the OnSubmit event of your form, then read that using the
HttpContext.Current.Request.Cookies collection to retrieve your value upon
post-back. I believe the HttpRequest stack has been created by then and upon
calling the OnPreInit method of your page, has built these values from the
request headers.
 
M

Milosz Skalecki [MCAD]

Howdy,

Why using cookie, wouldn't be easier just by using
string selectedValue = Request.Form[yourDropDown.UniqueID]
?
 
C

Chad Scharf

Yes, that would be easier. Sorry, I typically never use Request.Form and
forgot about it.

Is the UniqueID available though in the Page_PreInit event, seeing as the
control tree has not yet been built, it would be difficult for the Page to
calculate the UniqueID, and typically the Control instance would be null as
well no?

--
Chad Scharf
_______________________________
http://www.chadscharf.com


Milosz Skalecki said:
Howdy,

Why using cookie, wouldn't be easier just by using
string selectedValue = Request.Form[yourDropDown.UniqueID]
?


--
Milosz


Chad Scharf said:
Unfortunately I believe the answer is no. Is there a reason you need this
value before the page is initialized?

You could potentially inject the value into a cookie using JavaScript using
the OnSubmit event of your form, then read that using the
HttpContext.Current.Request.Cookies collection to retrieve your value upon
post-back. I believe the HttpRequest stack has been created by then and upon
calling the OnPreInit method of your page, has built these values from the
request headers.
 
M

Milosz Skalecki [MCAD]

Hi Chad,

The control tree is already created in PreInit event, as the aspx page
compiler puts all the declarations into FrameworkInitialize() method which is
called from Page.ProcessRequest (member of the IHttpHandler interface) (have
a look at the content of your website's asp.net folder, especially at
compiled pages). Therefore, it's safe to run the code:
Request.Form[ddl.uniqueID]

Regards
--
Milosz


Chad Scharf said:
Yes, that would be easier. Sorry, I typically never use Request.Form and
forgot about it.

Is the UniqueID available though in the Page_PreInit event, seeing as the
control tree has not yet been built, it would be difficult for the Page to
calculate the UniqueID, and typically the Control instance would be null as
well no?

--
Chad Scharf
_______________________________
http://www.chadscharf.com


Milosz Skalecki said:
Howdy,

Why using cookie, wouldn't be easier just by using
string selectedValue = Request.Form[yourDropDown.UniqueID]
?


--
Milosz


Chad Scharf said:
Unfortunately I believe the answer is no. Is there a reason you need this
value before the page is initialized?

You could potentially inject the value into a cookie using JavaScript using
the OnSubmit event of your form, then read that using the
HttpContext.Current.Request.Cookies collection to retrieve your value upon
post-back. I believe the HttpRequest stack has been created by then and upon
calling the OnPreInit method of your page, has built these values from the
request headers.

--
Chad Scharf
_______________________________
http://www.chadscharf.com


:

Is it possible to get DropDownList.SelectedValue in Page_PreInit() event
during the postback?
 
R

Redhairs

It doesn't work and return error msg "Object reference not set to an
instance of an object"
Milosz Skalecki said:
Hi Chad,

The control tree is already created in PreInit event, as the aspx page
compiler puts all the declarations into FrameworkInitialize() method which
is
called from Page.ProcessRequest (member of the IHttpHandler interface)
(have
a look at the content of your website's asp.net folder, especially at
compiled pages). Therefore, it's safe to run the code:
Request.Form[ddl.uniqueID]

Regards
--
Milosz


Chad Scharf said:
Yes, that would be easier. Sorry, I typically never use Request.Form and
forgot about it.

Is the UniqueID available though in the Page_PreInit event, seeing as the
control tree has not yet been built, it would be difficult for the Page
to
calculate the UniqueID, and typically the Control instance would be null
as
well no?

--
Chad Scharf
_______________________________
http://www.chadscharf.com


Milosz Skalecki said:
Howdy,

Why using cookie, wouldn't be easier just by using
string selectedValue = Request.Form[yourDropDown.UniqueID]
?


--
Milosz


:

Unfortunately I believe the answer is no. Is there a reason you need
this
value before the page is initialized?

You could potentially inject the value into a cookie using JavaScript
using
the OnSubmit event of your form, then read that using the
HttpContext.Current.Request.Cookies collection to retrieve your value
upon
post-back. I believe the HttpRequest stack has been created by then
and upon
calling the OnPreInit method of your page, has built these values
from the
request headers.

--
Chad Scharf
_______________________________
http://www.chadscharf.com


:

Is it possible to get DropDownList.SelectedValue in Page_PreInit()
event
during the postback?
 
M

Milosz Skalecki [MCAD]

Hi Redhairs,

I checked it and it works fine. Are you by any chance create this control
dynamically or drop down list is placed inside a tamplated control? Could you
please pase some code?

Regards
--
Milosz


Redhairs said:
It doesn't work and return error msg "Object reference not set to an
instance of an object"
Milosz Skalecki said:
Hi Chad,

The control tree is already created in PreInit event, as the aspx page
compiler puts all the declarations into FrameworkInitialize() method which
is
called from Page.ProcessRequest (member of the IHttpHandler interface)
(have
a look at the content of your website's asp.net folder, especially at
compiled pages). Therefore, it's safe to run the code:
Request.Form[ddl.uniqueID]

Regards
--
Milosz


Chad Scharf said:
Yes, that would be easier. Sorry, I typically never use Request.Form and
forgot about it.

Is the UniqueID available though in the Page_PreInit event, seeing as the
control tree has not yet been built, it would be difficult for the Page
to
calculate the UniqueID, and typically the Control instance would be null
as
well no?

--
Chad Scharf
_______________________________
http://www.chadscharf.com


:

Howdy,

Why using cookie, wouldn't be easier just by using
string selectedValue = Request.Form[yourDropDown.UniqueID]
?


--
Milosz


:

Unfortunately I believe the answer is no. Is there a reason you need
this
value before the page is initialized?

You could potentially inject the value into a cookie using JavaScript
using
the OnSubmit event of your form, then read that using the
HttpContext.Current.Request.Cookies collection to retrieve your value
upon
post-back. I believe the HttpRequest stack has been created by then
and upon
calling the OnPreInit method of your page, has built these values
from the
request headers.

--
Chad Scharf
_______________________________
http://www.chadscharf.com


:

Is it possible to get DropDownList.SelectedValue in Page_PreInit()
event
during the postback?
 
R

Redhairs

Yes, I use use the master page so the DropDownList was placed in the Content
control,
how to solve this problem?
Btw, waht's the difference between ClientID and UniqueID?


Milosz Skalecki said:
Hi Redhairs,

I checked it and it works fine. Are you by any chance create this control
dynamically or drop down list is placed inside a tamplated control? Could
you
please pase some code?

Regards
--
Milosz


Redhairs said:
It doesn't work and return error msg "Object reference not set to an
instance of an object"
Milosz Skalecki said:
Hi Chad,

The control tree is already created in PreInit event, as the aspx page
compiler puts all the declarations into FrameworkInitialize() method
which
is
called from Page.ProcessRequest (member of the IHttpHandler interface)
(have
a look at the content of your website's asp.net folder, especially at
compiled pages). Therefore, it's safe to run the code:
Request.Form[ddl.uniqueID]

Regards
--
Milosz


:

Yes, that would be easier. Sorry, I typically never use Request.Form
and
forgot about it.

Is the UniqueID available though in the Page_PreInit event, seeing as
the
control tree has not yet been built, it would be difficult for the
Page
to
calculate the UniqueID, and typically the Control instance would be
null
as
well no?

--
Chad Scharf
_______________________________
http://www.chadscharf.com


:

Howdy,

Why using cookie, wouldn't be easier just by using
string selectedValue = Request.Form[yourDropDown.UniqueID]
?


--
Milosz


:

Unfortunately I believe the answer is no. Is there a reason you
need
this
value before the page is initialized?

You could potentially inject the value into a cookie using
JavaScript
using
the OnSubmit event of your form, then read that using the
HttpContext.Current.Request.Cookies collection to retrieve your
value
upon
post-back. I believe the HttpRequest stack has been created by
then
and upon
calling the OnPreInit method of your page, has built these values
from the
request headers.

--
Chad Scharf
_______________________________
http://www.chadscharf.com


:

Is it possible to get DropDownList.SelectedValue in
Page_PreInit()
event
during the postback?
 
M

Milosz Skalecki [MCAD]

Howdy,

In this case (masterpage) you have to manually find the control as it has
not been referenced yet (but it has been instantiated in the contrnt place
holder):

string selectedValue =
Request.Form[Master.FindControl("placeholdername").FindControl("ddlID").UniqueID];

It's quite nasty, so my question is why would you need that? The difference
bewteen UniqueID and ClientID is the first is used as "name" attribute for
input controls and it consists of ID + parents ID (simpifying) separated by a
dollar character, whilst ClientID is used as "id" attribute of all the html
elements that are generated by web controls/html controls with runat=server
separated by underscore char. For more info check this out:
http://www.shanebauer.com/Weblog/PermaLink,guid,488f7bb3-dac8-400b-aacf-e94d4da9b533.aspx
--
Milosz


Redhairs said:
Yes, I use use the master page so the DropDownList was placed in the Content
control,
how to solve this problem?
Btw, waht's the difference between ClientID and UniqueID?


Milosz Skalecki said:
Hi Redhairs,

I checked it and it works fine. Are you by any chance create this control
dynamically or drop down list is placed inside a tamplated control? Could
you
please pase some code?

Regards
--
Milosz


Redhairs said:
It doesn't work and return error msg "Object reference not set to an
instance of an object"
Hi Chad,

The control tree is already created in PreInit event, as the aspx page
compiler puts all the declarations into FrameworkInitialize() method
which
is
called from Page.ProcessRequest (member of the IHttpHandler interface)
(have
a look at the content of your website's asp.net folder, especially at
compiled pages). Therefore, it's safe to run the code:
Request.Form[ddl.uniqueID]

Regards
--
Milosz


:

Yes, that would be easier. Sorry, I typically never use Request.Form
and
forgot about it.

Is the UniqueID available though in the Page_PreInit event, seeing as
the
control tree has not yet been built, it would be difficult for the
Page
to
calculate the UniqueID, and typically the Control instance would be
null
as
well no?

--
Chad Scharf
_______________________________
http://www.chadscharf.com


:

Howdy,

Why using cookie, wouldn't be easier just by using
string selectedValue = Request.Form[yourDropDown.UniqueID]
?


--
Milosz


:

Unfortunately I believe the answer is no. Is there a reason you
need
this
value before the page is initialized?

You could potentially inject the value into a cookie using
JavaScript
using
the OnSubmit event of your form, then read that using the
HttpContext.Current.Request.Cookies collection to retrieve your
value
upon
post-back. I believe the HttpRequest stack has been created by
then
and upon
calling the OnPreInit method of your page, has built these values
from the
request headers.

--
Chad Scharf
_______________________________
http://www.chadscharf.com


:

Is it possible to get DropDownList.SelectedValue in
Page_PreInit()
event
during the postback?
 
R

Redhairs

Thanks for your quick response.

I can find the
Master.FindControl("placeholdername").FindControl("ddlID").UniqueID in
Page_PreInit()
but still get "Object reference not set to an instance of an object" if try
to access
Request.Form[Master.FindControl("placeholdername").FindControl("ddlID").UniqueID];

But the ddlID.UnigueID is one of the keys of Request.Form collection.
Can't figure out why.




Milosz Skalecki said:
Howdy,

In this case (masterpage) you have to manually find the control as it has
not been referenced yet (but it has been instantiated in the contrnt place
holder):

string selectedValue =
Request.Form[Master.FindControl("placeholdername").FindControl("ddlID").UniqueID];

It's quite nasty, so my question is why would you need that? The
difference
bewteen UniqueID and ClientID is the first is used as "name" attribute for
input controls and it consists of ID + parents ID (simpifying) separated
by a
dollar character, whilst ClientID is used as "id" attribute of all the
html
elements that are generated by web controls/html controls with
runat=server
separated by underscore char. For more info check this out:
http://www.shanebauer.com/Weblog/PermaLink,guid,488f7bb3-dac8-400b-aacf-e94d4da9b533.aspx
--
Milosz


Redhairs said:
Yes, I use use the master page so the DropDownList was placed in the
Content
control,
how to solve this problem?
Btw, waht's the difference between ClientID and UniqueID?


Milosz Skalecki said:
Hi Redhairs,

I checked it and it works fine. Are you by any chance create this
control
dynamically or drop down list is placed inside a tamplated control?
Could
you
please pase some code?

Regards
--
Milosz


:

It doesn't work and return error msg "Object reference not set to an
instance of an object"
Hi Chad,

The control tree is already created in PreInit event, as the aspx
page
compiler puts all the declarations into FrameworkInitialize() method
which
is
called from Page.ProcessRequest (member of the IHttpHandler
interface)
(have
a look at the content of your website's asp.net folder, especially
at
compiled pages). Therefore, it's safe to run the code:
Request.Form[ddl.uniqueID]

Regards
--
Milosz


:

Yes, that would be easier. Sorry, I typically never use
Request.Form
and
forgot about it.

Is the UniqueID available though in the Page_PreInit event, seeing
as
the
control tree has not yet been built, it would be difficult for the
Page
to
calculate the UniqueID, and typically the Control instance would be
null
as
well no?

--
Chad Scharf
_______________________________
http://www.chadscharf.com


:

Howdy,

Why using cookie, wouldn't be easier just by using
string selectedValue = Request.Form[yourDropDown.UniqueID]
?


--
Milosz


:

Unfortunately I believe the answer is no. Is there a reason you
need
this
value before the page is initialized?

You could potentially inject the value into a cookie using
JavaScript
using
the OnSubmit event of your form, then read that using the
HttpContext.Current.Request.Cookies collection to retrieve your
value
upon
post-back. I believe the HttpRequest stack has been created by
then
and upon
calling the OnPreInit method of your page, has built these
values
from the
request headers.

--
Chad Scharf
_______________________________
http://www.chadscharf.com


:

Is it possible to get DropDownList.SelectedValue in
Page_PreInit()
event
during the postback?
 
R

Redhairs

The source codes are as below, the page is inherited from one base class

Test.aspx:
<asp:Content ID="Content1" ContentPlaceHolderID="TheHolder" Runat=server>
<asp:DropDownList ID="langSelection" runat="server"
CssClass="selectLang" AutoPostBack=true OnSelectionIndexChanged="......"/>
</Content>


Test.aspx.cs
void Page_PreInit(object sender, EventArg e)
{

DropDownList _theDDL = (DropDownList)
Master.FindControl("TheHolder").FindControl("langSelection");
if (_theDDL != null)
Response.Write(Request.Form[_theDDL.UnigueID]); // null Object
reference err here
}







Milosz Skalecki said:
Howdy,

In this case (masterpage) you have to manually find the control as it has
not been referenced yet (but it has been instantiated in the contrnt place
holder):

string selectedValue =
Request.Form[Master.FindControl("placeholdername").FindControl("ddlID").UniqueID];

It's quite nasty, so my question is why would you need that? The
difference
bewteen UniqueID and ClientID is the first is used as "name" attribute for
input controls and it consists of ID + parents ID (simpifying) separated
by a
dollar character, whilst ClientID is used as "id" attribute of all the
html
elements that are generated by web controls/html controls with
runat=server
separated by underscore char. For more info check this out:
http://www.shanebauer.com/Weblog/PermaLink,guid,488f7bb3-dac8-400b-aacf-e94d4da9b533.aspx
--
Milosz


Redhairs said:
Yes, I use use the master page so the DropDownList was placed in the
Content
control,
how to solve this problem?
Btw, waht's the difference between ClientID and UniqueID?


Milosz Skalecki said:
Hi Redhairs,

I checked it and it works fine. Are you by any chance create this
control
dynamically or drop down list is placed inside a tamplated control?
Could
you
please pase some code?

Regards
--
Milosz


:

It doesn't work and return error msg "Object reference not set to an
instance of an object"
Hi Chad,

The control tree is already created in PreInit event, as the aspx
page
compiler puts all the declarations into FrameworkInitialize() method
which
is
called from Page.ProcessRequest (member of the IHttpHandler
interface)
(have
a look at the content of your website's asp.net folder, especially
at
compiled pages). Therefore, it's safe to run the code:
Request.Form[ddl.uniqueID]

Regards
--
Milosz


:

Yes, that would be easier. Sorry, I typically never use
Request.Form
and
forgot about it.

Is the UniqueID available though in the Page_PreInit event, seeing
as
the
control tree has not yet been built, it would be difficult for the
Page
to
calculate the UniqueID, and typically the Control instance would be
null
as
well no?

--
Chad Scharf
_______________________________
http://www.chadscharf.com


:

Howdy,

Why using cookie, wouldn't be easier just by using
string selectedValue = Request.Form[yourDropDown.UniqueID]
?


--
Milosz


:

Unfortunately I believe the answer is no. Is there a reason you
need
this
value before the page is initialized?

You could potentially inject the value into a cookie using
JavaScript
using
the OnSubmit event of your form, then read that using the
HttpContext.Current.Request.Cookies collection to retrieve your
value
upon
post-back. I believe the HttpRequest stack has been created by
then
and upon
calling the OnPreInit method of your page, has built these
values
from the
request headers.

--
Chad Scharf
_______________________________
http://www.chadscharf.com


:

Is it possible to get DropDownList.SelectedValue in
Page_PreInit()
event
during the postback?
 
S

Scott Roberts

If you're already in the master page code-behind, why would you reference
"Master.FindControl"? Is the master page nested inside another master page
or something?

Seems like you would want:

string selectedValue =
Request.Form[this.FindControl("placeholdername").FindControl("ddlID").UniqueID];

Of course, this code assumes that there is a control named "ddlID" inside
"placeholdername" on every single content page that would ever use this
master page. That seems like a pretty unlikely scenario to me.


Milosz Skalecki said:
Howdy,

In this case (masterpage) you have to manually find the control as it has
not been referenced yet (but it has been instantiated in the contrnt place
holder):

string selectedValue =
Request.Form[Master.FindControl("placeholdername").FindControl("ddlID").UniqueID];

It's quite nasty, so my question is why would you need that? The
difference
bewteen UniqueID and ClientID is the first is used as "name" attribute for
input controls and it consists of ID + parents ID (simpifying) separated
by a
dollar character, whilst ClientID is used as "id" attribute of all the
html
elements that are generated by web controls/html controls with
runat=server
separated by underscore char. For more info check this out:
http://www.shanebauer.com/Weblog/PermaLink,guid,488f7bb3-dac8-400b-aacf-e94d4da9b533.aspx
--
Milosz


Redhairs said:
Yes, I use use the master page so the DropDownList was placed in the
Content
control,
how to solve this problem?
Btw, waht's the difference between ClientID and UniqueID?


Milosz Skalecki said:
Hi Redhairs,

I checked it and it works fine. Are you by any chance create this
control
dynamically or drop down list is placed inside a tamplated control?
Could
you
please pase some code?

Regards
--
Milosz


:

It doesn't work and return error msg "Object reference not set to an
instance of an object"
Hi Chad,

The control tree is already created in PreInit event, as the aspx
page
compiler puts all the declarations into FrameworkInitialize() method
which
is
called from Page.ProcessRequest (member of the IHttpHandler
interface)
(have
a look at the content of your website's asp.net folder, especially
at
compiled pages). Therefore, it's safe to run the code:
Request.Form[ddl.uniqueID]

Regards
--
Milosz


:

Yes, that would be easier. Sorry, I typically never use
Request.Form
and
forgot about it.

Is the UniqueID available though in the Page_PreInit event, seeing
as
the
control tree has not yet been built, it would be difficult for the
Page
to
calculate the UniqueID, and typically the Control instance would be
null
as
well no?

--
Chad Scharf
_______________________________
http://www.chadscharf.com


:

Howdy,

Why using cookie, wouldn't be easier just by using
string selectedValue = Request.Form[yourDropDown.UniqueID]
?


--
Milosz


:

Unfortunately I believe the answer is no. Is there a reason you
need
this
value before the page is initialized?

You could potentially inject the value into a cookie using
JavaScript
using
the OnSubmit event of your form, then read that using the
HttpContext.Current.Request.Cookies collection to retrieve your
value
upon
post-back. I believe the HttpRequest stack has been created by
then
and upon
calling the OnPreInit method of your page, has built these
values
from the
request headers.

--
Chad Scharf
_______________________________
http://www.chadscharf.com


:

Is it possible to get DropDownList.SelectedValue in
Page_PreInit()
event
during the postback?
 
S

Scott Roberts

Did you set a break point and look at each object/method result to see which
one was null? I bet it's "Master".

To reiterate Milosz's question, what the heck are you really trying to do?
It seems pretty unlikely that you would ever want the code-behind of a
master page to access controls on a content page.

Also, as long as you keep posting incomplete code/questions you're likely to
keep getting incomplete answers.


Redhairs said:
Thanks for your quick response.

I can find the
Master.FindControl("placeholdername").FindControl("ddlID").UniqueID in
Page_PreInit()
but still get "Object reference not set to an instance of an object" if
try to access
Request.Form[Master.FindControl("placeholdername").FindControl("ddlID").UniqueID];

But the ddlID.UnigueID is one of the keys of Request.Form collection.
Can't figure out why.




Milosz Skalecki said:
Howdy,

In this case (masterpage) you have to manually find the control as it has
not been referenced yet (but it has been instantiated in the contrnt
place
holder):

string selectedValue =
Request.Form[Master.FindControl("placeholdername").FindControl("ddlID").UniqueID];

It's quite nasty, so my question is why would you need that? The
difference
bewteen UniqueID and ClientID is the first is used as "name" attribute
for
input controls and it consists of ID + parents ID (simpifying) separated
by a
dollar character, whilst ClientID is used as "id" attribute of all the
html
elements that are generated by web controls/html controls with
runat=server
separated by underscore char. For more info check this out:
http://www.shanebauer.com/Weblog/PermaLink,guid,488f7bb3-dac8-400b-aacf-e94d4da9b533.aspx
--
Milosz


Redhairs said:
Yes, I use use the master page so the DropDownList was placed in the
Content
control,
how to solve this problem?
Btw, waht's the difference between ClientID and UniqueID?


Hi Redhairs,

I checked it and it works fine. Are you by any chance create this
control
dynamically or drop down list is placed inside a tamplated control?
Could
you
please pase some code?

Regards
--
Milosz


:

It doesn't work and return error msg "Object reference not set to an
instance of an object"
Hi Chad,

The control tree is already created in PreInit event, as the aspx
page
compiler puts all the declarations into FrameworkInitialize()
method
which
is
called from Page.ProcessRequest (member of the IHttpHandler
interface)
(have
a look at the content of your website's asp.net folder, especially
at
compiled pages). Therefore, it's safe to run the code:
Request.Form[ddl.uniqueID]

Regards
--
Milosz


:

Yes, that would be easier. Sorry, I typically never use
Request.Form
and
forgot about it.

Is the UniqueID available though in the Page_PreInit event, seeing
as
the
control tree has not yet been built, it would be difficult for the
Page
to
calculate the UniqueID, and typically the Control instance would
be
null
as
well no?

--
Chad Scharf
_______________________________
http://www.chadscharf.com


:

Howdy,

Why using cookie, wouldn't be easier just by using
string selectedValue = Request.Form[yourDropDown.UniqueID]
?


--
Milosz


:

Unfortunately I believe the answer is no. Is there a reason
you
need
this
value before the page is initialized?

You could potentially inject the value into a cookie using
JavaScript
using
the OnSubmit event of your form, then read that using the
HttpContext.Current.Request.Cookies collection to retrieve
your
value
upon
post-back. I believe the HttpRequest stack has been created by
then
and upon
calling the OnPreInit method of your page, has built these
values
from the
request headers.

--
Chad Scharf
_______________________________
http://www.chadscharf.com


:

Is it possible to get DropDownList.SelectedValue in
Page_PreInit()
event
during the postback?
 
R

Redhairs

The dropdownlist control is in content page not the master page.
What I want to do is to have a master page drop-down list, and its
OnSelectionIndexChange event
will postback and apply different master page in Page_PreInit() event


Scott Roberts said:
If you're already in the master page code-behind, why would you reference
"Master.FindControl"? Is the master page nested inside another master page
or something?

Seems like you would want:

string selectedValue =
Request.Form[this.FindControl("placeholdername").FindControl("ddlID").UniqueID];

Of course, this code assumes that there is a control named "ddlID" inside
"placeholdername" on every single content page that would ever use this
master page. That seems like a pretty unlikely scenario to me.


Milosz Skalecki said:
Howdy,

In this case (masterpage) you have to manually find the control as it has
not been referenced yet (but it has been instantiated in the contrnt
place
holder):

string selectedValue =
Request.Form[Master.FindControl("placeholdername").FindControl("ddlID").UniqueID];

It's quite nasty, so my question is why would you need that? The
difference
bewteen UniqueID and ClientID is the first is used as "name" attribute
for
input controls and it consists of ID + parents ID (simpifying) separated
by a
dollar character, whilst ClientID is used as "id" attribute of all the
html
elements that are generated by web controls/html controls with
runat=server
separated by underscore char. For more info check this out:
http://www.shanebauer.com/Weblog/PermaLink,guid,488f7bb3-dac8-400b-aacf-e94d4da9b533.aspx
--
Milosz


Redhairs said:
Yes, I use use the master page so the DropDownList was placed in the
Content
control,
how to solve this problem?
Btw, waht's the difference between ClientID and UniqueID?


Hi Redhairs,

I checked it and it works fine. Are you by any chance create this
control
dynamically or drop down list is placed inside a tamplated control?
Could
you
please pase some code?

Regards
--
Milosz


:

It doesn't work and return error msg "Object reference not set to an
instance of an object"
Hi Chad,

The control tree is already created in PreInit event, as the aspx
page
compiler puts all the declarations into FrameworkInitialize()
method
which
is
called from Page.ProcessRequest (member of the IHttpHandler
interface)
(have
a look at the content of your website's asp.net folder, especially
at
compiled pages). Therefore, it's safe to run the code:
Request.Form[ddl.uniqueID]

Regards
--
Milosz


:

Yes, that would be easier. Sorry, I typically never use
Request.Form
and
forgot about it.

Is the UniqueID available though in the Page_PreInit event, seeing
as
the
control tree has not yet been built, it would be difficult for the
Page
to
calculate the UniqueID, and typically the Control instance would
be
null
as
well no?

--
Chad Scharf
_______________________________
http://www.chadscharf.com


:

Howdy,

Why using cookie, wouldn't be easier just by using
string selectedValue = Request.Form[yourDropDown.UniqueID]
?


--
Milosz


:

Unfortunately I believe the answer is no. Is there a reason
you
need
this
value before the page is initialized?

You could potentially inject the value into a cookie using
JavaScript
using
the OnSubmit event of your form, then read that using the
HttpContext.Current.Request.Cookies collection to retrieve
your
value
upon
post-back. I believe the HttpRequest stack has been created by
then
and upon
calling the OnPreInit method of your page, has built these
values
from the
request headers.

--
Chad Scharf
_______________________________
http://www.chadscharf.com


:

Is it possible to get DropDownList.SelectedValue in
Page_PreInit()
event
during the postback?
 
R

Redhairs

I find if I change the drop-down list first time, the page will postback and
get the
Request.Form[_theDDL.UnigueID] correctly in Page_PreInit(). but the
drop-downlist
won't change and onSelectionIndexChanged won't be triggered.

Then I change the drow-down list again, this time CAN NOT find the
Request.Form[_theDDL.UnigueID] correctly in Page_PreInit().
butbut the drop-downlist change and onSelectionIndexChanged is raised.

Do I miss any thing here?



Redhairs said:
The source codes are as below, the page is inherited from one base class

Test.aspx:
<asp:Content ID="Content1" ContentPlaceHolderID="TheHolder" Runat=server>
<asp:DropDownList ID="langSelection" runat="server"
CssClass="selectLang" AutoPostBack=true OnSelectionIndexChanged="......"/>
</Content>


Test.aspx.cs
void Page_PreInit(object sender, EventArg e)
{

DropDownList _theDDL = (DropDownList)
Master.FindControl("TheHolder").FindControl("langSelection");
if (_theDDL != null)
Response.Write(Request.Form[_theDDL.UnigueID]); // null Object
reference err here
}







Milosz Skalecki said:
Howdy,

In this case (masterpage) you have to manually find the control as it has
not been referenced yet (but it has been instantiated in the contrnt
place
holder):

string selectedValue =
Request.Form[Master.FindControl("placeholdername").FindControl("ddlID").UniqueID];

It's quite nasty, so my question is why would you need that? The
difference
bewteen UniqueID and ClientID is the first is used as "name" attribute
for
input controls and it consists of ID + parents ID (simpifying) separated
by a
dollar character, whilst ClientID is used as "id" attribute of all the
html
elements that are generated by web controls/html controls with
runat=server
separated by underscore char. For more info check this out:
http://www.shanebauer.com/Weblog/PermaLink,guid,488f7bb3-dac8-400b-aacf-e94d4da9b533.aspx
--
Milosz


Redhairs said:
Yes, I use use the master page so the DropDownList was placed in the
Content
control,
how to solve this problem?
Btw, waht's the difference between ClientID and UniqueID?


Hi Redhairs,

I checked it and it works fine. Are you by any chance create this
control
dynamically or drop down list is placed inside a tamplated control?
Could
you
please pase some code?

Regards
--
Milosz


:

It doesn't work and return error msg "Object reference not set to an
instance of an object"
Hi Chad,

The control tree is already created in PreInit event, as the aspx
page
compiler puts all the declarations into FrameworkInitialize()
method
which
is
called from Page.ProcessRequest (member of the IHttpHandler
interface)
(have
a look at the content of your website's asp.net folder, especially
at
compiled pages). Therefore, it's safe to run the code:
Request.Form[ddl.uniqueID]

Regards
--
Milosz


:

Yes, that would be easier. Sorry, I typically never use
Request.Form
and
forgot about it.

Is the UniqueID available though in the Page_PreInit event, seeing
as
the
control tree has not yet been built, it would be difficult for the
Page
to
calculate the UniqueID, and typically the Control instance would
be
null
as
well no?

--
Chad Scharf
_______________________________
http://www.chadscharf.com


:

Howdy,

Why using cookie, wouldn't be easier just by using
string selectedValue = Request.Form[yourDropDown.UniqueID]
?


--
Milosz


:

Unfortunately I believe the answer is no. Is there a reason
you
need
this
value before the page is initialized?

You could potentially inject the value into a cookie using
JavaScript
using
the OnSubmit event of your form, then read that using the
HttpContext.Current.Request.Cookies collection to retrieve
your
value
upon
post-back. I believe the HttpRequest stack has been created by
then
and upon
calling the OnPreInit method of your page, has built these
values
from the
request headers.

--
Chad Scharf
_______________________________
http://www.chadscharf.com


:

Is it possible to get DropDownList.SelectedValue in
Page_PreInit()
event
during the postback?
 
M

Milosz Skalecki [MCAD]

Hi Red,

I don't know what is happening in your code but everything is hunky-dory in
mine. Could you please paste entire code from both aspx page and code beside.

Regards
--
Milosz


Redhairs said:
I find if I change the drop-down list first time, the page will postback and
get the
Request.Form[_theDDL.UnigueID] correctly in Page_PreInit(). but the
drop-downlist
won't change and onSelectionIndexChanged won't be triggered.

Then I change the drow-down list again, this time CAN NOT find the
Request.Form[_theDDL.UnigueID] correctly in Page_PreInit().
butbut the drop-downlist change and onSelectionIndexChanged is raised.

Do I miss any thing here?



Redhairs said:
The source codes are as below, the page is inherited from one base class

Test.aspx:
<asp:Content ID="Content1" ContentPlaceHolderID="TheHolder" Runat=server>
<asp:DropDownList ID="langSelection" runat="server"
CssClass="selectLang" AutoPostBack=true OnSelectionIndexChanged="......"/>
</Content>


Test.aspx.cs
void Page_PreInit(object sender, EventArg e)
{

DropDownList _theDDL = (DropDownList)
Master.FindControl("TheHolder").FindControl("langSelection");
if (_theDDL != null)
Response.Write(Request.Form[_theDDL.UnigueID]); // null Object
reference err here
}







Milosz Skalecki said:
Howdy,

In this case (masterpage) you have to manually find the control as it has
not been referenced yet (but it has been instantiated in the contrnt
place
holder):

string selectedValue =
Request.Form[Master.FindControl("placeholdername").FindControl("ddlID").UniqueID];

It's quite nasty, so my question is why would you need that? The
difference
bewteen UniqueID and ClientID is the first is used as "name" attribute
for
input controls and it consists of ID + parents ID (simpifying) separated
by a
dollar character, whilst ClientID is used as "id" attribute of all the
html
elements that are generated by web controls/html controls with
runat=server
separated by underscore char. For more info check this out:
http://www.shanebauer.com/Weblog/PermaLink,guid,488f7bb3-dac8-400b-aacf-e94d4da9b533.aspx
--
Milosz


:

Yes, I use use the master page so the DropDownList was placed in the
Content
control,
how to solve this problem?
Btw, waht's the difference between ClientID and UniqueID?


Hi Redhairs,

I checked it and it works fine. Are you by any chance create this
control
dynamically or drop down list is placed inside a tamplated control?
Could
you
please pase some code?

Regards
--
Milosz


:

It doesn't work and return error msg "Object reference not set to an
instance of an object"
Hi Chad,

The control tree is already created in PreInit event, as the aspx
page
compiler puts all the declarations into FrameworkInitialize()
method
which
is
called from Page.ProcessRequest (member of the IHttpHandler
interface)
(have
a look at the content of your website's asp.net folder, especially
at
compiled pages). Therefore, it's safe to run the code:
Request.Form[ddl.uniqueID]

Regards
--
Milosz


:

Yes, that would be easier. Sorry, I typically never use
Request.Form
and
forgot about it.

Is the UniqueID available though in the Page_PreInit event, seeing
as
the
control tree has not yet been built, it would be difficult for the
Page
to
calculate the UniqueID, and typically the Control instance would
be
null
as
well no?

--
Chad Scharf
_______________________________
http://www.chadscharf.com


:

Howdy,

Why using cookie, wouldn't be easier just by using
string selectedValue = Request.Form[yourDropDown.UniqueID]
?


--
Milosz


:

Unfortunately I believe the answer is no. Is there a reason
you
need
this
value before the page is initialized?

You could potentially inject the value into a cookie using
JavaScript
using
the OnSubmit event of your form, then read that using the
HttpContext.Current.Request.Cookies collection to retrieve
your
value
upon
post-back. I believe the HttpRequest stack has been created by
then
and upon
calling the OnPreInit method of your page, has built these
values
from the
request headers.

--
Chad Scharf
_______________________________
http://www.chadscharf.com


:

Is it possible to get DropDownList.SelectedValue in
Page_PreInit()
event
during the postback?
 
R

Redhairs

I finally found a clue but not sure if it's correct or not.

My goal is to dynamically apply master page in Page_PreInit() with the
selected value of drop-down list.
(the drop-down-list control is placed in the Content control), so during the
postback, I need to read the
selected value of drop-down list in Page_PreInit() event then set the
Page.MasterPageFile property.

Since the dropdownlist is not created yet in Page_PreInit() and I need to
use Master.FindControl......
way to retrieve the UniqueID and value from Request.Form collection. But at
this time I haven't set the
master page, so the Master is a null object and I can't find the
dropdownlist.UniqueID.

Any workaround? I can only append the selected value as the querystring but
no postback any more?




Milosz Skalecki said:
Hi Red,

I don't know what is happening in your code but everything is hunky-dory
in
mine. Could you please paste entire code from both aspx page and code
beside.

Regards
--
Milosz


Redhairs said:
I find if I change the drop-down list first time, the page will postback
and
get the
Request.Form[_theDDL.UnigueID] correctly in Page_PreInit(). but the
drop-downlist
won't change and onSelectionIndexChanged won't be triggered.

Then I change the drow-down list again, this time CAN NOT find the
Request.Form[_theDDL.UnigueID] correctly in Page_PreInit().
butbut the drop-downlist change and onSelectionIndexChanged is raised.

Do I miss any thing here?



Redhairs said:
The source codes are as below, the page is inherited from one base
class

Test.aspx:
<asp:Content ID="Content1" ContentPlaceHolderID="TheHolder"
Runat=server>
<asp:DropDownList ID="langSelection" runat="server"
CssClass="selectLang" AutoPostBack=true
OnSelectionIndexChanged="......"/>
</Content>


Test.aspx.cs
void Page_PreInit(object sender, EventArg e)
{

DropDownList _theDDL = (DropDownList)
Master.FindControl("TheHolder").FindControl("langSelection");
if (_theDDL != null)
Response.Write(Request.Form[_theDDL.UnigueID]); // null Object
reference err here
}







Howdy,

In this case (masterpage) you have to manually find the control as it
has
not been referenced yet (but it has been instantiated in the contrnt
place
holder):

string selectedValue =
Request.Form[Master.FindControl("placeholdername").FindControl("ddlID").UniqueID];

It's quite nasty, so my question is why would you need that? The
difference
bewteen UniqueID and ClientID is the first is used as "name" attribute
for
input controls and it consists of ID + parents ID (simpifying)
separated
by a
dollar character, whilst ClientID is used as "id" attribute of all the
html
elements that are generated by web controls/html controls with
runat=server
separated by underscore char. For more info check this out:
http://www.shanebauer.com/Weblog/PermaLink,guid,488f7bb3-dac8-400b-aacf-e94d4da9b533.aspx
--
Milosz


:

Yes, I use use the master page so the DropDownList was placed in the
Content
control,
how to solve this problem?
Btw, waht's the difference between ClientID and UniqueID?


Hi Redhairs,

I checked it and it works fine. Are you by any chance create this
control
dynamically or drop down list is placed inside a tamplated control?
Could
you
please pase some code?

Regards
--
Milosz


:

It doesn't work and return error msg "Object reference not set to
an
instance of an object"
message
Hi Chad,

The control tree is already created in PreInit event, as the
aspx
page
compiler puts all the declarations into FrameworkInitialize()
method
which
is
called from Page.ProcessRequest (member of the IHttpHandler
interface)
(have
a look at the content of your website's asp.net folder,
especially
at
compiled pages). Therefore, it's safe to run the code:
Request.Form[ddl.uniqueID]

Regards
--
Milosz


:

Yes, that would be easier. Sorry, I typically never use
Request.Form
and
forgot about it.

Is the UniqueID available though in the Page_PreInit event,
seeing
as
the
control tree has not yet been built, it would be difficult for
the
Page
to
calculate the UniqueID, and typically the Control instance
would
be
null
as
well no?

--
Chad Scharf
_______________________________
http://www.chadscharf.com


:

Howdy,

Why using cookie, wouldn't be easier just by using
string selectedValue = Request.Form[yourDropDown.UniqueID]
?


--
Milosz


:

Unfortunately I believe the answer is no. Is there a reason
you
need
this
value before the page is initialized?

You could potentially inject the value into a cookie using
JavaScript
using
the OnSubmit event of your form, then read that using the
HttpContext.Current.Request.Cookies collection to retrieve
your
value
upon
post-back. I believe the HttpRequest stack has been created
by
then
and upon
calling the OnPreInit method of your page, has built these
values
from the
request headers.

--
Chad Scharf
_______________________________
http://www.chadscharf.com


:

Is it possible to get DropDownList.SelectedValue in
Page_PreInit()
event
during the postback?
 
R

Redhairs

Actually, the DDL.UniqueID="Value" do exist in the Request.Form collection
but
can't retrieve it via Request.Form[DDL.UniqueID], is it because server will
validate
the viewstate?



Redhairs said:
I finally found a clue but not sure if it's correct or not.

My goal is to dynamically apply master page in Page_PreInit() with the
selected value of drop-down list.
(the drop-down-list control is placed in the Content control), so during
the postback, I need to read the
selected value of drop-down list in Page_PreInit() event then set the
Page.MasterPageFile property.

Since the dropdownlist is not created yet in Page_PreInit() and I need to
use Master.FindControl......
way to retrieve the UniqueID and value from Request.Form collection. But
at this time I haven't set the
master page, so the Master is a null object and I can't find the
dropdownlist.UniqueID.

Any workaround? I can only append the selected value as the querystring
but no postback any more?




Milosz Skalecki said:
Hi Red,

I don't know what is happening in your code but everything is hunky-dory
in
mine. Could you please paste entire code from both aspx page and code
beside.

Regards
--
Milosz


Redhairs said:
I find if I change the drop-down list first time, the page will postback
and
get the
Request.Form[_theDDL.UnigueID] correctly in Page_PreInit(). but the
drop-downlist
won't change and onSelectionIndexChanged won't be triggered.

Then I change the drow-down list again, this time CAN NOT find the
Request.Form[_theDDL.UnigueID] correctly in Page_PreInit().
butbut the drop-downlist change and onSelectionIndexChanged is raised.

Do I miss any thing here?



The source codes are as below, the page is inherited from one base
class

Test.aspx:
<asp:Content ID="Content1" ContentPlaceHolderID="TheHolder"
Runat=server>
<asp:DropDownList ID="langSelection" runat="server"
CssClass="selectLang" AutoPostBack=true
OnSelectionIndexChanged="......"/>
</Content>


Test.aspx.cs
void Page_PreInit(object sender, EventArg e)
{

DropDownList _theDDL = (DropDownList)
Master.FindControl("TheHolder").FindControl("langSelection");
if (_theDDL != null)
Response.Write(Request.Form[_theDDL.UnigueID]); // null Object
reference err here
}







Howdy,

In this case (masterpage) you have to manually find the control as it
has
not been referenced yet (but it has been instantiated in the contrnt
place
holder):

string selectedValue =
Request.Form[Master.FindControl("placeholdername").FindControl("ddlID").UniqueID];

It's quite nasty, so my question is why would you need that? The
difference
bewteen UniqueID and ClientID is the first is used as "name"
attribute
for
input controls and it consists of ID + parents ID (simpifying)
separated
by a
dollar character, whilst ClientID is used as "id" attribute of all
the
html
elements that are generated by web controls/html controls with
runat=server
separated by underscore char. For more info check this out:
http://www.shanebauer.com/Weblog/PermaLink,guid,488f7bb3-dac8-400b-aacf-e94d4da9b533.aspx
--
Milosz


:

Yes, I use use the master page so the DropDownList was placed in the
Content
control,
how to solve this problem?
Btw, waht's the difference between ClientID and UniqueID?


message
Hi Redhairs,

I checked it and it works fine. Are you by any chance create this
control
dynamically or drop down list is placed inside a tamplated
control?
Could
you
please pase some code?

Regards
--
Milosz


:

It doesn't work and return error msg "Object reference not set to
an
instance of an object"
message
Hi Chad,

The control tree is already created in PreInit event, as the
aspx
page
compiler puts all the declarations into FrameworkInitialize()
method
which
is
called from Page.ProcessRequest (member of the IHttpHandler
interface)
(have
a look at the content of your website's asp.net folder,
especially
at
compiled pages). Therefore, it's safe to run the code:
Request.Form[ddl.uniqueID]

Regards
--
Milosz


:

Yes, that would be easier. Sorry, I typically never use
Request.Form
and
forgot about it.

Is the UniqueID available though in the Page_PreInit event,
seeing
as
the
control tree has not yet been built, it would be difficult for
the
Page
to
calculate the UniqueID, and typically the Control instance
would
be
null
as
well no?

--
Chad Scharf
_______________________________
http://www.chadscharf.com


:

Howdy,

Why using cookie, wouldn't be easier just by using
string selectedValue = Request.Form[yourDropDown.UniqueID]
?


--
Milosz


:

Unfortunately I believe the answer is no. Is there a
reason
you
need
this
value before the page is initialized?

You could potentially inject the value into a cookie using
JavaScript
using
the OnSubmit event of your form, then read that using the
HttpContext.Current.Request.Cookies collection to retrieve
your
value
upon
post-back. I believe the HttpRequest stack has been
created by
then
and upon
calling the OnPreInit method of your page, has built these
values
from the
request headers.

--
Chad Scharf
_______________________________
http://www.chadscharf.com


:

Is it possible to get DropDownList.SelectedValue in
Page_PreInit()
event
during the postback?
 
M

Milosz Skalecki [MCAD]

Red,

Finally, you explained what you've been trying to achive. There are two or
three solutions but i need to know answers on following questions:
- is 'select master page/layout' drop down list put on every single page
- do different master pages from the list have the same placeholders
- could you explain a little bit how 'select layout' functionality should
work.
I'm asking because MasterPageFile is not persited anywhere the default value
is taken from <%@ Page %> MasterPageFile attribute, but you're trying to use
dropdown's selected value to set MasetrPageFile property without knowing
where to look (i.e before the postback non-default master was used).

Waiting for your reply
--
Milosz


Redhairs said:
Actually, the DDL.UniqueID="Value" do exist in the Request.Form collection
but
can't retrieve it via Request.Form[DDL.UniqueID], is it because server will
validate
the viewstate?



Redhairs said:
I finally found a clue but not sure if it's correct or not.

My goal is to dynamically apply master page in Page_PreInit() with the
selected value of drop-down list.
(the drop-down-list control is placed in the Content control), so during
the postback, I need to read the
selected value of drop-down list in Page_PreInit() event then set the
Page.MasterPageFile property.

Since the dropdownlist is not created yet in Page_PreInit() and I need to
use Master.FindControl......
way to retrieve the UniqueID and value from Request.Form collection. But
at this time I haven't set the
master page, so the Master is a null object and I can't find the
dropdownlist.UniqueID.

Any workaround? I can only append the selected value as the querystring
but no postback any more?




Milosz Skalecki said:
Hi Red,

I don't know what is happening in your code but everything is hunky-dory
in
mine. Could you please paste entire code from both aspx page and code
beside.

Regards
--
Milosz


:

I find if I change the drop-down list first time, the page will postback
and
get the
Request.Form[_theDDL.UnigueID] correctly in Page_PreInit(). but the
drop-downlist
won't change and onSelectionIndexChanged won't be triggered.

Then I change the drow-down list again, this time CAN NOT find the
Request.Form[_theDDL.UnigueID] correctly in Page_PreInit().
butbut the drop-downlist change and onSelectionIndexChanged is raised.

Do I miss any thing here?



The source codes are as below, the page is inherited from one base
class

Test.aspx:
<asp:Content ID="Content1" ContentPlaceHolderID="TheHolder"
Runat=server>
<asp:DropDownList ID="langSelection" runat="server"
CssClass="selectLang" AutoPostBack=true
OnSelectionIndexChanged="......"/>
</Content>


Test.aspx.cs
void Page_PreInit(object sender, EventArg e)
{

DropDownList _theDDL = (DropDownList)
Master.FindControl("TheHolder").FindControl("langSelection");
if (_theDDL != null)
Response.Write(Request.Form[_theDDL.UnigueID]); // null Object
reference err here
}







Howdy,

In this case (masterpage) you have to manually find the control as it
has
not been referenced yet (but it has been instantiated in the contrnt
place
holder):

string selectedValue =
Request.Form[Master.FindControl("placeholdername").FindControl("ddlID").UniqueID];

It's quite nasty, so my question is why would you need that? The
difference
bewteen UniqueID and ClientID is the first is used as "name"
attribute
for
input controls and it consists of ID + parents ID (simpifying)
separated
by a
dollar character, whilst ClientID is used as "id" attribute of all
the
html
elements that are generated by web controls/html controls with
runat=server
separated by underscore char. For more info check this out:
http://www.shanebauer.com/Weblog/PermaLink,guid,488f7bb3-dac8-400b-aacf-e94d4da9b533.aspx
--
Milosz


:

Yes, I use use the master page so the DropDownList was placed in the
Content
control,
how to solve this problem?
Btw, waht's the difference between ClientID and UniqueID?


message
Hi Redhairs,

I checked it and it works fine. Are you by any chance create this
control
dynamically or drop down list is placed inside a tamplated
control?
Could
you
please pase some code?

Regards
--
Milosz


:

It doesn't work and return error msg "Object reference not set to
an
instance of an object"
message
Hi Chad,

The control tree is already created in PreInit event, as the
aspx
page
compiler puts all the declarations into FrameworkInitialize()
method
which
is
called from Page.ProcessRequest (member of the IHttpHandler
interface)
(have
a look at the content of your website's asp.net folder,
especially
at
compiled pages). Therefore, it's safe to run the code:
Request.Form[ddl.uniqueID]

Regards
--
Milosz


:

Yes, that would be easier. Sorry, I typically never use
Request.Form
and
forgot about it.

Is the UniqueID available though in the Page_PreInit event,
seeing
as
the
control tree has not yet been built, it would be difficult for
the
Page
to
calculate the UniqueID, and typically the Control instance
would
be
null
as
well no?

--
Chad Scharf
_______________________________
http://www.chadscharf.com


:

Howdy,

Why using cookie, wouldn't be easier just by using
string selectedValue = Request.Form[yourDropDown.UniqueID]
?


--
Milosz


:

Unfortunately I believe the answer is no. Is there a
reason
you
need
this
value before the page is initialized?

You could potentially inject the value into a cookie using
JavaScript
using
the OnSubmit event of your form, then read that using the
HttpContext.Current.Request.Cookies collection to retrieve
your
value
upon
post-back. I believe the HttpRequest stack has been
created by
then
and upon
calling the OnPreInit method of your page, has built these
values
from the
request headers.

--
Chad Scharf
_______________________________
http://www.chadscharf.com


:

Is it possible to get DropDownList.SelectedValue in
Page_PreInit()
event
during the postback?
 
R

Redhairs

I have created several master pages and this page will assign default master
page
in page_PreInit() during the first time access. The dropdownlist will be
initialized
to display the available master page names during the Page_Load().

When the onSelectedIndexChanges event of dropdownlist will autopostback and
assign the master page with selected value in Page_PreInit().

The problem here is not able to read the selected value of dropdownlist in
Page_PreInit()




Milosz Skalecki said:
Red,

Finally, you explained what you've been trying to achive. There are two or
three solutions but i need to know answers on following questions:
- is 'select master page/layout' drop down list put on every single page
- do different master pages from the list have the same placeholders
- could you explain a little bit how 'select layout' functionality should
work.
I'm asking because MasterPageFile is not persited anywhere the default
value
is taken from <%@ Page %> MasterPageFile attribute, but you're trying to
use
dropdown's selected value to set MasetrPageFile property without knowing
where to look (i.e before the postback non-default master was used).

Waiting for your reply
--
Milosz


Redhairs said:
Actually, the DDL.UniqueID="Value" do exist in the Request.Form
collection
but
can't retrieve it via Request.Form[DDL.UniqueID], is it because server
will
validate
the viewstate?



Redhairs said:
I finally found a clue but not sure if it's correct or not.

My goal is to dynamically apply master page in Page_PreInit() with the
selected value of drop-down list.
(the drop-down-list control is placed in the Content control), so
during
the postback, I need to read the
selected value of drop-down list in Page_PreInit() event then set the
Page.MasterPageFile property.

Since the dropdownlist is not created yet in Page_PreInit() and I need
to
use Master.FindControl......
way to retrieve the UniqueID and value from Request.Form collection.
But
at this time I haven't set the
master page, so the Master is a null object and I can't find the
dropdownlist.UniqueID.

Any workaround? I can only append the selected value as the querystring
but no postback any more?




Hi Red,

I don't know what is happening in your code but everything is
hunky-dory
in
mine. Could you please paste entire code from both aspx page and code
beside.

Regards
--
Milosz


:

I find if I change the drop-down list first time, the page will
postback
and
get the
Request.Form[_theDDL.UnigueID] correctly in Page_PreInit(). but the
drop-downlist
won't change and onSelectionIndexChanged won't be triggered.

Then I change the drow-down list again, this time CAN NOT find the
Request.Form[_theDDL.UnigueID] correctly in Page_PreInit().
butbut the drop-downlist change and onSelectionIndexChanged is
raised.

Do I miss any thing here?



The source codes are as below, the page is inherited from one base
class

Test.aspx:
<asp:Content ID="Content1" ContentPlaceHolderID="TheHolder"
Runat=server>
<asp:DropDownList ID="langSelection" runat="server"
CssClass="selectLang" AutoPostBack=true
OnSelectionIndexChanged="......"/>
</Content>


Test.aspx.cs
void Page_PreInit(object sender, EventArg e)
{

DropDownList _theDDL = (DropDownList)
Master.FindControl("TheHolder").FindControl("langSelection");
if (_theDDL != null)
Response.Write(Request.Form[_theDDL.UnigueID]); // null Object
reference err here
}







message
Howdy,

In this case (masterpage) you have to manually find the control as
it
has
not been referenced yet (but it has been instantiated in the
contrnt
place
holder):

string selectedValue =
Request.Form[Master.FindControl("placeholdername").FindControl("ddlID").UniqueID];

It's quite nasty, so my question is why would you need that? The
difference
bewteen UniqueID and ClientID is the first is used as "name"
attribute
for
input controls and it consists of ID + parents ID (simpifying)
separated
by a
dollar character, whilst ClientID is used as "id" attribute of all
the
html
elements that are generated by web controls/html controls with
runat=server
separated by underscore char. For more info check this out:
http://www.shanebauer.com/Weblog/PermaLink,guid,488f7bb3-dac8-400b-aacf-e94d4da9b533.aspx
--
Milosz


:

Yes, I use use the master page so the DropDownList was placed in
the
Content
control,
how to solve this problem?
Btw, waht's the difference between ClientID and UniqueID?


message
Hi Redhairs,

I checked it and it works fine. Are you by any chance create
this
control
dynamically or drop down list is placed inside a tamplated
control?
Could
you
please pase some code?

Regards
--
Milosz


:

It doesn't work and return error msg "Object reference not set
to
an
instance of an object"
message
Hi Chad,

The control tree is already created in PreInit event, as the
aspx
page
compiler puts all the declarations into
FrameworkInitialize()
method
which
is
called from Page.ProcessRequest (member of the IHttpHandler
interface)
(have
a look at the content of your website's asp.net folder,
especially
at
compiled pages). Therefore, it's safe to run the code:
Request.Form[ddl.uniqueID]

Regards
--
Milosz


:

Yes, that would be easier. Sorry, I typically never use
Request.Form
and
forgot about it.

Is the UniqueID available though in the Page_PreInit event,
seeing
as
the
control tree has not yet been built, it would be difficult
for
the
Page
to
calculate the UniqueID, and typically the Control instance
would
be
null
as
well no?

--
Chad Scharf
_______________________________
http://www.chadscharf.com


:

Howdy,

Why using cookie, wouldn't be easier just by using
string selectedValue =
Request.Form[yourDropDown.UniqueID]
?


--
Milosz


:

Unfortunately I believe the answer is no. Is there a
reason
you
need
this
value before the page is initialized?

You could potentially inject the value into a cookie
using
JavaScript
using
the OnSubmit event of your form, then read that using
the
HttpContext.Current.Request.Cookies collection to
retrieve
your
value
upon
post-back. I believe the HttpRequest stack has been
created by
then
and upon
calling the OnPreInit method of your page, has built
these
values
from the
request headers.

--
Chad Scharf
_______________________________
http://www.chadscharf.com


:

Is it possible to get DropDownList.SelectedValue in
Page_PreInit()
event
during the postback?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top