简体   繁体   English

如果在页面加载期间将容器div设置为不可见,则不会显示asp.net GridView表

[英]asp.net GridView table won't display if container div is set invisible during page load

I have two GridViews in an ASP WebForm project, one for display and one for edit. 我在ASP WebForm项目中有两个GridView,一个用于显示,一个用于编辑。 I have them each in a separate Div. 我将它们分别放在一个单独的Div中。 I start with the Edit div invisible, and have a button in the display div that makes the display div invisible and the edit div visible. 我从不显示Edit div开始,在display div中有一个按钮,该按钮使display div不可见,而edit div可见。 The basic code looks like this: 基本代码如下所示:

    <div id="DisplayDiv">
        <asp:GridView ID="CertList" runat="server" AutoGenerateColumns="False" DataSourceID="GetMyData">
            <Columns>
                <asp:BoundField DataField="a few datafields go here  />
            </Columns>
        </asp:GridView>
         <asp:Button ID="btnEdit" runat="server" OnClick="btnEdit_Click" Text="Edit" />
    </div>

    <div id="EditDiv" visible="false">
        <asp:GridView ID="CertList" runat="server" AutoGenerateColumns="False" DataSourceID="GetMyData" ">
            <Columns>
                <asp:BoundField DataField="a few datafields go here  />
            </Columns>
        </asp:GridView>
    </div>

The click event for the button looks like this: 该按钮的click事件如下所示:

    protected void btnEdit_Click(object sender, EventArgs e)
    {
        EditDiv.Visible = true;
        DisplayDiv.Visible = false;
    }

Everything works fine with the plumbing if I don't set the EditDiv's visible attribute to false in the markup. 如果我没有在标记中将EditDiv的visible属性设置为false,则一切正常。 If I do set it to false, the table doesn't show when I set it back to true programmatically in the button click event. 如果确实将其设置为false,则在按钮单击事件中以编程方式将其设置为true时,该表不会显示。 It seems that the DataView's rendering capability is tied to the ability to access the markup. 似乎DataView的呈现功能与访问标记的能力有关。 So, based on that theory, I tried setting the position to absolute and the left to -10000 and I got the same result. 因此,基于该理论,我尝试将位置设置为绝对位置,将位置设置为-10000,结果相同。

Is this just something I can't do, or am I missing something? 这是我不能做的事情,还是我错过了什么?

Edit: I put this test together at home and it works fine: 编辑:我把这个测试放在家里,它工作正常:

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">

<div id="Div1" runat="server">
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataKeyNames="CustomerID" DataSourceID="SqlDataSource1">
        <Columns>
            <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True" 
                SortExpression="CustomerID" />
            <asp:BoundField DataField="CompanyName" HeaderText="CompanyName" 
                SortExpression="CompanyName" />
            <asp:BoundField DataField="ContactName" HeaderText="ContactName" 
                SortExpression="ContactName" />
            <asp:BoundField DataField="ContactTitle" HeaderText="ContactTitle" 
                SortExpression="ContactTitle" />
        </Columns>
    </asp:GridView>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" 
        Text="Button1" />
</div>

<div id="Div2" runat="server" visible="false">
    <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" 
        DataKeyNames="ProductID" DataSourceID="SqlDataSource2">
        <Columns>
            <asp:BoundField DataField="ProductID" HeaderText="ProductID" 
                InsertVisible="False" ReadOnly="True" SortExpression="ProductID" />
            <asp:BoundField DataField="ProductName" HeaderText="ProductName" 
                SortExpression="ProductName" />
            <asp:BoundField DataField="UnitsInStock" HeaderText="UnitsInStock" 
                SortExpression="UnitsInStock" />
        </Columns>
    </asp:GridView>
    <asp:Button ID="Button2" runat="server" onclick="Button2_Click" 
        Text="Button2" />
</div>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" 
    SelectCommand="SELECT TOP (5) CustomerID, CompanyName, ContactName, ContactTitle FROM Customers">
</asp:SqlDataSource>

<asp:SqlDataSource ID="SqlDataSource2" runat="server" 
    ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" 
    SelectCommand="SELECT TOP (5) * FROM Products">
</asp:SqlDataSource>

</asp:Content>

And the code behind: 以及后面的代码:

    protected void Button1_Click(object sender, EventArgs e)
    {
        Div1.Visible = false;
        Div2.Visible = true;
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        Div2.Visible = false;
        Div1.Visible = true;
    }

