Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting visibility of Web Form components without 'AutoPostBack' 2

Status
Not open for further replies.

StevenK

Programmer
Jan 5, 2001
1,294
GB
I'm new to the workings of Web Forms.
I've placed a number of controls on a web form and am trying to set the 'Visible' property of one of the panels dependent on the 'Checked' status of a CheckBox.
I'm using the 'CheckedChanged' event code and setting 'AutoPostBack' = True such that the panel shows / hides on reponse of the user changing the state of the checkbox.
However, this poses something of a problem in itself.
As the form is 'auto posting back' when the user hits the 'Back' button on the browser - the page reloads to it's state prior to the checkbox being checked / unchecked.
I'd favour it going back to the calling form - rather than just undoing the 'posted' event from the form.
Any clues with this at all ?

Any help would be greatly received.
Thanks in advance
Steve
 
I don't think it is possible - someone else might have a better answer on the AutoPostBack - not PostBack but what I did to solve this problem was to add the following to the header of the HTML (found it in this forum somewhere)

<HTML>
<HEAD>
<title>EmployeeExpenseReport</title>
<script language="javascript">history.go(1);</script>

This resets the history so the back button doesn't go anywhere.

Hope this helps.

Hope everyone is having a great day!

Thanks - Jennifer
 
It's sure doable by attaching javascript to your checkbox:
Code:
page html:
<HEAD>
 <script language=javascript>
		function showHidePanel(objCheck, panelId){
			if(objCheck.checked){
				document.getElementById(panelId).style.display = "none";
			}
			else{
				document.getElementById(panelId).style.display = "block";
			}		
		}
    </script>
</HEAD>
 <body>
	
    <form id="Form1" method="post" runat="server">
		<table height="100%" width="100%">
			<tr>						
				<td valign=top>			
					<asp:Panel ID="myPanel" Runat=server>Hi there</asp:Panel>
					<br>
					<asp:CheckBox ID="chk" Runat=server AutoPostBack=False Text="Show/Hide panel" />
				</td>	
			</tr>
		</table>
     </form>
	
 </body>
</HTML>


code behind:
override protected void OnInit(EventArgs e)
{
  //
  // CODEGEN: This call is required by the ASP.NET Web Form Designer.
  //
  InitializeComponent();
  base.OnInit(e);

  if(chk.Attributes["onclick"] == null)
  {
    chk.Attributes.Add("onclick", "javascript:showHidePanel(this, '" + myPanel.ClientID + "')");
  }
}
Notice AutoPostBack=False in the checkbox.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top