469,950 Members | 2,041 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,950 developers. It's quick & easy.

Authentication OnClick="Button_Click" Event problem

Please help. I'm dying here and pulling out the last few remaining elements
of my hair over this.

I have built a form that will identify and authenticate users. I keep
getting the following error with the Form:

Compiler Error Message: CS0122:
'Sonar3.Controls.LoginControl.Button_Click(object, System.EventArgs)' is
inaccessible due to its protection level

Source Error:

Line 17: <td bgColor="goldenrod" colSpan="2">
Line 18: <P align="right">
Line 19: <asp:button id="Button1" OnClick="Button_Click" runat="server"
Text="Login"></asp:button></P>
Line 20: </td>
Line 21: </tr>

I don't what to do about this. Below is the Interface source and the
code-behind source:

Interface source:
<%@ Control Language="c#" AutoEventWireup="false"
Codebehind="LoginControl.ascx.cs" Inherits="Sonar3.Controls.LoginControl"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<table class="opaque70" style="WIDTH: 351px; HEIGHT: 171px" cellSpacing="3"
cellPadding="10"
width="351" align="center" border="0">
<tr>
<td bgColor="goldenrod" colSpan="2"><FONT face="Verdana" color="#ffffff"
size="2"><STRONG> Staff
Login</STRONG></FONT></td>
</tr>
<tr>
<td style="WIDTH: 105px" bgColor="#000033"><asp:label id="Label1"
runat="server" CssClass="txt">Email Address:</asp:label></td>
<td bgColor="#4682b4"><asp:textbox id="TextBox1"
runat="server"></asp:textbox></td>
</tr>
<tr>
<td style="WIDTH: 105px" bgColor="#000033"><asp:label id="Label2"
runat="server" CssClass="txt">Password:</asp:label></td>
<td bgColor="#4682b4"><asp:textbox id="TextBox2" runat="server"
TextMode="Password"></asp:textbox></td>
</tr>
<tr>
<td bgColor="goldenrod" colSpan="2">
<P align="right">
<asp:button id="Button1" OnClick="Button_Click" runat="server"
Text="Login"></asp:button></P>
</td>
</tr>
<tr>
<td colSpan="2"><asp:label id="lblmsg" Runat="server"></asp:label></td>
</tr>
</table>
Code Behind Source:

namespace Sonar3.Controls
{
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Drawing;
using System.Web.Security;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for LoginControl.
/// </summary>
public class LoginControl : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Label lblmsg;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.Button Button1;
protected System.Data.SqlClient.SqlConnection clubconn;
protected System.Data.SqlClient.SqlCommand clubcmd;
protected System.Data.SqlClient.SqlParameter clubParam;

int intResult;

private void Page_Load(object sender, System.EventArgs e)
{
// string LinkPath;
//
}
private void Button_Click(object sender, System.EventArgs e)
{
if(DBAuthenticate(TextBox1.Text, TextBox2.Text)>0)
{
FormsAuthenticationTicket ticket = new
FormsAuthenticationTicket(1,"loggedonuser", DateTime.Now,
DateTime.Now.AddMinutes(Session.Timeout),false, TextBox1.Text);

//Encrypt the ticket.
string encryptedTicket = FormsAuthentication.Encrypt(ticket);

//Create a cookie, and then add the encrypted ticket to the cookie as
data.
HttpCookie authCookie = new
HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);

//Add the cookie to the outgoing cookies collection.
Response.Cookies.Add(authCookie);

//You can redirect now.
FormsAuthentication.RedirectFromLoginPage(TextBox1 .Text, false);
}

}
int DBAuthenticate(string strUserName, string strPassword)
{
string strclubconn=ConfigurationSettings.AppSettings["ConnectionString"];
clubconn=new SqlConnection(strclubconn);
clubcmd.CommandType=CommandType.StoredProcedure;
clubParam=clubcmd.Parameters.Add("RETURN_VALUE", SqlDbType.Int);
clubParam.Direction=ParameterDirection.ReturnValue ;
clubcmd.Parameters.Add("@username",strUserName);
clubcmd.Parameters.Add("@password",strPassword);
clubconn.Open();
clubcmd.ExecuteNonQuery();
intResult=System.Convert.ToInt32(clubcmd.Parameter s[ "RETURN_VALUE"
].Value);
clubconn.Close();
if(intResult<0)
{
if(intResult== -1)
{
lblmsg.Text="User email Not Registered";
}
else
{
lblmsg.Text="Invalid Password. Please try again.";
}
}
return intResult;
}

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

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}


Nov 19 '05 #1
3 2279
Seems like somewhere in your code you're calling the Button_Click event
handler from outside the class or from one of its derivatives. Change its
accessiblity to protected, and if that didn't owrk, to public. And check
where it's getting called from because such an event handler should work fine
with private protection level.