So there's a proof of concept. 因此,有一个概念证明。 It's probably a silly mistake, but I'll post it once I find out what the story is. 这可能是一个愚蠢的错误,但是一旦发现故事是什么,我将其发布。

Set runat="server" for both the divs then try to access from the code behind. 为两个div都设置runat="server" ,然后尝试从后面的代码访问。

<div id="DisplayDiv" runat="server">
   ....
</div>

<div id="EditDiv" runat="server" visible="false">
   ....
</div>

Or you can use a asp:Panel instead of a div then you can access the Visible property from code behind. 或者您可以使用asp:Panel而不是div然后可以从后面的代码访问Visible属性。

<asp:Panel Id ="PnlDisplay" runat="server">
     ....
</asp:Panel>

<asp:Panel Id ="PnlEdit" runat="server" Visible="false">
     ....
</asp:Panel>

And you code behind 然后你在后面编码

protected void btnEdit_Click(object sender, EventArgs e)
{
     PnlDisplay.Visible = false;
     PnlEdit.Visible = true;
}

The problem turned out to be due to a strange idea I had in the SQLDataSource. 原来,这个问题是由于我在SQLDataSource中有一个奇怪的主意。 The markup looks like this (Note: there is no ID in the table with a value of 0, that's a dummy value): 标记如下所示(注意:表中没有ID,其值为0,这是一个虚拟值):

<asp:SqlDataSource ID="foo" runat="server" 
    ConnectionString="<%$ ConnectionStrings:BlahBlah %>" 
    SelectCommand="SELECT blahblahblabbertyblah from blah
                    WHERE ID = '0'>
</asp:SqlDataSource>

And then my code behind has this little masterpiece: 然后我的代码背后有这个小杰作:

    protected void btnEdit_Click(object sender, EventArgs e)
    {
        DisplayDiv.Visible = false;
        EditDiv.Visible = true; 
        string SqlSelect =
            "SELECT c.CourseCode AS [Course Code], c.Description, ce.CertDate AS [Date Certified], ce.Recert, " +
            "DATEADD(m, c.Period, ce.CertDate) AS [Expiration Date], e.DocLink, e.CertifiedTrainer, e.GroupLeader " +
            "FROM Certifications c " +
            "INNER JOIN CertificationEmployees ce ON c.ID = ce.CertID " +
            "RIGHT JOIN Employees e ON ce.EmpID = e.ID " +
            "WHERE e.ID = '" + EmployeeList.SelectedValue + "' " +
            "ORDER BY [Course Code]";
        EditCertifications.SelectCommand = 
            "SELECT blahblahblabbertyblah from blah " +
            "WHERE e.ID = '" + aListImUsing.SelectedValue + "' " +
        DataView eView = (DataView)EditCertifications.Select(DataSourceSelectArguments.Empty);
        eTable = eView.ToTable();
        //use the eView table to set various onscreen values
    }

As it turned out, every time I did anything that required a screen change, the GridView mysteriously disappeared. 事实证明,每次我做任何需要更换屏幕的事情时,GridView都会神秘地消失。 I added a CommandField column to the GridView, and when I clicked on the Edit button, I'd lose the GridView. 我在GridView中添加了CommandField列,然后单击“编辑”按钮时,我将失去GridView。 Turned out that the GridView was null. 原来GridView为null。 I was thinking that if I respecified the SelectCommand programmatically, it would persist. 我在想,如果我以编程方式重新指定SelectCommand,它将继续存在。 It doesn't; 不会的 it reverts back to the one specified in the markup as soon as wherever you've specified it in code goes out of scope. 只要您在代码中指定的位置超出范围,它就会恢复为标记中指定的位置。

The fix was to change the dummy value in the WHERE clause to a parameter as I'm supposed to do, and tie the parameter to the current value in the dropdown list that I'm using. 解决方法是按照我的预期将WHERE子句中的虚拟值更改为参数,并将该参数与我正在使用的下拉列表中的当前值绑定。 I changed the WHERE clause to this: 我将WHERE子句更改为:

    WHERE e.ID = @EmpID

and added this: 并添加以下内容:

    <SelectParameters>
        <asp:ControlParameter ControlID="EmployeeList" Name="EmpID" PropertyName="SelectedValue" />
    </SelectParameters>

And my GridViews magically appeared. 我的GridViews神奇地出现了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM