获取父页面的类名? [英] Getting parent page classname?

查看:65
本文介绍了获取父页面的类名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的结构:


mypage.aspx(class = mypage)

myusercontro.ascx


在mypage.aspx上我可以声明一个变量:

animal =" monkey"


mypage.animal


如果我不知道加载UC的哪个父页面怎么能这样做
$ b $进入(即,它是动态加载的)?我不知道如何找出什么

父页面的类名来自UC。


免责声明:这是我的理解这通常不是首选的两种方法之间传递数据的方法,相反,我会更好。

让父母告诉UC这个变量是什么,而不是依靠UC来从父母那里读取它。我正在热身于这个概念,但是,在这个案例中,这似乎是更多的工作。例如,我有10个父母

页面和10个UC。如果我将变量设置为10个UC,并让每个UC读取它,

我只需要维护/设置10位代码。但是,如果10个父级页面需要告诉每个单独的控件,那么需要创建的100位代码需要创建才能维护。那么,问第二个问题,是我的逻辑错误吗?如果是这样,我的思路有什么缺点(UC从父母那里读取变量?) (John让我想到了这条道路上的
...所以,感谢John!如果有人想在这件事上添加他们的

意见,请做! )


-Darrel

解决方案

" darrel" <无***** @ hotmail.com>在消息中写道

news:u2 ************** @ TK2MSFTNGP10.phx.gbl ...

我有这样的结构:

mypage.aspx(class = mypage)
myusercontro.ascx

在mypage.aspx上我可以声明一个变量:
animal ="猴子

我可以通过简单的方式从UC中读到这个:
mypage.animal


你试过吗?我不相信它会编译,我不知道如果编译它会是什么意思

。哪个mypage实例是

指的是什么?

如果我不知道加载UC的哪个父页面怎么能这样做?
进入(即,它正在被动态加载)?我不知道如何找出
父页面的类名来自UC。

免责声明:据我所知,这不是'通常是在两者之间传递数据的首选方法,相反,我会更好地让父母告诉UC变量是什么,而不是依靠
UC从父母那里读取它。我正在热身于这个概念,但是,在这种情况下,它似乎更多的工作。例如,我有10个父母页面和10个UC。如果我将变量设置为10个UC,并且每个UC都读取它,那么我只需要维护/设置10位代码。但是,如果10页的父页面需要告诉每个单独的控件,那么需要创建的100位代码才能维护。


实际上,没有。如果你有10个UC都引用相同的数据,那么你应该考虑把这段数据拉到一个基类中

所有10个UC派生。只有一个属性。另外,你确实有10个单独的用户控件需要相同的

数据,并且它们是从10个单独的页面使用并动态加载的吗?
另外,如果用户控制所有知道变量的名称,那么页面

不能改变变量的名称,或者,例如把它放在Cache或

Session。

所以,问第二个问题,我的逻辑错了吗?如果是这样,我的思路有什么缺点
(UC从父母那里读取变量)? (约翰让我想到了这条路......所以,感谢约翰!如果有人想对此事添加他们的意见,请做!)



孩子们不应该知道他们父母那么多亲密的细节!


John Saunders


PS你的方式,如果该变量发生了不好的事情(例如,如果

它被设置为Nothing),那么你的罪魁祸首可能是10 UC中的任何一个。我的方式,

只有页面才可能这样做,因为UC无法访问页面变量



>你试过吗?我不相信它会编译,我不知道

什么是

如果它编译的话就意味着它。这是指哪个mypage实例?


嗯......实际上,让我试试吧(它没有给出语法错误,所以我假设它会b / b
它会编译。 ..)


嗯,我刚尝试过,它似乎工作正常。这里是具体的

我所拥有的:


我的ASPX页面:

<%@ Page Language = " VB" AutoEventWireup = QUOT假QUOT; Codebehind =" default.aspx.vb"

Inherits =" districts.default9th"代码隐藏中的%>

...

public shared currentDistrict

Private Sub Page_Load(ByVal sender As System.Object ,ByVal e As

System.EventArgs)处理MyBase.Load

currentDistrict =" 9"

End Sub


我的控制:


Private Sub Page_Load(ByVal sender As System.Object,ByVal e As

System.EventArgs)处理MyBase。加载

dim district as string = default9th.currentDistrict

response.Write(" district:*"& district&" *")

End Sub


