Re: Get DropDownList.SelectedValue from Page_PreInit Event

Tech-Archive recommends: Fix windows errors by optimizing your registry



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" wrote:

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" <redhair@xxxxx> wrote in message
news:uCLL%23BjWIHA.5208@xxxxxxxxxxxxxxxxxxxxxxx
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 [MCAD]" <mily242@xxxxxxxxxxxxxxxxx> wrote in message
news:9C78BD50-AE15-40FF-A379-34E7DA8C43AE@xxxxxxxxxxxxxxxx
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" wrote:

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" <redhair@xxxxx> wrote in message
news:O$C0qOVWIHA.4740@xxxxxxxxxxxxxxxxxxxxxxx
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 [MCAD]" <mily242@xxxxxxxxxxxxxxxxx> wrote in message
news:9CB89864-FA79-441B-A3AF-DD636BB84220@xxxxxxxxxxxxxxxx
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" wrote:

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 [MCAD]" <mily242@xxxxxxxxxxxxxxxxx> wrote in
message
news:BC6247CA-61E5-4908-9C04-C5FF9782BB89@xxxxxxxxxxxxxxxx
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" wrote:

It doesn't work and return error msg "Object reference not set to
an
instance of an object"
"Milosz Skalecki [MCAD]" <mily242@xxxxxxxxxxxxxxxxx> wrote in
message
news:EF293811-3B1C-4454-B199-FFBD99D95D4E@xxxxxxxxxxxxxxxx
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" wrote:

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 [MCAD]" wrote:

Howdy,

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


--
Milosz


"Chad Scharf" wrote:

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


"Redhairs" wrote:

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



















.



Relevant Pages

  • Re: Get DropDownList.SelectedValue from Page_PreInit Event
    ... My goal is to dynamically apply master page in Page_PreInitwith the ... (the drop-down-list control is placed in the Content control), ... way to retrieve the UniqueID and value from Request.Form collection. ... string selectedValue = Request.Form ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Get DropDownList.SelectedValue from Page_PreInit Event
    ... (the drop-down-list control is placed in the Content control), ... selected value of drop-down list in Page_PreInitevent then set the ... way to retrieve the UniqueID and value from Request.Form collection. ... string selectedValue = Request.Form ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Get DropDownList.SelectedValue from Page_PreInit Event
    ... I have created several master pages and this page will assign default master ... (the drop-down-list control is placed in the Content control), ... way to retrieve the UniqueID and value from Request.Form collection. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Get DropDownList.SelectedValue from Page_PreInit Event
    ... The dropdownlist control is in content page not the master page. ... waht's the difference between ClientID and UniqueID? ... "Chad Scharf" wrote: ...
    (microsoft.public.dotnet.framework.aspnet)
  • RE: Reference A var on Master Page from User Control
    ... following to the Page_Load event of the Master Page: ... Public sub OnCurrentUser(sender as Object, e as UserInfoEventArgs) Handles ... The login User control will check what roles they have and create a User ...
    (microsoft.public.dotnet.framework.aspnet)