Click here to Skip to main content
15,911,707 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi I've a javascript function that hides or shows a DIV section on my web form, on page load the section is hidden but if the user checks a checkbox the section is displayed. This form has a few address fields that allow the user to search on street, town, post code etc by clicking a button and displaying any options in a datagridview where a record can be selected from to populate the necessary address fields. The thing is when the search button is clicked my <DIV> section is being hidden even though the checkbox is checked. How do I increment my numInt once the button has been clicked?

Here's my javascript function:

JavaScript
// Check for INT value.
        var numInt = 1;
        function chckFrInt() {// Check to see if it's the first time.
            if (numInt == 1) {//  if it IS the first time
                hideDiv()
            }
        }

        function hideDiv() {
            document.getElementById("divVisiting").style.display = "none";
        }
    </script>
</head>
<body onload="javascript:chckFrInt()">
    <form id="frmServiceCallNew" runat="server">

I'm calling the javascript:chckFrInt function in the onload event. And the code behind one of the search buttons is:

ASP.NET
<tr valign="middle">
<td align="right"><asp:Label ID="lblVisitingPostCode" runat="server" CssClass="Label_Small_Bold" Text="Postcode / Town:"></asp:Label></td>
<td align="left"><asp:TextBox id="txtVisitingPostCode" runat="server" CssClass="Input_Text_Long" MaxLength="50"></asp:TextBox>
<asp:TextBox ID="txtVisitingTown" runat="server" CssClass="Input_Text_Long" MaxLength="50"></asp:TextBox></td>
<td align="left"><asp:Button ID="btnSearchVisitingAddress" runat="server" Text="Search Address" CssClass="Button_XSmall" OnClick="btnSearchVisitingAddress_Click" />
<asp:Button ID="btnClearVisitingAddress" runat="server" Text="Clear" CssClass="Button_XSmall" OnClick="btnClearVisitingAddress_Click" />
</td>
</tr>

and the c# code:

C#
protected void btnSearchAddress_Click(object sender, EventArgs e)
        {
            if (ServiceDB.getPostCodes(txtCustomerCountry.Text,
                                    txtCustomerStreet.Text,
                                    txtCustomerTown.Text,
                                    txtCustomerPostcode.Text)
                                    )
            {
                gvPostCodes.Visible = true;
                gvPostCodes.DataSource = ServiceDB.dsResults.Tables["tbl_PostCodes"];
                gvPostCodes.DataBind();
            }
        }

each time the page reloads numInt always = 1 therefore my
remains hidden even though the checkbox is checked.
Posted

Instead of checking a page counter, just check the checkbox directly to see if it is checked. Something like:

JavaScript
function chckFrInt() {
    if (document.getElementById("check1").checked) {
        hideDiv()
    }
}
 
Share this answer
 
The reason why your numInt always equals one is because you are doing a full page postback when you click your button. When you do a full page postback the page is recreated, which means your javascript is being reset.

In your gvPostCodes set the Visible property to False, then in btnSearchAddress_Click set Visible equal to true (like you're doing); however, take out your javascript int the body element.

<body önload="javascript:chckFrInt()"> should just be <body></body>
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900