这里怎样才能查找到 MyUserControl 中的 MyButton ?
// 这里怎样才能查找到 MyUserControl 中的 MyButton ?
// 我不想要用在MyUserControl公开属性的方法取得,只想用MyButton的ID即"MyButton"取得,行吗?
MyUserControl.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="MyUserControl.ascx.cs" Inherits="MyControl_MyUserControl" %>
<asp:Button ID="MyButton" runat="server" Text="Button" />
--------------------------------------------------------------------------------
TestFindControlInAscx.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestFindControlInAscx.aspx.cs" Inherits="Test_TestFindControlInAscx" %>
<%@ Register Src="../MyControl/MyUserControl.ascx" TagName="MyUserControl" TagPrefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<uc1:MyUserControl ID="MyUserControl1" runat="server" />
</form>
</body>
</html>
--------------------------------------------------------------------------------
TestFindControlInAscx.aspx.cs
public partial class Test_TestFindControlInAscx : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// 这里怎样才能查找到 MyUserControl 中的 MyButton ?
// 我不想要用在MyUserControl公开属性的方法取得,只想用MyButton的ID即"MyButton"取得,行吗?
Button button = (Button)this.FindControl("MyButton");
if (button == null)
{
foreach (Control control in Page.Controls)
{
button = (Button)control.FindControl("MyButton");
if (button != null)
break;
}
}
button.Text = "我的按钮在Page_Load中修改";
}
}
没分可给了,明天再加分。
问题点数:5、回复次数:3Top
1 楼chjlcn(http://www.chenjiliang.com)回复于 2006-03-09 11:47:57 得分 0
我的Page_Load中的代码是不行的,有其他方法吗一?谢谢。Top
2 楼beiouwolf(beiouwolf)回复于 2006-03-09 11:49:48 得分 5
myUserControl.findcontrol()
不是THIS.
Top
3 楼chjlcn(http://www.chenjiliang.com)回复于 2006-03-09 11:53:51 得分 0
好的谢谢。不过暂时没分给啊。不好意思。Top




