【问题标题】:how to show hidden div from codebehind c#如何从代码隐藏c#中显示隐藏的div
【发布时间】:2012-06-25 03:05:00
【问题描述】:

我正在尝试初始化一个用户控件,该控件包含一个在页面首次加载时隐藏的 gridview。当用户单击页面上的“搜索”按钮时,我想显示该用户控件中的网格视图。我尝试了很多不同的方法来显示和隐藏用户控件。我尝试将用户控件放在 div 中,然后使用 style="display: none" 将 div 初始化为隐藏,然后在代码中将样式更改为“块”。我尝试使用 2 个类,其中一个类的显示设置为无,另一个类的显示设置为阻止,然后根据用户单击“搜索”按钮在这两个类之间切换。我尝试使用面板并将其可见性设置为真/假,但是当用户单击“搜索”按钮后我将其可见性设置为真时,面板永远不会显示。我尝试过的所有方法,除了使用面板及其可见性选项外,都给我以下例外:

{InnerText = '((System.Web.UI.HtmlControls.HtmlContainerControl)(divGrid)).InnerText' threw an exception of type 'System.Web.HttpException'}

这是我正在使用的导致我出现此错误的代码:

标记:

<div id="divGrid" runat="server">
    <uc1:ContactsGridUserControl ID="ContactsGridUserControl1" runat="server" UpdateMode="Conditional"/>      
</div> 

<asp:Button ID="btnSearch" runat="server" Text="Search" Font-Size="Small" Height="30px" 
                Width="67px" ClientIDMode="Static" onclick="btnSearch_Click"></asp:Button>

后面的代码:

protected void btnSearch_Click(object sender, EventArgs e)
{
    //string redirect = CommonStrings.editContactPage;
    bool bStatus = false;

    try
    {
        bStatus = validateFields();

        if (bStatus)
        {
            divGrid.Style["display"] = "block";           

        }
        else
        {
            lblError.Text = "Please enter characters for at least one field below";

        }
    }
    catch (Exception ex)
    {
        lblError.Text = "Exception: " + ex.ToString();
    }
}

protected bool validateFields()
{
    try
    {
        if ((txtbxFirstName.Text != null && txtbxFirstName.Text.Length > 0) ||
            (txtbxLastName.Text != null && txtbxLastName.Text.Length > 0) ||
            (txtbxPhoneNum.Text != null && txtbxPhoneNum.Text.Length > 0) ||
            (txtbxCity.Text != null && txtbxCity.Text.Length > 0) ||
            (txtbxZipCode.Text != null && txtbxZipCode.Text.Length > 0) ||
            (txtDate1.Text != null && txtDate1.Text.Length > 0) ||
            (txtDate2.Text != null && txtDate2.Text.Length > 0) ||
            (txtbxCompanyName.Text != null && txtbxCompanyName.Text.Length > 0))
        {
            return true;

        }
        else
        {
            return false;
        }
    }
    catch (Exception ex)
    {
        lblError.Text = "Exception: " + ex.ToString();
        return false;
    }
}

完整堆栈跟踪:(我在 Visual Studio 中右键单击调用堆栈窗口并选择“显示外部代码”。我不确定我是否这样做是为了显示完整堆栈跟踪,而不是显示顶部的内容默认堆栈)

