Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how to attribute to loginbutton  in the loginform

Posted on 2009-04-11
9
Medium Priority
?
398 Views
Last Modified: 2013-11-07
Just like this , i need to add attribute for the loginbutton inside the login form ,
Login1.Attributes.Add("onclick", "return validate();")

but if i enter loginbutton i get name is not declared,
like this
LoginButton.Attributes.Add("onclick", "return validate();")

I want to add this atribute on pageload only , not on the onclientclick in design page.....plz suggest,,
<asp:Login ID="Login1" runat="server" >
                    <LayoutTemplate>
                        <table border="0" cellpadding="1" cellspacing="0" style="border-collapse: collapse">
                            <tr>
                                <td style="height: 146px">
                                    <table border="0" cellpadding="0">
                                        <tr>
                                            <td align="center" colspan="2" style="height: 12px">
                                                Log In</td>
                                        </tr>
                                        <tr>
                                            <td align="right">
                                                <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">User Name:</asp:Label></td>
                                            <td>
                                                <asp:TextBox ID="UserName" runat="server"></asp:TextBox>
                                                <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName"
                                                    ErrorMessage="User Name is required." ToolTip="User Name is required." ValidationGroup="Login1">*</asp:RequiredFieldValidator>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td align="right">
                                                <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label></td>
                                            <td>
                                                <asp:TextBox ID="Password" runat="server" TextMode="Password"></asp:TextBox>
                                                <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password"
                                                    ErrorMessage="Password is required." ToolTip="Password is required." ValidationGroup="Login1">*</asp:RequiredFieldValidator>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td colspan="2">
                                                <asp:CheckBox ID="RememberMe" runat="server" Text="Remember me next time." />
                                            </td>
                                        </tr>
                                        <tr>
                                            <td align="center" colspan="2" style="color: red">
                                                <asp:Literal ID="FailureText" runat="server" EnableViewState="False"></asp:Literal>
                                                <asp:Label ID="Label1" runat="server" Style="position: relative" Text="Label"></asp:Label></td>
                                        </tr>
                                        <tr>
                                            <td align="right" colspan="2">
                                                <asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl="~/forgorpassword.aspx"
                                                    Style="left: 0px; position: relative; top: 0px">ForgetPassword?</asp:HyperLink>&nbsp;
                                                <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/regis1.aspx" Style="left: -139px;
                                                    position: relative; top: 1px">SignUP?</asp:HyperLink>
                                                        <asp:Button ID="LoginButton" runat="server" CommandName="Login"
                                                    Text="Log In" ValidationGroup="Login1" />
                                                   
                                            </td>
                                        </tr>
                                    </table>
                                </td>
                            </tr>
                        </table>
                    </LayoutTemplate>
                                   </asp:Login>

Open in new window

0
Comment
Question by:Rajar Ahmed
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 23

Expert Comment

by:apresto
ID: 24121713
Try this (C#):
((Button)Login1.FindControl("LoginButton")).Attributes.Add("onclick", "return validate();")
0
 
LVL 23

Expert Comment

by:apresto
ID: 24121717
Sorry didnt realise it was in VB, just change the casting to VB.Net instead of this ((button).... as this is a c# casting
0
 
LVL 18

Author Comment

by:Rajar Ahmed
ID: 24121732
i converted , , accordingly to vb,
  Dim loginButton As Button = Login1.FindControl("LoginButton")
         loginButton .Attributes.Add("onclick", "return validate();")
 
  but this generates a postback which is unneccessary .....i need to avoid the postback ...
 if i add like this
 Login1.Attributes.Add("onclick", "return validate();") .am not gettin postback......

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 23

Expert Comment

by:apresto
ID: 24136350
If you do "Login1.Attributes...." you are adding the attribute to the actual Login control, if you use the other you are assigning the attribute to the button itself.
Do you want to postback if you form is validated and NOt postback when it is NOT validated? Or you dont want a postback fullstop?
0
 
LVL 18

Author Comment

by:Rajar Ahmed
ID: 24136394
i need to do the simple task of validation , when its not validated the page should not postback and pops out the error message ,
0
 
LVL 23

Expert Comment

by:apresto
ID: 24136453
are you returning true or false in your validation, based on whether the form is valid, in the Javascript?
0
 
LVL 18

Author Comment

by:Rajar Ahmed
ID: 24136563
I need to display the failed message , without the post back  onclicking the loginbutton  .. i pasted the javascript  below....
It display failed message but after the postback.....need to avoid the postback on loginbutton click...
 

 if(xmlHttp.responseText == "failed")
        {
            document.getElementById("Login1_Label1").innerHTML='Failed';
        }

<script language="javascript" >
function validate()
{
var xmlHttp;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
  xmlHttp.onreadystatechange=function()//already exists
    {
    if(xmlHttp.readyState==4)
      {
      
      if(xmlHttp.responseText == "failed")
        {
            document.getElementById("Login1_Label1").innerHTML='Failed';
        }
      else
            { 
              window.location.href = "index.aspx";            
      }
      }
    }
    alert("togetquery");
  xmlHttp.open("GET","index.aspx?proc=chk&val="+document.getElementById("Login1_UserName").value+"&val2="+document.getElementById("Login1_Password").value,true);
  xmlHttp.send(null);
  }
</script>

Open in new window

0
 
LVL 23

Accepted Solution

by:
apresto earned 2000 total points
ID: 24138512
Make sure you return false where you dont want the form to submit:i.e

 if(xmlHttp.responseText == "failed")
       {
           document.getElementById("Login1_Label1").innerHTML='Failed';
           return false;
       }
 
 
or if you dont want to return false there, create a holding variable to hold your boolean value:
 
var success = false; //normally at the top of your function/class
....
success = false; //When you decide in a condition that you dont want to submit
...
return success;//Stop the script and return the success state

Open in new window

0
 
LVL 18

Author Comment

by:Rajar Ahmed
ID: 24140928
total confusionnnnnnn,,,,,,,,,,,,,,,

after click loginbutton, imdiately its getting postback before validating itself and after returning the false message.....
if i use this code/.....
 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim loginButton As Button = Login1.FindControl("LoginButton")
        loginButton.Attributes.Add("onclick", "return chkEmail();")
    End Sub

whereas in code This, the page is not gettin post back ,and returning false message..........just like a client side validation....
 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Login1.Attributes.Add("onclick", "return chkEmail();")
    End Sub

Where i s the problem..??

i guess problem is not in the javascript.......plz sugggest......??

function chkEmail()
{
alert("incheckemail");
var xmlHttp;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
  xmlHttp.onreadystatechange=function()//already exists
    {
    if(xmlHttp.readyState==4)
      {
      
      if(xmlHttp.responseText == "failed")
        {
        alert("in fail");
document.getElementById("Login1_Label1").innerHTML='Failed';
return false;
        }
      else
            { alert("inelse");
 
              window.location.href = "hi.aspx";       
      }
 
      }
    }
    
  xmlHttp.open("GET","index.aspx?proc=chk&val="+document.getElementById("Login1_UserName").value+"&val2="+document.getElementById("Login1_Password").value,true);
  xmlHttp.send(null);
  }

Open in new window

0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question