【问题标题】:Get form name inside master page using IEnumerator使用 IEnumerator 在母版页中获取表单名称
【发布时间】:2014-07-11 02:39:31
【问题描述】:

我想从呈现的 HTML 中获取表单名称。我已将 aspx 页面放在母版页中。我有以下代码,我将页面名称作为参数传递。但是我的代码返回 null,因为 ctlEnumerator.Current 显示母版页名称。我应该在我的代码中进行哪些更改以获取表单名称?提前致谢。

private HtmlForm __getHtmlForm(System.Web.UI.Page page)  
{  
IEnumerator ctlEnumerator = page.Controls. GetEnumerator();  
while (ctlEnumerator.MoveNext())  
{  
object curr = ctlEnumerator.Current;  

if (curr is HtmlForm)  
{  
return ((HtmlForm)curr);  
}  
}  
return null;  
}   

【问题讨论】:

  • 你想要什么"name",页面类型的名称,页面的标题还是什么?此外,您在谈论名称、页面和母版页,但您显示的方法返回 HtmlForm。完全不清楚。
  • 我有一个母版页,并在其中放置了一个 aspx 页面。我想从重新渲染的 html 中获取表单名称。

标签: c# asp.net master-pages ienumerator


【解决方案1】:

所以你想得到当前页面的HtmlForm,你可以使用这个甚至不需要参数的静态方法:

public static System.Web.UI.HtmlControls.HtmlForm GetHtmlForm()
{
    var context = HttpContext.Current;
    if (context != null)
    {
        Page currentPage = context.CurrentHandler as Page;
        if (currentPage != null)
            return currentPage.Form;
    }
    return null;
}

现在你可以通过这种方式在 master 中获取它:

string currentFormName = GetHtmlForm().Name;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-03
    • 1970-01-01
    相关资源
    最近更新 更多