这似乎有效。

实际上,没有。如果你有10个UC都引用了相同的数据,那么你应该考虑把这段数据拉到一个基类中,所有10个UC都来自这个基类。只有一个属性。另外,
你真的有10个单独的用户控件都需要相同的数据,它们是从10个单独的页面使用并动态加载的吗?


这是'场景:


我们正在建设一个拥有10个(或更多)''子网站的网站网站''在他们内部。

他们都会分享相同的核心功能,只有差异

是他们所属的地区。根据它,他们会渲染

不同的内容/图形/菜单等。


所以,我看到了3个选项:


1)构建一个页面并通过查询字符串传递''district''信息

2)构建一个页面并通过URL重写处理''district''声明

3)为每个区创建一个父模板''模板''ASPX页面,并在那里设置

唯一信息。


#3的优势在于,如果其中10个区域中的一个希望他们想要的话,我们可以很容易地开发一个全新的模板




用户控件包括网站菜单,面包屑导航,

边栏,内容,目录,反馈表等等。他们都会工作

相同,但需要知道他们目前正在装载哪个网站

进入。


我认为我的主要问题是m has是基础的整个OOP概念

类。

另外,如果用户控制所有知道变量的名称,那么
页面可以不要更改变量的名称,例如,将其放在Cache或
Session中。


嗯,页面不需要......至少不是这个特定的变量。

孩子们不应该知道关于他们的那么多亲密细节父母!


; o)


如果你来自一个自由派家庭怎么办? ; o)

P.S.你的方式,如果该变量发生了不好的事情(例如,如果它被设置为Nothing),那么你的罪魁祸首可能是10 UC中的任何一个。




啊......好吧,事情是这个变量不会改变。 ASPX页面将它设置为
,就是这样。这个概念是这样的:


district1.aspx

- 加载所有的UC,他们需要知道他们目前在

''分区1页''


district1.aspx

- 加载所有的UC,他们需要知道他们目前在

''区2页''


等...


-Darrel


这也有效,但我猜这是一个可怕的黑客:


我创建了一个名为sharedVariables的新类。

然后我可以在任何地方设置变量并从任何地方读取它。

因此,aspx页面可以设置它:


projectclass.sharedVariables.district =" 9"


和任何UC都可以读取它:


district = projectclass.sharedVariables.district


现在,这很简单,而且很有效。但是,我猜这是一个很大的问题,这是我没看到的吗?


-Darrel

I have this structure:

mypage.aspx (class = mypage)
myusercontro.ascx

On the mypage.aspx I can declare a variable:
animal = "monkey"

I can read this from the UC by simply doing this:
mypage.animal

How can I do that if I don''t know which parent page the UC is being loaded
into (ie, it''s being dynamiacally loaded)? I''m not sure how to find out what
the classname of the parent page is from the UC.

DISCLAIMER: It''s my understanding that this isn''t usually the preferred
method of passing data between the two and that, rather, I''d be better off
having the parent tell the UC what the variable is, instead of relying on
the UC to read it from the parent. I''m warming up to that concept, but, in
this case, it seems to be more work tha not. For instance I have 10 parent
pages and 10 UCs. If I set the variable in 10 UCs, and have each UC read it,
I''m only needing to maintain/set 10 bits of code. However, if the 10 parent
pages need to tell each individual control, then that''s 100 bits of code
that need to be created to maintain things. So, to ask a second question, is
my logic wrong? If so, what are the drawbacks to my line of thinking (having
the UC read the variable from the parent)? (John has got me thinking down
this road...so, thanks John! and if anyone else would like to add their
opinion to the matter, please do!)

-Darrel

解决方案

"darrel" <no*****@hotmail.com> wrote in message
news:u2**************@TK2MSFTNGP10.phx.gbl...

I have this structure:

mypage.aspx (class = mypage)
myusercontro.ascx

On the mypage.aspx I can declare a variable:
animal = "monkey"

I can read this from the UC by simply doing this:
mypage.animal
Have you tried that? I don''t believe it will compile, and I don''t know what
it would mean if it did compile. Which instance of mypage would this be
referring to?
How can I do that if I don''t know which parent page the UC is being loaded
into (ie, it''s being dynamiacally loaded)? I''m not sure how to find out
what
the classname of the parent page is from the UC.