App_Web_gjla55xr.dll!AdvancedSearch.btnSearch_Click(object sender, System.EventArgs e) 第 50 行 C# System.Web.dll!System.Web.UI.WebControls.Button.OnClick(System.EventArgs e) + 0x77 字节
System.Web.dll!System.Web.UI.WebControls.Button.RaisePostBackEvent(string eventArgument) + 0x71 bytes
System.Web.dll!System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(string eventArgument) + 0xb 字节
System.Web.dll!System.Web.UI.Page.RaisePostBackEvent(System.Web.UI.IPostBackEventHandler sourceControl, string eventArgument) + 0xe 字节
System.Web.dll!System.Web.UI.Page.RaisePostBackEvent(System.Collections.Specialized.NameValueCollection postData) + 0x25 字节
System.Web.dll!System.Web.UI.Page.ProcessRequestMain(bool includeStagesBeforeAsyncPoint, bool includeStagesAfterAsyncPoint) + 0x15bc 字节
System.Web.dll!System.Web.UI.Page.ProcessRequest(bool includeStagesBeforeAsyncPoint, bool includeStagesAfterAsyncPoint) + 0x8d 字节
System.Web.dll!System.Web.UI.Page.ProcessRequest() + 0x4f 字节 System.Web.dll!System.Web.UI.Page.ProcessRequestWithNoAssert(System.Web.HttpContext 上下文) + 0x16 字节
System.Web.dll!System.Web.UI.Page.ProcessRequest(System.Web.HttpContext 上下文) + 0x32 字节
App_Web_gjla55xr.dll!ASP.advancedsearch_aspx.ProcessRequest(System.Web.HttpContext context) + 0x33 字节 C# System.Web.dll!System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() + 0x65 字节 System.Web.dll!System.Web.HttpApplication.ExecuteStep(System.Web.HttpApplication.IExecutionStep step, ref bool completedSynchronously) + 0x4c 字节 System.Web.dll!System.Web.HttpApplication.ApplicationStepManager.ResumeSteps(System.Exception 错误) + 0x13e 字节
System.Web.dll!System.Web.HttpApplication.System.Web.IHttpAsyncHandler.BeginProcessRequest(System.Web.HttpContext context, System.AsyncCallback cb, object extraData) + 0xf8 bytes
System.Web.dll!System.Web.HttpRuntime.ProcessRequestInternal(System.Web.HttpWorkerRequest WR) + 0x1a2 字节 System.Web.dll!System.Web.HttpRuntime.ProcessRequestNoDemand(System.Web.HttpWorkerRequest WR) + 0x7d 字节
System.Web.dll!System.Web.HttpRuntime.ProcessRequest(System.Web.HttpWorkerRequest WR) + 0x47 字节
WebDev.WebHost40.dll!Microsoft.VisualStudio.WebHost.Request.Process() + 0x17b 字节 WebDev.WebHost40.dll!Microsoft.VisualStudio.WebHost.Host.ProcessRequest(Microsoft.VisualStudio.WebHost.Connection conn) + 0x6c 字节
[Appdomain 转换]
WebDev.WebHost40.dll!Microsoft.VisualStudio.WebHost.Server.OnSocketAccept(object acceptedSocket) + 0x83 字节
mscorlib.dll!System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(object state) + 0x2d bytes mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback 回调, 对象状态, bool ignoreSyncCtx) + 0xb0 字节
mscorlib.dll!System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() + 0x5a 字节 mscorlib.dll!System.Threading.ThreadPoolWorkQueue.Dispatch() + 0x147 字节
mscorlib.dll!System.Threading._ThreadPoolWaitCallback.PerformWaitCallback() + 0x2d 字节
[本机到托管转换]

编辑:我解决了我的问题的答案。我需要用一个 ajax 更新面板包围用户控件,并将一个 asyncpostbacktrigger 连接到 Search 按钮单击事件。我还用另一个面板围绕用户控件,并从后面的代码中将该面板的可见性设置为真/假。

更新代码:

<asp:UpdatePanel ID="UpdatePanel2" runat="server">
    <ContentTemplate>
        <asp:Panel ID="pnlGrid" Visible="false" runat="server">   
            <uc1:ContactsGridUserControl ID="ContactsGridUserControl1" runat="server" UpdateMode="Conditional"/>
        </asp:Panel>
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="btnSearch" EventName="Click" />
    </Triggers>

【问题讨论】:

  • 你的div 有一个类hidden...也许你应该删除那个类?
  • 任何内部异常?显示堆栈!
  • 嘿,杰夫,我删除了隐藏的类。对这种混乱感到抱歉,那是我在交换 2 个不同的类时尝试的另一种方法留下的代码,一个类的显示设置为无,另一个类的显示设置为阻止。取出 class=hidden 后我仍然得到同样的错误
  • 嘿 Charleh,我如何显示堆栈?我看到一个调用堆栈窗口,但它只显示当前在堆栈顶部的内容。我正在使用 Visual Studio 2010。谢谢!
  • 你能调试到validateFields()吗?我想实际的异常是在那里抛出的。并检查 InnerException,如果有的话。

标签: c# asp.net html


【解决方案1】:

我相信所有 ASP.NET WebForms 控件的基类是 HtmlControl,它具有 Visible 属性。这行得通吗?:

divGrid.Visible = true; 

【讨论】:

  • 我试过了,但由于某种原因,div 没有显示。我也尝试使用面板并尝试将可见设置为 true,但仍然无法正常工作
【解决方案2】:

我找到了问题的答案。我必须用更新面板包围用户控件,并将触发器设置为搜索按钮的单击事件。我还用一个面板包围了用户控件,并简单地将可见设置为真/假。

【讨论】:

    【解决方案3】:

    我实际上遇到了这个问题。我在 Page_Load 事件中使用了divControl.Visible = false;,神秘地解决了代码破坏了我的页面格式的问题。所以最后我在 Page_Load 事件上尝试了divControl.Style.Add("visibility", "hidden"); divControl.Style.Add("visibility", "visible"); 在所需的方法上解决了它并且不会破坏页面格式。

    【讨论】:

      【解决方案4】:

      如果我没记错的话,我不久前也遇到过类似的问题,这是由于如果控件在页面加载时(或启动时的某个时间点)不可见,则处理有时无法正常工作。我现在不太记得细节了。

      解决办法:

      • 使用 jquery 在文档准备好时将其隐藏
      • 在“隐藏”时使元素的位置离开屏幕,并在要显示时将其移回原处

      如果这些都不起作用,请发布完整的堆栈跟踪。

      【讨论】:

      • 感谢您迄今为止的帮助!我尝试了这些建议,不幸的是他们会隐藏 div 但不显示它。我在上面发布了完整的堆栈跟踪。谢谢!
      • 您可能会仔细检查代码隐藏是否未将控件设置为以某种方式不可见。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-08
      • 1970-01-01
      • 2014-05-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多