多个Codebehind页面?可能/实用? [英] Multiple Codebehind Pages? Possible/Practical?

查看:94
本文介绍了多个Codebehind页面?可能/实用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是关于如何处理一个引用

多个方法的.aspx页面以及存储这些方法的位置(例如一个代码文件或

多个代码文件)。 br />

前言

========

我有一个简单的类别/产品asp.net/C#网站我正在编写

web dev express并在dreamweaver MX中进行布局。我发现web快速布局

工具难以使用,并且在dwmx中更容易实现复杂的

模板。


开我的网站的主要模板页面,有一个导航侧栏

由两个下拉列表控件组成,位于左边距,

用于网站的每个页面。这是

模板上的锁定区域。我使用代码隐藏文件,因此控件功能(例如,

OnSelectedIndexChanged)在派生的.aspx页面中被引用为

OnSelectedIndexChanged = category,OnSelectedIndexChanged = Product。这些

工作正常。


在default.aspx页面(派生自primary.dwt),有相同的

控件(显然)和代码隐藏文件是default.aspx.cs。

这一切都有效。


问题

============

一旦我执行类别下拉列表(仅使用其中一个

下拉列表作为示例 - 另一个dll是非常相似的),并且

因此执行OnSelectedIndexChanged = category方法(代码

,在default.aspx.cs页面中),category()方法将用户带到新页面category.aspx,其中用户在BODY区域看到一些新的

内容,以及相同的下拉列表所在的位置

左边距(因为category.aspx页面基于

primary.dwt模板)。


执行ddl控件category.aspx页面也工作正常

因为代码为se方法在同一个default.aspx.cs页面,

和codebehind指令是相同的,因为这个页面是基于

的primary.dwt文件(其中设置了codebehind指令)。

很抱歉有很长的序言。

所以这里有一些问题:

======== ==============


1.我需要在

类别的主体中执行其他方法, aspx页面,独立于左手

下拉列表的功能。


如果调用了这个特定于category.aspx页面的新方法,对于

例子,newmethod(),我应该将此代码也放入

default.aspx.cs文件中,还是应该有一个单独的文件,比如说,

category.aspx.cs为了保持newmethod()的代码分开?


2.沿着这些方向,从良好的编码开始。透视,将一个文件中的所有代码隐藏在一个文件中是更好的(例如,那个程序员可以做什么专业的?b $ b)或者以某种方式分解它是否更好(如果那是'

可能)?例如,有TWO Codefile =" somefile.aspx.cs"指令?


3.有没有更好的方法来组织这段代码?似乎即使

我有一个包含所有代码的.dll,然后只是从.DLL引用

class.method,我仍然遇到同样的问题的地方

把所有的代码 - 特别是当一个.aspx页需要参考

多种方法?


提前感谢您的任何见解。

-Rangy

This question is about how to handle an .aspx page that references
multiple methods and where to store these methods (e.g. one codefile or
multiple codefiles).

PREFACE
========
I have a simple category/product asp.net/C# web site I am coding in
web dev express and laying out in dreamweaver MX. I find the layout
tools in web express difficult to use and implementation of complex
templates much easier in dwmx.

On my site''s primary template page, there is a navigation side bar
comprised of two dropdownlst controls that is in the left margin and
used on every page in the site. This is a locked region on the
template. I use code-behind files so the controls functionality, (eg.
OnSelectedIndexChanged) are referenced in the derived .aspx pages as
OnSelectedIndexChanged=category, OnSelectedIndexChanged=Product. These
work fine.

On the default.aspx page (derived from primary.dwt), there are the same
controls (obviously) and the codebehind file is default.aspx.cs.
Everything to this point works.

THE PROBLEM
============
Once I execute the category dropdownlist (using only one of the
dropdownlists as an example -- the other dll is very similar), and
therefore execute the OnSelectedIndexChanged=category method (the code
for which is in the default.aspx.cs page), the category() method takes
the user to a new page, category.aspx, where the user sees some new
content in the BODY region, and where the same dropdownlists are in
the left margin (because the category.aspx page is basesd on the
primary.dwt template).

Executing the ddl controls on the category.aspx page also works fine
because the code for these methods is in the same default.aspx.cs page,
and the codebehind directive is the same because this page is based on
the primary.dwt file (where the codebehind directive is set).
Sorry for the long preface.
So here are the questions:
======================