DISCLAIMER: It''s my understanding that this isn''t usually the preferred
method of passing data between the two and that, rather, I''d be better off
having the parent tell the UC what the variable is, instead of relying on
the UC to read it from the parent. I''m warming up to that concept, but, in
this case, it seems to be more work tha not. For instance I have 10 parent
pages and 10 UCs. If I set the variable in 10 UCs, and have each UC read
it,
I''m only needing to maintain/set 10 bits of code. However, if the 10
parent
pages need to tell each individual control, then that''s 100 bits of code
that need to be created to maintain things.
Actually, no. If you''ve got 10 UC all referencing the same piece of data,
then you should consider pulling that piece of data out into a base class
from which all 10 UC derive. There would be only a single property. Also, do
you really have 10 separate user controls all requiring the same piece of
data, and they''re used from 10 separate pages and are dynamically loaded?

Also, if the user controls all know the name of the variable, then the page
can''t change the name of the variable or, for instance, put it in Cache or
Session.
So, to ask a second question, is
my logic wrong? If so, what are the drawbacks to my line of thinking
(having
the UC read the variable from the parent)? (John has got me thinking down
this road...so, thanks John! and if anyone else would like to add their
opinion to the matter, please do!)



Children should not know that much intimate detail about their parents!

John Saunders

P.S. Your way, if something bad happens to that variable (for instance, if
it got set to Nothing), then your culprit could be any one of 10 UC. My way,
only the page can possibly have done that because the UC have no access to
the variables of the page.


> Have you tried that? I don''t believe it will compile, and I don''t know
what

it would mean if it did compile. Which instance of mypage would this be
referring to?
Hmm...actually, let me try it (it didn''t give a syntax error, so I assumed
it''d compile...)

Well, I just tried it, and it seems to work fine. Here''s the specifics of
what I have:

My ASPX page:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="default.aspx.vb"
Inherits="districts.default9th" %>
in the codebehind...

public shared currentDistrict
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
currentDistrict = "9"
End Sub

My Control:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
dim district as string = default9th.currentDistrict
response.Write ("district: *" & district & "*")
End Sub

That seems to work.
Actually, no. If you''ve got 10 UC all referencing the same piece of data,
then you should consider pulling that piece of data out into a base class
from which all 10 UC derive. There would be only a single property. Also, do you really have 10 separate user controls all requiring the same piece of
data, and they''re used from 10 separate pages and are dynamically loaded?
Here''s the scenario:

We''re building a site that will have 10 (or more) ''sub-sites'' within them.
They''ll all share the same core set of features with the only differences
being which ''district'' they belong to. Depending on that, they''d render
different content/graphics/menus, etc.

So, I saw 3 options:

1) build one page and pass the ''district'' info in via a querystring
2) build one page and handle the ''district'' declarations via URL rewriting
3) create one parent ''template'' ASPX page for each district, and set the
unique info there.

The advantage of #3 was that it will be fairly easy for us to develop a
completely new template if one of the 10 districts desides they want
something radically different.

The usercontrols include things like the site menu, breadcrumb navigation,
side bars, content, directories, feedback forms, etc. They''ll all work the
same, but will need to know which ''site'' they''re presently being loaded
into.

I think the main problem I am having is the whole OOP concept of the base
classes.
Also, if the user controls all know the name of the variable, then the page can''t change the name of the variable or, for instance, put it in Cache or
Session.
Well, the page wouldn''t need to...at least not this specific variable.
Children should not know that much intimate detail about their parents!
;o)

What if you come from a liberal family? ;o)
P.S. Your way, if something bad happens to that variable (for instance, if
it got set to Nothing), then your culprit could be any one of 10 UC.



Ah...well, the thing is this variable won''t change. The ASPX page sets it
and that''s it. The concept is like this:

district1.aspx
- loads all the UCs and they need to know they are currently in the
''district 1 page''

district1.aspx
- loads all the UCs and they need to know they are currently in the
''district 2 page''

etc...

-Darrel


This also works, but I''m guessing this is a horrendous hack:

I made a new class called sharedVariables.

I can then set a variable in this from anywhere and read it from anywhere.
So, the aspx page can set it:

projectclass.sharedVariables.district = "9"

and any UC can read it:

district = projectclass.sharedVariables.district

Now, this is simple, and it works. But I''m guessing there''s a big catch to
this that I''m not seeing?

-Darrel


这篇关于获取父页面的类名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