Adding 'onClientClick' javascript to an ASP.NET Login control - asp.net

I've got to put a login page from a 3rd party website in an iframe on another website that I'm developing. I need to add some JavaScript to break out of the iframe when the user logs in, but I can't make the login button to execute the JavaScript and do the postback for the login - just one or the other.
Here's the code from the iframe'd login page that I'm triying to adapt:
<asp:LoginView ID="LoginView1" runat="server">
<AnonymousTemplate>
<asp:Login ID="Login1" runat="server" OnAuthenticate="Login1_Authenticate">
<LayoutTemplate>
<asp:Label ID="lblUsername" runat="server" AssociatedControlID="Username" Text="Email" />
<asp:TextBox ID="Username" runat="server" Text="myName" />
<asp:Label ID="lblPassword" runat="server" AssociatedControlID="Password" Text="Password" />
<asp:TextBox ID="Password" runat="server" Text="myPassword" />
<asp:ImageButton ID="btnLogin" runat="server" CommandName="Login" ImageUrl="~/Images/login-submit.gif" AlternateText="Login" OnClientClick="top.location.href = document.location.href; return true;" />
</LayoutTemplate>
</asp:Login>
</AnonymousTemplate>
<LoggedInTemplate>
You are currently logged in blurb..
</LoggedInTemplate>
Currently when the login button is clicked, the login page breaks out of the iframe, but then I have to click the button again to log the user in. Can anyone see what I'm doing wrong? Thanks.

top.location.href is setting the url of the browser to be a new url so you are never completing the action of the click.
What you could do is set the target of the form to be "_top"
something like
OnClientClick="document.getElementById('MYFORM_CLIENTID').target='_top';return true;"

Looking at your code I think that on the first click you are not logging in. You are just redirecting the parent window to your login page (the one which is displayed in the IFrame at first). That is why you need to click that button twice to log in - on the second time there is no redirect, as the parent window's URL does not change, so basically on the second time the top.location.href = document.location.href part does not yield any results and the login proceeds.
I think that the right course of action would be to authenticate first, then redirect, the opposite of how it looks currently. You can add a script on postback on that page, so that it checks if it's running in a frame and if so, it redirects the parent window to another page. Of course if you can modify the code... With the amount of detail that was provided I'm only able to tell what may be wrong and suggest a fix, but can't give a solution.

Related

How to restrict the user to select the tab in ajax tab container?

I have used ajax tab container in my application In that container 4 tabs available.
I need to restrict the users to select the tabs. How can i perform this operation in asp.net application.
I have tried enable=false then user unable to enter the text in the tab body.
can any one help me.
Html:
<ajax:TabContainer ID="tabcontainer" CssClass="Tab" runat="server" ActiveTabIndex="0" EnablePartialRendering="true">
<ajax:TabPanel runat="server" HeaderText="Basic Details" ID="tabpanelBasicDetails" >
<HeaderTemplate>
<asp:Image ID="imgBasicDetailsBlack" runat="server" ImageUrl="~/Images/basicdetails-b.PNG" />
</HeaderTemplate>
<ContentTemplate>
<asp:Button Id="btn1" runat="server"></asp:Button>
</ContentTemplate>
</ajax:TabPanel>
</ajax:TabContainer>
You can review the available Tab API in this article: http://www.asp.net/ajaxlibrary/tabs-control.ashx
There is a OnClientClick event which expects a name of a javascript function to attach to the client-side click event of the tab. You can implement your own logic that checks for the tab press and cancel the event by setting "return false" to disable the operation.
Rumen

In an ASP.Net LoginView, 2 sets of UserName and Password TextBoxes are displayed

Using an ASP.Net LoginView, 2 sets of UserName and Password TextBoxes are displayed.
Using this markup, can you tell us how to only show 1 set of the UserName and Password TextBoxes from the AnonymousTemplate?
<asp:UpdatePanel
ID="UpdatePanelParentsSummary"
runat="server"
UpdateMode="Conditional">
<ContentTemplate>
<div class="Menu">
<asp:LoginView
ID="loginViewMain"
runat="server">
<AnonymousTemplate>
<asp:LoginStatus
ID="loginStatus"
runat="server" />
<asp:Login runat="server" ID="login"></asp:Login>
</AnonymousTemplate>
<LoggedInTemplate>
<asp:LoginName
ID="loginName"
runat="server"
FormatString="Hello, {0}!<br/><br/> You have successfully<br/> logged onto the staff site." />
(<asp:LoginStatus ID="loginStatus" runat="server" />)
<asp:SiteMapDataSource
id="KnowledgeAcademySiteMap"
runat="server"
ShowStartingNode="false" />
<asp:TreeView
id="TreeViewMain"
runat="server"
ExpandDepth="0"
OnTreeNodeExpanded="TreeViewMain_TreeNodeExpanded"
DataSourceID="KnowledgeAcademySiteMap">
<RootNodeStyle ImageUrl="/Images/book.png" />
<ParentNodeStyle ImageUrl="/Images/book.png" />
<LeafNodeStyle ImageUrl="/Images/book.png" />
</asp:TreeView>
</LoggedInTemplate>
</asp:LoginView>
</div>
</asp:ContentPlaceHolder>
</ContentTemplate>
</asp:UpdatePanel>
With the markup shown, there should be only one <asp:Login> control displayed. There must be some other markup or code that is affecting this.
The best option at this point would be to debug the problem by adding some additional content that will help figure out where everything is coming from. For example, you could try the following:
Enable Tracing in the page by setting Trace=true in the #Page directive at the top of the ASPX file. That will render a graph of the control tree at the bottom of the page, which might help you figure out where the other Login control is.
Add some dummy markup immediately before and after the Login control that you see, such as putting the text BEGINBEGINBEGIN right before it, and ENDENDEND right after it. If you see that rendering once, then there's a different Login control coming from somewhere else. If you see it rendering twice, then something is causing the whole control to render twice, which must be coming from some custom code that is not being shown.
Start removing bits and pieces of the page to simplify it, until you figure out what is causing the difference.