"I am Sam" wrote:
Please help. I'm dying here and pulling out the last few remaining elements
of my hair over this.

I have built a form that will identify and authenticate users. I keep
getting the following error with the Form:

Compiler Error Message: CS0122:
'Sonar3.Controls.LoginControl.Button_Click(object, System.EventArgs)' is
inaccessible due to its protection level

Source Error:

Line 17: <td bgColor="goldenrod" colSpan="2">
Line 18: <P align="right">
Line 19: <asp:button id="Button1" OnClick="Button_Click" runat="server"
Text="Login"></asp:button></P>
Line 20: </td>
Line 21: </tr>

I don't what to do about this. Below is the Interface source and the
code-behind source:

Interface source:
<%@ Control Language="c#" AutoEventWireup="false"
Codebehind="LoginControl.ascx.cs" Inherits="Sonar3.Controls.LoginControl"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<table class="opaque70" style="WIDTH: 351px; HEIGHT: 171px" cellSpacing="3"
cellPadding="10"
width="351" align="center" border="0">
<tr>
<td bgColor="goldenrod" colSpan="2"><FONT face="Verdana" color="#ffffff"
size="2"><STRONG> Staff
Login</STRONG></FONT></td>
</tr>
<tr>
<td style="WIDTH: 105px" bgColor="#000033"><asp:label id="Label1"
runat="server" CssClass="txt">Email Address:</asp:label></td>
<td bgColor="#4682b4"><asp:textbox id="TextBox1"
runat="server"></asp:textbox></td>
</tr>
<tr>
<td style="WIDTH: 105px" bgColor="#000033"><asp:label id="Label2"
runat="server" CssClass="txt">Password:</asp:label></td>
<td bgColor="#4682b4"><asp:textbox id="TextBox2" runat="server"
TextMode="Password"></asp:textbox></td>
</tr>
<tr>
<td bgColor="goldenrod" colSpan="2">
<P align="right">
<asp:button id="Button1" OnClick="Button_Click" runat="server"
Text="Login"></asp:button></P>
</td>
</tr>
<tr>
<td colSpan="2"><asp:label id="lblmsg" Runat="server"></asp:label></td>
</tr>
</table>
Code Behind Source:

namespace Sonar3.Controls
{
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Drawing;
using System.Web.Security;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for LoginControl.
/// </summary>
public class LoginControl : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Label lblmsg;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.Button Button1;
protected System.Data.SqlClient.SqlConnection clubconn;
protected System.Data.SqlClient.SqlCommand clubcmd;
protected System.Data.SqlClient.SqlParameter clubParam;

int intResult;

private void Page_Load(object sender, System.EventArgs e)
{
// string LinkPath;
//
}
private void Button_Click(object sender, System.EventArgs e)
{
if(DBAuthenticate(TextBox1.Text, TextBox2.Text)>0)
{
FormsAuthenticationTicket ticket = new
FormsAuthenticationTicket(1,"loggedonuser", DateTime.Now,
DateTime.Now.AddMinutes(Session.Timeout),false, TextBox1.Text);

//Encrypt the ticket.
string encryptedTicket = FormsAuthentication.Encrypt(ticket);

//Create a cookie, and then add the encrypted ticket to the cookie as
data.
HttpCookie authCookie = new
HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);

//Add the cookie to the outgoing cookies collection.
Response.Cookies.Add(authCookie);

//You can redirect now.
FormsAuthentication.RedirectFromLoginPage(TextBox1 .Text, false);
}

}
int DBAuthenticate(string strUserName, string strPassword)
{
string strclubconn=ConfigurationSettings.AppSettings["ConnectionString"];
clubconn=new SqlConnection(strclubconn);
clubcmd.CommandType=CommandType.StoredProcedure;
clubParam=clubcmd.Parameters.Add("RETURN_VALUE", SqlDbType.Int);
clubParam.Direction=ParameterDirection.ReturnValue ;
clubcmd.Parameters.Add("@username",strUserName);
clubcmd.Parameters.Add("@password",strPassword);
clubconn.Open();
clubcmd.ExecuteNonQuery();
intResult=System.Convert.ToInt32(clubcmd.Parameter s[ "RETURN_VALUE"
].Value);
clubconn.Close();
if(intResult<0)
{
if(intResult== -1)
{
lblmsg.Text="User email Not Registered";
}
else
{
lblmsg.Text="Invalid Password. Please try again.";
}
}
return intResult;
}

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

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}

