如何从代码隐藏中显示隐藏的div C#

2023-12-29

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

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

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

markup:

<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 字节
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 步骤,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上下文,System.AsyncCallback cb,对象extraData)+ 0xf8字节
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 字节
[应用程序域转换]
WebDev.WebHost40.dll!Microsoft.VisualStudio.WebHost.Server.OnSocketAccept(对象acceptedSocket)+ 0x83字节
mscorlib.dll!System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(对象状态) + 0x2d 字节 mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContextexecutionContext,System.Threading.ContextCallback回调,对象状态,boolignoreSyncCtx)+ 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 连接到“搜索”按钮单击事件。我还用另一个面板包围用户控件,并从后面的代码中将该面板的可见性设置为 true/false。

更新的代码:

<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>

我相信所有 ASP.NET WebForms 控件的基类是 HtmlControl,它有一个Visible http://msdn.microsoft.com/en-US/library/system.web.ui.control.visible%28v=vs.80%29财产。这行得通吗?:

divGrid.Visible = true; 
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何从代码隐藏中显示隐藏的div C# 的相关文章

随机推荐