Create a web page as 2 parts

I want to develop a registration form with 2 panels. One panel is personal information and another is address details. In these panels the user fills in all details of personal information and after completion of this, the user clicks on Add Address Details. If the user clicks on that, the second panel should be visible without page refresh. How can I accomplish this?
you would use javascript to append the append the second form to to the div of the first form. Or however you have it setup. So in jquery it would look something like this:
<script type="text/javascript">
$('#buttonid').click(function() {
$('#divid').append("<form><input />etc etc etc</form>");
});
</script>
You should use the ASP.NET Wizard control. It is the perfect tool for these scenarios. Scott Gu has a post with some links explaining how to use it. I recommend you look at it.There's a video linked that walks you through a full example.
I don't know, but hiding/showing panels based on certain conditions on the same page feels inelegant and hacky to me.
You should look into using an UpdatePanel. Within there you could place to Panels with different form information. Tie in a few button clicks and hide/show the different panels.
Edit with a simple example
<asp:ScriptManager ID="sm" runat="server"></asp:ScriptManager>
<asp:UpdatePanel id="upnl" runat="server" ChildrenAsTriggers="true">
<ContentTemplate>
<asp:Panel ID="pnl1" runat="server">
<!--personal info-->
<asp:Button ID="btn1" runat="server" Text="this button could validate personal info, hide pnl1 and show pnl2" />
</asp:Panel>
<asp:Panel ID="pn2" runat="server" Visible="false">
<!--address info-->
<asp:Button id="btn2" runat="server" Text="this button could validate address information and finally submit the form." />
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>

Handle Enter Key for search and login page

I have an ASP.Net 3.5 in VB.Net page that uses a master-page and has several pages it uses to display content. On the master-page their is a search text-box for the site so that shows up on all the pages. Their is also a login page screen to get to a members account.
The issue is: When you are on a content page I would like when a user puts something in to search for and presses enter it will process the search request.
But....
If you are on the login page I would like to disable enter key for the search and if the user presses enter on the login page to process the login request.
Is their an easy way to accomplish this?
Create your form in an asp:panel and set the DefaultButton to be the button of the form you want to submit when enter is pressed
<asp:Panel DefaultButton="btnSubmit" runat="server">
<asp:TextBox ID="UserName" runat="server"/>
<asp:TextBox ID="Password" TextMode="Password" runat="server"/>
<asp:Button ID="btnSubmit" OnClick="btnSubmit_Click" Text="Sign In" runat="server"/>
</asp:Panel>
Note: asp:panel creates a div

IE 8 - ASP.NET form not submitting when user presses enter key

I have a simple form written in asp.net/C# and when trying to hit enter while in the form's input box doesn't submit the form for some reason. I had implemented a fix for a previous bug where pressing enter would merely refresh the page without submitting the form data but now pressing enter just does nothing, the fix is below:
<div style="display: none">
<input type="text" name="hiddenText" />
</div>
anybody know about a fix for this or a workaround?
I'm assuming you have a button somewhere on your page, as well as an event handler for it.
Have you tried wrapping your form (with the button) inside a Panel control and setting the default button attribute?
i.e.
<asp:Panel id="pnlMyForm" runat="server" DefaultButton="btnMyButton">
<asp:textbox id="txtInput" runat="server" />
<asp:Button id="btnMyButton" text="Submit" runat="server" />
</asp:Panel>
You can specify a default button for a form, which means hitting enter on any input control will fire that button (i.e. target the submit button). I haven't heard of this not working in any specific browser. This should eliminate your need for a workaround/hack.
<form id="form1" runat="server">
<asp:Panel ID="pnlFormContents" runat="server" DefaultButton="btnSubmit">
<!-- add some input controls as needed -->
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click"/>
</asp:Panel>
</form>
Hope this helps...
I don't remember the specifics of the rules, but most browsers have the capability of submitting forms when ENTER is pressed if conditions are met. I think it had to do with whether you had 1 or more-than-one field, or whether or not there was at least one submit button (even if you hide it). I've done it in a site I recently did, but I don't have the code handy, but I can tell you it works without any special scripting. Check this posting for more details:
http://manfred.dschini.org/2007/09/20/submit-form-on-enter-key/

Resources