Entry
How can I access an Server Control on the calling page from a custom User Control?
Feb 22nd, 2006 08:58
rob cherny,
Figured out what I was doing wrong, so it wasn't that hard after all:
=======================================================
The calling page:
<%@ Page Language="C#" Debug="True" ClientTarget="uplevel" %>
<%@ Register TagPrefix="test" TagName="Function" Src="function.ascx" %>
<script runat="server">
void Page_Load()
{
testLabel.Text = "<p>This is my inline control</p>";
}
</script>
<html>
<head><title></title></head>
<body id="myBody" runat="server">
<test:Function id="testFunction" Runat="Server" />
<br>
<br>
<div style="border: 1px solid red;">Testing inline: <asp:literal
id="testLabel" runat="server" /></div>
<form runat="server">
<input type="submit" runat="server">
</form>
</body>
</html>
=======================================================
Then, the simple control test case (where I was declaring the variable
wrong, which in the end, was the problem):
=======================================================
<%@ Control Language="C#" Debug="True" %>
<script runat="server">
public void Page_Load()
{
myLit.Text = "This is my control Literal";
HtmlGenericControl myBody =
(HtmlGenericControl)Page.FindControl("myBody");
myBody.Attributes.Add("class", "myClass");
if (Page.IsPostBack) {
Response.Write("<p>This is a postback.</p>");
Literal pageLiteral =
(Literal)Page.FindControl("testLabel");
pageLiteral.Text =
"<p>this is the inline, but controled
from the control!</p>";
}
}
</script>
<asp:Literal id="myLit" runat="server" />