简体   繁体   English

如何从我的aspx页面访问.ascx控件

[英]How to access .ascx control from my aspx page

I have following code in my ascx page 我的ascx页面中有以下代码

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl1.ascx.cs"       Inherits="WebUserControl" %>


<li id="firstry" runat="server"> first </li>

And aspx page contains : 和aspx页面包含:

<uc:Spinner id="Spinner" 
    runat="server" 
    MinValue="1" 
    MaxValue="10" />

This simply prints my li into my aspx page.. but I want to access the the control in ascx so that i can apply inline or a css class into that control ..Can any one guide me ? 这只是将我的li打印到我的aspx页面中。.但是我想访问ascx中的控件,以便可以将内联或CSS类应用到该控件中。任何人都可以指导我吗?

In the Control's code aside add: 在控件的代码旁边添加:

public Control FirstTryControl
{
   get { return firsttry; }
}

Then you can access it normally from the page... 然后,您可以从页面正常访问它...

Spinner.FirstTryControl.Styles.Add(...)

That is a brute force approach, you might want to consider adding properties specific to what you need instead. 那是一种蛮力方法,您可能要考虑添加特定于您需要的属性。 In the code aside add something like: 在代码旁边,添加如下内容:

private _spinnerClass = string.empty;
public string SpinnerClass 
{
   get { return _spinnerClass; }
   set { _spinnerClass = value; }
}

protected void Page_Render(o,e)
{
   Spinner.Attributes.Add('class', _spinnerClass);
}

Then in the page you can define these properties right from the markup: 然后,您可以在页面中直接从标记定义以下属性:

<uc:Spinner id="Spinner" 
    runat="server" 
    MinValue="1" 
    MaxValue="10"
    SpinnerClass="green" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM