开发者

Problem with setting visibility of div in aspx page

开发者 https://www.devze.com 2023-02-21 15:00 出处:网络
I have a div in checkout.aspx page. The contents of div are as follows: <div id=\"PaymentDetails\" runat=\"server\" style=\"text-align:center\" visible=\"true\">

I have a div in checkout.aspx page. The contents of div are as follows:

<div id="PaymentDetails" runat="server" style="text-align:center" visible="true">
    <asp:Label ID="PaymentDetailsLbl" Text="Payment Details:" runat="server" Font-Size="Large"></asp:Label>
    <br />
    <br />
    <br />
    <asp:Label ID="UNameLbl" Text="User Name:" runat="server"></asp:Label>
    &nbsp;
    <asp:Label ID="UNameTextLabel" runat="server" Width="150px"></asp:Label>
    <br />
    <br />
    <asp:Label ID="AmountLbl" Text="Amount:" runat="server"></asp:Label>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:Label ID="AmountTextLabel" runat="server" Width="50px"></asp:Label>
    <br />
    <br />
    <asp:Label ID="CCNumberLbl" Text="Credit Card No:" runat="server"></asp:Label>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="CCNumberTBox" runat="server"></asp:TextBox>
    <br />
    <br />
    <br />
    <asp:Button ID="SubmitBtn" runat="server" Text="Submit" OnClick="SubmitBtn_Click" />
    <asp:Button ID="ResetBtn" runat="server" OnClick="ResetBtn_Click" Text="Reset" />
    <br />
    <br />
    <asp:Label runat="server" ID="SuccessMessageLabel" ForeColor="Red"></asp:Label>
    <asp:Button ID="SoftwareDownloadsBtn" runat="server" Text="Software Downloads" Visible="false"
        OnClick="SoftwareDownloadsBtn_Click" />
    <br />
    <br />
    <asp:RegularExpressionValidator ID="CCNumberValidator" ErrorMessage="Credit Card Number: Min 10 and max 16 digits, starts with 3 or 4"
        ControlToValidate="CCNumberTBox" runat="server"></asp:RegularExpressionValidator>
</div>

And i try to set the visibility of div as false when the user has not selected any items or the user has deleted all the items from the shopping cart using this code:

 else if ((Session["SelectedRowItems"] == null) || (shoppingCartItems.Count == 0))
        {
            this.Page.FindControl("PaymentDetails").Visible = false;
            GridView1.EmptyDataText = "No Items Checked Out";
            GridView1.EmptyDataRowStyle.CssClass = "EmptyGridViewContent";
        }

But i'm getting the following error:

Object reference not set to an instance of an object.

The deta开发者_Python百科iled screenshot of the error is here

BTW i'm using VS 2008, asp.net/C# and its a web application project

Please help me.

Thanks in anticipation


Using FindControl() to locate PaymentDetails only works if PaymentDetails is part of the root container. That is, it will not recursively search the controls that are children of other controls.

It appears FindControl() is returning null and you get the error when you try and call a method on null.

FindControl() is unnecessary here. Just use PaymentDetails.Visible = false.


If your div is neither is a child page or usercontrol nor master page, then you need not to use FindControl method. Add a runat server tag and access it in the code behind file, using the id.

apsx page:

<div id="myDiv" runt="server" >

// Your html

</div>

aspx.cs:
private void ShowHideDiv(bool status)
{
   myDiv.Visible = status;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消