从代码隐藏中按名称获取 div 控件

发布于 2024-12-29 10:11:55 字数 888 浏览 0 评论 0原文

好的,我想通过 id 访问 div 控件,该 id 是从 C# 代码动态更改的。也许用代码会更清楚:

string divHtml = "";
        for (int j = 1; j < 4; j++)
        {
            string ph = "placeHolder" + j;

            Control phd = (Control)FindControl(ph);
            StringWriter sw = new StringWriter();
            HtmlTextWriter w = new HtmlTextWriter(sw);
            phd.RenderControl(w);
            divHtml = divHtml + sw.GetStringBuilder().ToString();
        }

ascx部分是:

<div runat="server" id="placeHolder1" class="placeholder" >                
</div>

它通过了编译时间,但phd处于空值,而不是控制值。如果我像这样直接访问控制,它就可以正常工作:

StringWriter sw = new StringWriter();
HtmlTextWriter w = new HtmlTextWriter(sw);
placeHolder1.RenderControl(w);
divHtml = divHtml + sw.GetStringBuilder().ToString();

提前谢谢您......

Ok, I want to access div control by id witch is changed dynamically from C# code. Maybe it will be clearer with code:

string divHtml = "";
        for (int j = 1; j < 4; j++)
        {
            string ph = "placeHolder" + j;

            Control phd = (Control)FindControl(ph);
            StringWriter sw = new StringWriter();
            HtmlTextWriter w = new HtmlTextWriter(sw);
            phd.RenderControl(w);
            divHtml = divHtml + sw.GetStringBuilder().ToString();
        }

and ascx part is:

<div runat="server" id="placeHolder1" class="placeholder" >                
</div>

It pass compile time but phd is at null value, not to the value of control. It works fine if i access control directly like this:

StringWriter sw = new StringWriter();
HtmlTextWriter w = new HtmlTextWriter(sw);
placeHolder1.RenderControl(w);
divHtml = divHtml + sw.GetStringBuilder().ToString();

Thank you in advance....

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

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

发布评论

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

评论(2

嗼ふ静 2025-01-05 10:11:55

FindControl 仅查找第一级中的控件,因此如果您的控件位于控件树中的下方,则需要编写自己的嵌套 FindControl 才能使其工作。

看看 ASP.NET 是否有更好的方法来查找其他控件中的控件? 了解更多信息。

或者,如果您有父级(页面),则应该可以直接在该控件上调用 page.FindControl("placeHolder1") 。

FindControl finds only controls in the first level, so if your control is located down in the control tree you need to write your own nested FindControl to make it work.

Have look at ASP.NET Is there a better way to find controls that are within other controls? for more info.

Or if you have the parent (page) it should work to call page.FindControl("placeHolder1") on that control directly.

梦屿孤独相伴 2025-01-05 10:11:55

我认为这是嵌套问题,感谢@bang。但由于我使用母版页来显示我的内容,所以这是最适合我的解决方案:

for (int j = 1; j < 4; j++)
        {
            string ph = "placeHolder" + j;

            ContentPlaceHolder cph = this.Master.FindControl("MainContent") as ContentPlaceHolder;
            Control ctrlPh = cph.FindControl(ph);

            StringWriter sw = new StringWriter();
            HtmlTextWriter w = new HtmlTextWriter(sw);
            ctrlPh.RenderControl(w);
            divHtml = divHtml + sw.GetStringBuilder().ToString();

        }

谢谢@bang,你把我推向了正确的方向。

I figured that it is problem with nesting, thanks to @bang. But since I use master page to display my content, this is solution that is best for me:

for (int j = 1; j < 4; j++)
        {
            string ph = "placeHolder" + j;

            ContentPlaceHolder cph = this.Master.FindControl("MainContent") as ContentPlaceHolder;
            Control ctrlPh = cph.FindControl(ph);

            StringWriter sw = new StringWriter();
            HtmlTextWriter w = new HtmlTextWriter(sw);
            ctrlPh.RenderControl(w);
            divHtml = divHtml + sw.GetStringBuilder().ToString();

        }

Thank you @bang, you pushed me in right direction.

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