ASP.NET访问Panel内的非服务器控件

发布于 2024-10-17 12:23:49 字数 392 浏览 2 评论 0原文

我有一个继承自 Panel 的自定义控件。在加载时,我想访问此面板内的所有控件(包括非服务器控件)以操作属性。面板的 Controls 属性为我提供了服务器控件,但不是非服务器控件。有什么办法可以访问它们吗?

例如:

<cc:MyPanel runat="server">
    <asp:TextBox id="txt1" runat="server" />
    <input type="text" id="txt2" />
</cc:MyPanel>

Load 事件(或者渲染控件之前的任何事件)期间,我想操作两个文本框。

谢谢

I've got a custom control that inherits from Panel. At Load-time, I want to access all of the controls inside of this panel, including non-server controls, to manipulate the attributes. The Controls property of the panel gives me the server controls, but not the non-server ones. Is there any way to access them?

For example:

<cc:MyPanel runat="server">
    <asp:TextBox id="txt1" runat="server" />
    <input type="text" id="txt2" />
</cc:MyPanel>

During the Load event (or really any event before the control is rendered), I want to manipulate both text boxes.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫七 2024-10-24 12:23:49

您必须在每个标记上添加 runat='server' 或执行一些 JavaScript 来处理此问题。
加载事件正在查看服务器控件。

将您的控件更改为:

<input id="txt2" runat="server" type="text" />

然后您可以执行以下操作:

string s = txt2.Text;

我能想到的唯一其他方法是,如果您想实际使用表单来发布值,然后使用 Request. 访问您所在的每个变量发帖。但我的想法是你更多地寻找前者而不是后者。

最后,如果您选择 JS 路线,这里有一篇来自 MSDN 的好文章,向您展示了如何做到这一点:
http://msdn.microsoft.com/en-us/library/3hc29e2a.aspx

You'd have to either add runat='server' on each tag or do some JavaScript to handle this.
The load event is looking at server controls.

Change your control to:

<input id="txt2" runat="server" type="text" />

Then you can do:

string s = txt2.Text;

The only other way I can think of is if you wanted to actually use a form to post the values and then use Request. to access each variable that you are posting. But my thought is you are looking more for the former rather then the latter.

Finally if you go the JS route here is a nice article from MSDN showing you how to do this:
http://msdn.microsoft.com/en-us/library/3hc29e2a.aspx

ぶ宁プ宁ぶ 2024-10-24 12:23:49

我会推荐 RegisterStartupScript 方法。我做了一个快速测试项目,虽然我不知道您的项目的具体情况,但在我的测试中成功访问和修改了面板内的客户端和服务器端控件的组合。

<asp:Panel ID="customPanel1" runat="server">
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
    <input type="text" id="clientSideInput1" /><br />
    <input type="text" id="clientSideInput2" /><br />
</asp:Panel> 

protected void Page_Load(object sender, EventArgs e)
{
    var controls = customPanel1.Controls;
    foreach(Control c in controls)
    {
        if (c.GetType() == typeof(TextBox))
            ((TextBox)c).Text = "It worked!";
    }
    if (!Page.ClientScript.IsClientScriptBlockRegistered(GetType(), "PageScripts"))
    {
        var jscript = "document.getElementById('clientSideInput1').style.background=\"Red\";";
        jscript += "document.getElementById('clientSideInput2').style.background=\"Yellow\";";
        Page.ClientScript.RegisterStartupScript(GetType(), "PageScripts", jscript, true);
    }
}

为我生成这个...

在此处输入图像描述

希望这能让您继续前进!

I would recommend the RegisterStartupScript method. I did a quick test project and while I don't know the specifics of your project, a combination of client side and server side controls inside of a panel are accessed and modified successfully in my test.

<asp:Panel ID="customPanel1" runat="server">
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
    <input type="text" id="clientSideInput1" /><br />
    <input type="text" id="clientSideInput2" /><br />
</asp:Panel> 

protected void Page_Load(object sender, EventArgs e)
{
    var controls = customPanel1.Controls;
    foreach(Control c in controls)
    {
        if (c.GetType() == typeof(TextBox))
            ((TextBox)c).Text = "It worked!";
    }
    if (!Page.ClientScript.IsClientScriptBlockRegistered(GetType(), "PageScripts"))
    {
        var jscript = "document.getElementById('clientSideInput1').style.background=\"Red\";";
        jscript += "document.getElementById('clientSideInput2').style.background=\"Yellow\";";
        Page.ClientScript.RegisterStartupScript(GetType(), "PageScripts", jscript, true);
    }
}

Produces this for me...

enter image description here

Hope that gets you going!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文