![](/img/trans.png)
[英]How to set property in master page from class in app_code? with asp.net c#
[英]ASP.Net c# - How do I instantiate an instance of a WebUserControl from within a App_Code class?
阿羅哈,
我有一個自定義控件,我需要在類中實例化並添加到頁面。 我的類有一個函數,我傳遞了Page
,所以我可以訪問頁面上的控件。 我正在使用這種方法添加標准的ASP控件以及它按預期工作的一切。 我的問題是自定義控件的類型是已知定義的?
我在這里閱讀將.cs從控件移動到App_Code文件夾中,但是當我這樣做時它不會編譯,因為它沒有看到ascx中的控件有效。 例如,我得到CS0103: The name 'litTest' does not exist in the current context
。 因為它們是部分類,我在App_Code文件夾中創建了一個新的空部分類,並單獨留下控件的.cs文件。
好吧它以這種方式編譯但是當我將控件添加到Page.controls集合時,我在頁面上看不到它應該是什么。 例如,我將TEST
添加到ascx文件的底部,我在頁面上看不到它。 此外,控件還有我需要設置的變量,在使用空的分部類時我無權訪問。
我知道我想要做的是使用標准控件,為什么我似乎無法使用自定義控件?
我在App_Code文件夾中的部分類:
public partial class CustomControlClass : System.Web.UI.UserControl
{
}
我為用戶控件的部分課程:
public partial class CustomControlClass : System.Web.UI.UserControl
{
private int _myValue= -1;
public int myValue
{
get { return _myValue; }
set { _myValue= value; }
}
protected void Page_Init(object sender, EventArgs e)
{
litTest.Text = "TEST";
}
}
我的用戶控件ascx:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="CustomControlClass.ascx.cs" Inherits="CustomControlClass" %>
<asp:Literal ID="litTest" runat="server"></asp:Literal>
TEST
我的App_Code類:
public class MyClass
{
public static void doWork(Page Ctrl)
{
CustomControlClass c = new CustomControlClass();
//c.myValue = 1; // Wont compile with this line
Ctrl.Controls.Add(c);
Literal l = new Literal();
l.Text = "HELLO";
Ctrl.Controls.Add(l);
}
}
頁面輸出根本沒有單詞TEST。 HELLO這個詞顯示得很好。 我試過從頁面Load
, Init
和PreInit
回調中調用MyClass.doWork()
,這些都導致了dame的事情。
更新:
正如Minh Nguyen建議我可以使用Ctrl.LoadControl("~/Controls/CustomControlClass.ascx");
加載控件但我無法將其轉換為自己的類型我必須將其指定為控件類型:
Control c= Ctrl.LoadControl("~/Controls/CustomControlClass.ascx");
這樣做可以讓我將控件添加到頁面並使其按預期工作。 缺點是我無法訪問任何控件屬性。 為了解決這個問題,我這樣做:
c.GetType().GetProperty("myValue").SetValue(c, 1, null);
這有效,但我不確定它的使用價格有多貴。 我希望我能投出控件,但是當我嘗試時會出錯。
我暫時沒有回答,看看是否還有其他選擇。
使用
CustomControlClass c = (CustomControlClass)Ctrl.LoadControl("~/VirtualPathToASCX/CustomControlClass.ascx");
代替
CustomControlClass c = new CustomControlClass();
更新:修復投射錯誤
//LoadControl
ASP.customcontrolclass_ascx c = (ASP.customcontrolclass_ascx)this.LoadControl("~/Controls/CustomControlClass.ascx");
//set myValue
c.myValue = 3;
您無法在VS自動完成列表中看到ASP.customcontrolclass_ascx類型,但它編譯時沒有錯誤。
不知道你的代碼發生了什么,但主要使用你所展示的內容對我有用:
〜/ controls / testControl.ascx :
<%@ Control Language='C#' AutoEventWireup='false'
Inherits='CustomControlClass'
%>
<asp:Literal ID='litTest' runat='server'></asp:Literal>
.aspx頁面 :
<%@ Page Language='C#' %>
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
<script runat='server'>
protected override void OnInit(EventArgs e) {
base.OnInit(e);
MyClass.doWork(this.Page);
}
</script>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head runat='server'><title></title></head>
<body><form id='form1' runat='server'>
</form></body></html>
〜/ app_code / CustomControlClass.cs :
using System;
using System.Web;
using System.Web.UI.WebControls;
public partial class CustomControlClass : System.Web.UI.UserControl {
private int _myValue= -1;
public int myValue {
get { return _myValue; }
set { _myValue= value; }
}
private Literal _litTest;
public Literal litTest {
get { return _litTest; }
set { _litTest= value; }
}
protected override void OnInit(EventArgs e) {
base.OnInit(e);
litTest.Text = "TEST";
}
}
〜/ app_code / MyClass :
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public class MyClass {
public static void doWork(Page Ctrl) {
CustomControlClass c =
(CustomControlClass) Ctrl.LoadControl("~/controls/testControl.ascx");
c.myValue = 1;
Ctrl.Form.Controls.Add(c);
Literal l = new Literal();
l.Text = string.Format(
"<p>CustomControlClass.myValue: {0} </p>",
c.myValue
)
+ string.Format(
"<p>CustomControlClass.litTest.Text: {0} </p>",
c.litTest.Text
)
;
Ctrl.Form.Controls.Add(l);
}
}
換句話說,當調用LoadControl()時,轉換為CustomControlClass對我有用 。 如果我理解你是正確的問題?
一種不同的LoadControl方式(僅測試.NET 4)
您可以調用的任何頁面中的用法如下
protected void Page_Load(object sender, EventArgs e)
{
Control ctrl = SomeClass.GetNewUserControl( "hello add me");
Panel1.Controls.Add(ctrl);
}
〜/的app_code / BaseMyControls.cs:
using System;
using System.Collections.Generic;
using System.Web;
/// <summary>
/// Summary description for BaseMyControls
/// </summary>
public class BaseMyControls : System.Web.UI.UserControl
{
public string strProperty;
public BaseMyControls()
{
}
}
〜/的app_code / SomeClassInApp_Code.cs:
using System;
using System.Collections.Generic;
using System.Web;
/// <summary>
/// Summary description for SomeClassInApp_Code
/// </summary>
public class SomeClassInApp_Code
{
public static System.Web.UI.Control GetNewUserControl(string someString)
{
BaseMyControls bc = (BaseMyControls)(new System.Web.UI.UserControl()).LoadControl("~/controls/MyUC.ascx");
bc.strProperty = someString;
return bc;
}
}
〜/控制/ MyUC.aspx:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="MyUC.ascx.cs" Inherits="MyUC" %>
<asp:Label runat="server" ID="lbl" /></asp:Label>
〜/控制/ MyUC.aspx.cs:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class MyUC : BaseMyControls
{
protected void Page_Load(object sender, EventArgs e)
{
string a = this.strProperty;
lbl.Text = a;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.