Nov 19 '05 #2
I looked further into your code. When you wire events in the ASPX page
through OnClick="EvenHandler", you need to either define the event handler
Button_Click in the <script runat=server> section of the ASPX page with
private accessibility or define it as protected in the Code Behind class.
That's because the ASPX page becomes a derived class from the Code Behind
class, and it won't be able to see the Code Behind's private members.
"I am Sam" wrote:
Please help. I'm dying here and pulling out the last few remaining elements
of my hair over this.

I have built a form that will identify and authenticate users. I keep
getting the following error with the Form:

Compiler Error Message: CS0122:
'Sonar3.Controls.LoginControl.Button_Click(object, System.EventArgs)' is
inaccessible due to its protection level

Source Error:

Line 17: <td bgColor="goldenrod" colSpan="2">
Line 18: <P align="right">
Line 19: <asp:button id="Button1" OnClick="Button_Click" runat="server"
Text="Login"></asp:button></P>
Line 20: </td>
Line 21: </tr>

I don't what to do about this. Below is the Interface source and the
code-behind source:

Interface source:
<%@ Control Language="c#" AutoEventWireup="false"
Codebehind="LoginControl.ascx.cs" Inherits="Sonar3.Controls.LoginControl"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<table class="opaque70" style="WIDTH: 351px; HEIGHT: 171px" cellSpacing="3"
cellPadding="10"
width="351" align="center" border="0">
<tr>
<td bgColor="goldenrod" colSpan="2"><FONT face="Verdana" color="#ffffff"
size="2"><STRONG> Staff
Login</STRONG></FONT></td>
</tr>
<tr>
<td style="WIDTH: 105px" bgColor="#000033"><asp:label id="Label1"
runat="server" CssClass="txt">Email Address:</asp:label></td>
<td bgColor="#4682b4"><asp:textbox id="TextBox1"
runat="server"></asp:textbox></td>
</tr>
<tr>
<td style="WIDTH: 105px" bgColor="#000033"><asp:label id="Label2"
runat="server" CssClass="txt">Password:</asp:label></td>
<td bgColor="#4682b4"><asp:textbox id="TextBox2" runat="server"
TextMode="Password"></asp:textbox></td>
</tr>
<tr>
<td bgColor="goldenrod" colSpan="2">
<P align="right">
<asp:button id="Button1" OnClick="Button_Click" runat="server"
Text="Login"></asp:button></P>
</td>
</tr>
<tr>
<td colSpan="2"><asp:label id="lblmsg" Runat="server"></asp:label></td>
</tr>
</table>
Code Behind Source:

namespace Sonar3.Controls
{
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Drawing;
using System.Web.Security;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for LoginControl.
/// </summary>
public class LoginControl : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Label lblmsg;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.Button Button1;
protected System.Data.SqlClient.SqlConnection clubconn;
protected System.Data.SqlClient.SqlCommand clubcmd;
protected System.Data.SqlClient.SqlParameter clubParam;

int intResult;

private void Page_Load(object sender, System.EventArgs e)
{
// string LinkPath;
//
}
private void Button_Click(object sender, System.EventArgs e)
{
if(DBAuthenticate(TextBox1.Text, TextBox2.Text)>0)
{
FormsAuthenticationTicket ticket = new
FormsAuthenticationTicket(1,"loggedonuser", DateTime.Now,
DateTime.Now.AddMinutes(Session.Timeout),false, TextBox1.Text);

//Encrypt the ticket.
string encryptedTicket = FormsAuthentication.Encrypt(ticket);

//Create a cookie, and then add the encrypted ticket to the cookie as
data.
HttpCookie authCookie = new
HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);

//Add the cookie to the outgoing cookies collection.
Response.Cookies.Add(authCookie);

//You can redirect now.
FormsAuthentication.RedirectFromLoginPage(TextBox1 .Text, false);
}

}
int DBAuthenticate(string strUserName, string strPassword)
{
string strclubconn=ConfigurationSettings.AppSettings["ConnectionString"];
clubconn=new SqlConnection(strclubconn);
clubcmd.CommandType=CommandType.StoredProcedure;
clubParam=clubcmd.Parameters.Add("RETURN_VALUE", SqlDbType.Int);
clubParam.Direction=ParameterDirection.ReturnValue ;
clubcmd.Parameters.Add("@username",strUserName);
clubcmd.Parameters.Add("@password",strPassword);
clubconn.Open();
clubcmd.ExecuteNonQuery();
intResult=System.Convert.ToInt32(clubcmd.Parameter s[ "RETURN_VALUE"
].Value);
clubconn.Close();
if(intResult<0)
{
if(intResult== -1)
{
lblmsg.Text="User email Not Registered";
}
else
{
lblmsg.Text="Invalid Password. Please try again.";
}
}
return intResult;
}

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

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}