1. I need to execute other methods in the main body of the
category,aspx page, independent of the funcitonality of the left-hand
drop down lists.

If this new method, specific to the category.aspx page is called, for
example, newmethod(), should I put this code also into the
default.aspx.cs file, or should I have a separate file, say,
category.aspx.cs in order to keep the code for newmethod() separate?

2. Along those lines, from a "good coding" perspective, it is better
to have all the codebehind in one file (e.g. is that what professional
programmers do?) or is it better to break it up somehow (if that''s
possible)? E.g have TWO Codefile="somefile.aspx.cs" directives?

3. Is there a better way to organize this code? It seems that even if
I have a .dll with all the code, and then just reference the
class.method from the .DLL , I still have the same problem of where to
put all the code --- especially when one .aspx page needs to reference
multiple methods?

Thanks in advance for any insight.
-Rangy

推荐答案

MVP,如果我错了,请更正我但是这不可能通过使用

分部课程吗?


Shaun C McDonnell

解决方案架构师
MVPs, correct me if I am wrong but isn''t this possible through the use of
partial classes?

Shaun C McDonnell
Solutions Architect
这个问题是关于如何处理引用多种方法的.aspx页面以及存储这些方法的位置(例如一个代码文件
或多个代码文件)。

前言
========
我有一个简单的类别/产品asp.net/C#网站我正在编写的web dev表达和布局在Dreamweaver MX中。我发现web express中的布局工具难以使用,并且在dwmx中更容易实现复杂的模板。
在我网站的主要模板页面上,有一个导航侧栏
由两个下拉列表控件组成,位于左边距,并在网站的每个页面上使用。这是
模板上的锁定区域。我使用代码隐藏文件,因此控件功能(例如,
OnSelectedIndexChanged)在派生的.aspx页面中引用为OnSelectedIndexChanged = category,OnSelectedIndexChanged = Product。
这些工作正常。

在default.aspx页面(从primary.dwt派生),有相同的控件(显然),代码隐藏文件是default.aspx.cs。
一切都到这一点有效。

问题
============
一旦我执行类别下拉列表(仅使用其中一个
)下拉列表作为示例 - 另一个dll非常相似),因此
执行OnSelectedIndexChanged = category方法(代码
在default.aspx.cs页面中),类别()方法将用户带到一个新的页面category.aspx,其中用户在BODY区域看到一些新的内容,并且相同的下拉列表位于左边距(因为category.aspx页面基于
primary.dwt模板)。
执行category.aspx页面上的ddl控件也可以正常工作,因为这些方法的代码在default.aspx.cs页面中,并且codebehind指令是同样的,因为这个页面基于primary.dwt文件(设置了codebehind指令)。

对于长篇序言感到抱歉。
以下是问题所在:
======================
1.我需要在
类的主体中执行其他方法, aspx页面,独立于左侧下拉列表的功能。

如果调用了这个特定于category.aspx页面的新方法,那么
例如, newmethod(),我应该把这段代码也放到
default.aspx.cs文件中,还是应该有一个单独的文件,比如
category.aspx.cs,以保留newmethod的代码()分开?

