Show Hide ASP.Net Panel based on Button Clicked using C# and VB.Net

Last Reply 19 days ago By pandeyism

Posted 19 days ago

I can not insert image here. Let me explain the problem.

1. I have 2 Button on same page (User_login,Admin_login) but only these 2 buttons are visible.

2. My requirement is, when I will click any button, the corresponding fields will be visible.

I have made them true inside corresponding button_click event

But problem is, those text are not appearing.

My requirement is,

1. After clicking any button, the corresponding Fields Should Appear (EIther USER Log IN Text BoxOR ADMIN Log in text Box)

2. I should be able to enter userID and Password, then again when I will click on the SAME corresponding LOG IN Button, it should check in database and on successful matching credential, it should redirect to other PAGE. 

SO under the corresponding BUTTON_CLICK event, after the statements , I have written code to connect to Database .

PROBLEM  is, the TEXT Boxes are not appearing and the control always moves to next line of code.

Please see my complete code. You will understand where is my problem.

Can U please give me some IDEA how to implement this.

Posted 19 days ago Modified on 15 days ago

Hey BubunDash,

Please refer below sample.

HTML

C#

<asp:Button ID="Button1" Text="UserLogin" runat="server" OnClick="UserLogin" /><br />
<br />
<asp:Button ID="Button2" Text="AdminLogin" runat="server" OnClick="AdminLogin" /><br />
<asp:Panel runat="server" ID="PanelUser">
    <asp:Label Text="UserName" runat="server" ID="lblUserName" />
    <asp:TextBox runat="server" ID="Text_UserName" />
    <asp:Label ID="lblUserPass" Text="UserPassword" runat="server" />
    <asp:TextBox runat="server" ID="Text_Password" />
</asp:Panel>
<asp:Panel runat="server" ID="PanelAdmin">
    <asp:Label ID="lblAdminName" Text="AdminName" runat="server" />
    <asp:TextBox runat="server" ID="Text_AdminUserName" />
    <asp:Label ID="lblAdminPassword" Text="AdminPassword" runat="server" />
    <asp:TextBox runat="server" ID="Text_AdminPassword" />
</asp:Panel>

Namespaces

C#

using System.Data.SqlClient;
using System.Configuration;

VB.Net

Imports System.Data.SqlClient

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        PanelUser.Visible = false;
        PanelAdmin.Visible = false;
    }
}
protected void UserLogin(object sender, EventArgs e)
{
    string name = Text_UserName.Text;
    string password = Text_Password.Text;
    if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(password))
    {
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT Name, Country FROM Customers WHERE Name =@Name AND Country = @Country", con))
            {
                cmd.Parameters.AddWithValue("@Name", name);
                cmd.Parameters.AddWithValue("@Country", password);
                con.Open();
                string value = Convert.ToString(cmd.ExecuteScalar());
                if (!string.IsNullOrEmpty(value))
                {
                    //User Panel
                    Response.Redirect("Default.aspx");
                }
                else
                {
                    Text_UserName.Focus();
                    Text_UserName.BackColor = System.Drawing.Color.Yellow;
                }
                con.Close();
            }
        }
    }
    else
    {
        PanelUser.Visible = true;
    }
}

protected void AdminLogin(object sender, EventArgs e)
{
    string name = Text_AdminUserName.Text;
    string password = Text_AdminPassword.Text;
    if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(password))
    {
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT Name, Country FROM Customers WHERE Name =@Name AND Country = @Country", con))
            {
                cmd.Parameters.AddWithValue("@Name", name);
                cmd.Parameters.AddWithValue("@Country", password);
                con.Open();
                string value = Convert.ToString(cmd.ExecuteScalar());
                if (!string.IsNullOrEmpty(value))
                {
                    //Admin Panel
                    Response.Redirect("Admin.aspx");
                }
                else
                {
                    Text_AdminUserName.Focus();
                    Text_AdminUserName.BackColor = System.Drawing.Color.Yellow;
                }
                con.Close();
            }
        }
    }
    else
    {
        PanelAdmin.Visible = true;
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        PanelUser.Visible = False
        PanelAdmin.Visible = False
    End If
End Sub

Protected Sub UserLogin(ByVal sender As Object, ByVal e As EventArgs)
    Dim name As String = Text_UserName.Text
    Dim password As String = Text_Password.Text

    If Not String.IsNullOrEmpty(name) AndAlso Not String.IsNullOrEmpty(password) Then
        Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
        Using con As SqlConnection = New SqlConnection(constr)
            Using cmd As SqlCommand = New SqlCommand("SELECT Name, Country FROM Customers WHERE Name =@Name AND Country = @Country", con)
                cmd.Parameters.AddWithValue("@Name", name)
                cmd.Parameters.AddWithValue("@Country", password)
                con.Open()
                Dim value As String = Convert.ToString(cmd.ExecuteScalar())
                If Not String.IsNullOrEmpty(value) Then
                    Response.Redirect("Default.aspx")
                Else
                    Text_UserName.Focus()
                    Text_UserName.BackColor = System.Drawing.Color.Yellow
                End If

                con.Close()
            End Using
        End Using
    Else
        PanelUser.Visible = True
    End If
End Sub

Protected Sub AdminLogin(ByVal sender As Object, ByVal e As EventArgs)
    Dim name As String = Text_AdminUserName.Text
    Dim password As String = Text_AdminPassword.Text
    If Not String.IsNullOrEmpty(name) AndAlso Not String.IsNullOrEmpty(password) Then
        Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
        Using con As SqlConnection = New SqlConnection(constr)
            Using cmd As SqlCommand = New SqlCommand("SELECT Name, Country FROM Customers WHERE Name =@Name AND Country = @Country", con)
                cmd.Parameters.AddWithValue("@Name", name)
                cmd.Parameters.AddWithValue("@Country", password)
                con.Open()
                Dim value As String = Convert.ToString(cmd.ExecuteScalar())

                If Not String.IsNullOrEmpty(value) Then
                    Response.Redirect("Admin.aspx")
                Else
                    Text_AdminUserName.Focus()
                    Text_AdminUserName.BackColor = System.Drawing.Color.Yellow
                End If

                con.Close()
            End Using
        End Using
    Else
        PanelAdmin.Visible = True
    End If
End Sub

Screenshot