Nov 19 '05 #3
Hi Sam,

Try removing the OnClick="Button_Click" from .aspx page. And add the event
handler in

private void InitializeComponent()

this.Button.Click += new System.EventHandler(this.Button_Click);
Cheers,

Jerome. M

"I am Sam" wrote:
Please help. I'm dying here and pulling out the last few remaining elements
of my hair over this.

I have built a form that will identify and authenticate users. I keep
getting the following error with the Form:

Compiler Error Message: CS0122:
'Sonar3.Controls.LoginControl.Button_Click(object, System.EventArgs)' is
inaccessible due to its protection level

Source Error:

Line 17: <td bgColor="goldenrod" colSpan="2">
Line 18: <P align="right">
Line 19: <asp:button id="Button1" OnClick="Button_Click" runat="server"
Text="Login"></asp:button></P>
Line 20: </td>
Line 21: </tr>

I don't what to do about this. Below is the Interface source and the
code-behind source:

Interface source:
<%@ Control Language="c#" AutoEventWireup="false"
Codebehind="LoginControl.ascx.cs" Inherits="Sonar3.Controls.LoginControl"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<table class="opaque70" style="WIDTH: 351px; HEIGHT: 171px" cellSpacing="3"
cellPadding="10"
width="351" align="center" border="0">
<tr>
<td bgColor="goldenrod" colSpan="2"><FONT face="Verdana" color="#ffffff"
size="2"><STRONG> Staff
Login</STRONG></FONT></td>
</tr>
<tr>
<td style="WIDTH: 105px" bgColor="#000033"><asp:label id="Label1"
runat="server" CssClass="txt">Email Address:</asp:label></td>
<td bgColor="#4682b4"><asp:textbox id="TextBox1"
runat="server"></asp:textbox></td>
</tr>
<tr>
<td style="WIDTH: 105px" bgColor="#000033"><asp:label id="Label2"
runat="server" CssClass="txt">Password:</asp:label></td>
<td bgColor="#4682b4"><asp:textbox id="TextBox2" runat="server"
TextMode="Password"></asp:textbox></td>
</tr>
<tr>
<td bgColor="goldenrod" colSpan="2">
<P align="right">
<asp:button id="Button1" OnClick="Button_Click" runat="server"
Text="Login"></asp:button></P>
</td>
</tr>
<tr>
<td colSpan="2"><asp:label id="lblmsg" Runat="server"></asp:label></td>
</tr>
</table>
Code Behind Source:

namespace Sonar3.Controls
{
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Drawing;
using System.Web.Security;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for LoginControl.
/// </summary>
public class LoginControl : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Label lblmsg;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.Button Button1;
protected System.Data.SqlClient.SqlConnection clubconn;
protected System.Data.SqlClient.SqlCommand clubcmd;
protected System.Data.SqlClient.SqlParameter clubParam;

int intResult;

private void Page_Load(object sender, System.EventArgs e)
{
// string LinkPath;
//
}
private void Button_Click(object sender, System.EventArgs e)
{
if(DBAuthenticate(TextBox1.Text, TextBox2.Text)>0)
{
FormsAuthenticationTicket ticket = new
FormsAuthenticationTicket(1,"loggedonuser", DateTime.Now,
DateTime.Now.AddMinutes(Session.Timeout),false, TextBox1.Text);

//Encrypt the ticket.
string encryptedTicket = FormsAuthentication.Encrypt(ticket);

//Create a cookie, and then add the encrypted ticket to the cookie as
data.
HttpCookie authCookie = new
HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);

//Add the cookie to the outgoing cookies collection.
Response.Cookies.Add(authCookie);

//You can redirect now.
FormsAuthentication.RedirectFromLoginPage(TextBox1 .Text, false);
}

}
int DBAuthenticate(string strUserName, string strPassword)
{
string strclubconn=ConfigurationSettings.AppSettings["ConnectionString"];
clubconn=new SqlConnection(strclubconn);
clubcmd.CommandType=CommandType.StoredProcedure;
clubParam=clubcmd.Parameters.Add("RETURN_VALUE", SqlDbType.Int);
clubParam.Direction=ParameterDirection.ReturnValue ;
clubcmd.Parameters.Add("@username",strUserName);
clubcmd.Parameters.Add("@password",strPassword);
clubconn.Open();
clubcmd.ExecuteNonQuery();
intResult=System.Convert.ToInt32(clubcmd.Parameter s[ "RETURN_VALUE"
].Value);
clubconn.Close();
if(intResult<0)
{
if(intResult== -1)
{
lblmsg.Text="User email Not Registered";
}
else
{
lblmsg.Text="Invalid Password. Please try again.";
}
}
return intResult;
}

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

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}

Nov 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.