如何继承.ascx文件到另一个 [英] how to inherit .ascx file to another

查看:122
本文介绍了如何继承.ascx文件到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我做了一个BaseUserControl.ascx和ChildUserControl.ascx
文件.两者在文件后面都有自己的代码.
所以我想要的,
我想骚扰ChildUserControl.ascx文件
到BaseUserControl.ascx.所以我BaseUserControl.ascx文件中的任何内容
应该在ChildUserControl.ascx中可见,
意味着如果我在BaseUserControl.ascx中保留了一个TextBox应该是可见的
进入ChildUserControl.ascx设计视图.并且应该可以在文件后面的ChildUserControl.ascx代码中对其进行访问.

1)这可能吗,如果可以,怎么办?

2)如果上面是可能的,那么如何将文件后面的BaseUserControl.ascx代码访问到文件后面的ChildUserControl.ascx代码中?

Hi,
i have made a BaseUserControl.ascx and a ChildUserControl.ascx
file. both have its own code behind file.
So what i want,
i wanted to inharit ChildUserControl.ascx file
to BaseUserControl.ascx. so whatever in my BaseUserControl.ascx file
should be visible into the ChildUserControl.ascx,
means if i have kept a TextBox in BaseUserControl.ascx should be visible
into the ChildUserControl.ascx desing view. and it should be accessible into the ChildUserControl.ascx code behind file.

1) Is this possible, if yes then how?

2) if above is possible then How to access BaseUserControl.ascx code behind file into the ChildUserControl.ascx code behind file?

推荐答案

1.这当然是可能的.

假设以下是BaseUserControl.ascx

1. This is of course possible.

Suppose, following is the BaseUserControl.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="BaseUserControl.ascx.cs" Inherits="BaseUserControl" %>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>



您需要在另一个用户控件(ChildUserControl)中注册该用户控件,并按以下方式使用它:



You need to register this user control in another user control (ChildUserControl) and use that as follows:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ChildUserControl.ascx.cs" Inherits="ChildUserControl" %>
<%@ Register src="BaseUserControl.ascx" tagname="BaseUserControl" tagprefix="uc1" %>
<uc1:BaseUserControl ID="BaseUserControl1" runat="server" /


>

并且,如果您在ASPX页面中注册ChildUserControl.ascx,您将看到BaseUserControl.ascx中的文本框将被显示

2.是的,也许可以通过另一个用户控件来继承用户控件后面的代码,但是建议将基本用户控件代码放在基类(继承System.Web.UI.UserControl)中,然后继承类由用户控件类后面的代码组成.即:


>

And, if you register the ChildUserControl.ascx in an aspx page, you will see the TextBox from the BaseUserControl.ascx will be shown

2. Yes, it might be possible to inherit the code behind of a user control by anothe user control, but, it is suggested to put the base user control codes inside a base class (That inherits the System.Web.UI.UserControl) and then inherit the class by the code behind classes of the user controls. That is:

//Put this inside the App_Code
class UserControlBase : System.Web.UI.UserControl
{
}

class ChildUserControl : UserControlBase
{
}


这篇关于如何继承.ascx文件到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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