2.沿着这些方向,从良好的编码开始。透视,最好将所有代码隐藏在一个文件中(例如专业程序员做什么?)或者以某种方式分解它是否更好
(如果那是'可能)?例如,有TWO Codefile =somefile.aspx.cs
指令吗?

3.有没有更好的方法来组织这段代码?看来即使
如果我有一个包含所有代码的.dll,然后只是从.DLL引用
class.method,我仍然有同样的问题在哪里放置
所有代码---特别是当一个.aspx页面需要引用多种方法时?

提前感谢任何见解。
-Rangy
This question is about how to handle an .aspx page that references
multiple methods and where to store these methods (e.g. one codefile
or multiple codefiles).

PREFACE
========
I have a simple category/product asp.net/C# web site I am coding in
web dev express and laying out in dreamweaver MX. I find the layout
tools in web express difficult to use and implementation of complex
templates much easier in dwmx.
On my site''s primary template page, there is a navigation side bar
comprised of two dropdownlst controls that is in the left margin and
used on every page in the site. This is a locked region on the
template. I use code-behind files so the controls functionality, (eg.
OnSelectedIndexChanged) are referenced in the derived .aspx pages as
OnSelectedIndexChanged=category, OnSelectedIndexChanged=Product.
These work fine.

On the default.aspx page (derived from primary.dwt), there are the
same controls (obviously) and the codebehind file is default.aspx.cs.
Everything to this point works.

THE PROBLEM
============
Once I execute the category dropdownlist (using only one of the
dropdownlists as an example -- the other dll is very similar), and
therefore execute the OnSelectedIndexChanged=category method (the code
for which is in the default.aspx.cs page), the category() method takes
the user to a new page, category.aspx, where the user sees some new
content in the BODY region, and where the same dropdownlists are in
the left margin (because the category.aspx page is basesd on the
primary.dwt template).
Executing the ddl controls on the category.aspx page also works fine
because the code for these methods is in the same default.aspx.cs
page, and the codebehind directive is the same because this page is
based on the primary.dwt file (where the codebehind directive is set).

Sorry for the long preface.
So here are the questions:
======================
1. I need to execute other methods in the main body of the
category,aspx page, independent of the funcitonality of the left-hand
drop down lists.

If this new method, specific to the category.aspx page is called, for
example, newmethod(), should I put this code also into the
default.aspx.cs file, or should I have a separate file, say,
category.aspx.cs in order to keep the code for newmethod() separate?

2. Along those lines, from a "good coding" perspective, it is better
to have all the codebehind in one file (e.g. is that what
professional programmers do?) or is it better to break it up somehow
(if that''s possible)? E.g have TWO Codefile="somefile.aspx.cs"
directives?

3. Is there a better way to organize this code? It seems that even
if I have a .dll with all the code, and then just reference the
class.method from the .DLL , I still have the same problem of where to
put all the code --- especially when one .aspx page needs to reference
multiple methods?

Thanks in advance for any insight.
-Rangy


在我看来,你想要将你的DropDownLists放入一个可以放入你的页面的

独立控件,或放置

将它们处理成基类的代码,所以


//你的基类可以处理你所有页面共有的功能


公共类MyBaseClass:System.Web.UI.Page

{

protected DropDownList selCategory;


protected void CategoryIndexChanged (对象发送者,EventArgs e){}

protected void GoToCategoryPage(int categoryId){}

}

//单个页面来自你的基类

公共类MyPageOne:MyBaseClass

{

private void NewMethod(){}

}

//你可以根据需要覆盖你的基本方法

公共类MyPageTwo:MyBaseClass

{

protected void override GoToCategory (int categoryId)

{

DoSomethingSlightlyDifferent(int categoryId);

}

}

公共类MyPageTwo:MyBaseClass

{

protected void override GoToCategory(int categoryId)

{

DoSomethingBeforeYouLeave();

base.GoToCategory(categoryId)

}

}

It sounds to me like you want either to put your DropDownLists into a
separate control which can be dropped into your pages, or to place the
code for handling them into a base class, so

// your base class can handle functionality common to all your pages

public class MyBaseClass : System.Web.UI.Page
{
protected DropDownList selCategory;

protected void CategoryIndexChanged(object sender, EventArgs e){}
protected void GoToCategoryPage(int categoryId){}
}
// individual pages derive from your base class
public class MyPageOne : MyBaseClass
{
private void NewMethod(){}
}
//you can override your base methods if needed
public class MyPageTwo : MyBaseClass
{
protected void override GoToCategory(int categoryId)
{
DoSomethingSlightlyDifferent(int categoryId);
}
}
public class MyPageTwo : MyBaseClass
{
protected void override GoToCategory(int categoryId)
{
DoSomethingBeforeYouLeave();
base.GoToCategory(categoryId)
}
}


感谢您提供详细的解释和代码。


如果我要将下拉列表创建为单独的控件,那么

的控制权将是什么我用来合并.cs代码和.aspx代码?

(例如服务器控件?)所以我不必重新格式化.aspx页面。


另外,由于我是面向对象的新手程序员,所有这些都是
你上面建议的
类存储了相同的.cs文件?或编译

到一个.dll?


谢谢!

Thank you for the detailed explanation and code.

If I were to create the dropdownlists as separate controls, what kind
of control would I use to incorporate the .cs code and the .aspx code?
(E.g. server control?) so I wouldn''t have to reformat the .aspx pages.

Also, as I am a novice object-oriented programmer, would all of these
classes you suggested above be stored the same .cs file? or compiled
into one .dll?

Thanks!


这篇关于多个Codebehind页面?可能/实用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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