Marius Vasile

Marius Vasile

  • 594
  • 1.9k
  • 144.8k

No button works on main page

Aug 24 2023 11:43 AM

I don't know what is happening but all of a sudden no buttons works on my main page in an asp.net c# application. I tried the same button and procedure on another page and it works perfectly. Where should be the problem?

<asp:Button ID="btnTest" OnClick="Test" runat="server" class="btn btn-md lfc-fossil ml-1" style="font-size:14px;width:150px" Text="Test" />
<asp:TextBox class="form-control mr-1 lfc-green hidden" ID="txtTest" runat="server" placeholder="Text Test" style="font-size:14px;width:150px" />

and code behind

protected void Test(object sender, EventArgs e)
{
    txtTest.Attributes["class"] = txtTest.Attributes["class"].Replace("hidden", "");
}

 Main page view

<%@ Page Title="Home" Language="C#" MasterPageFile="~/Instruire.Master" AutoEventWireup="true" CodeBehind="Home.aspx.cs" Inherits="Instruire.Home" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <div class="container-fluid bg-white p-2" style="max-width:90%">
        <div id="DivLogin" runat="server" class="hidden" style="border-style:solid;">
            <div class="row no-gutters justify-content-center mb-2">
                <div class="col-6 text-center">
                    <asp:Label class="h4" runat="server">Autentificare</asp:Label>
                </div>
            </div>
            <div class="row no-gutters justify-content-center mb-2">
                <div class="col-6 text-center">
                    <div class="row no-gutters">
                        <div class="col-4">
                            <asp:Label class="form-control border-0" runat="server">Utilizator:</asp:Label>
                        </div>
                        <div class="col-4">
                            <asp:TextBox class="form-control" runat="server" id="txtUserName"></asp:TextBox>
                        </div>
                        <div class="col-4">
                            <asp:RequiredFieldValidator ControlToValidate="txtUserName" Display="Static" ErrorMessage="*" runat="server" ID="vUserName" />
                        </div>
                    </div>
                </div>
            </div>
            <div class="row no-gutters justify-content-center mb-2">
                <div class="col-6 text-center">
                    <div class="row no-gutters">
                        <div class="col-4">
                            <asp:Label class="form-control border-0" runat="server">Parola:</asp:Label>
                        </div>
                        <div class="col-4">
                            <asp:TextBox class="form-control" runat="server" id="txtUserPass"></asp:TextBox>
                        </div>
                        <div class="col-4">
                            <asp:RequiredFieldValidator ControlToValidate="txtUserPass" Display="Static" ErrorMessage="*" runat="server" ID="vUserPass" />
                        </div>
                    </div>
                </div>
            </div>
            <div class="row no-gutters justify-content-center mb-2">
                <div class="col-6 text-center">
                    <div class="row no-gutters">
                        <div class="col-4">
                            <asp:Label class="form-control border-0" runat="server">Tine-ma minte!</asp:Label>
                        </div>
                        <div class="col-4">
                            <asp:CheckBox id="chkPersistCookie" runat="server" autopostback="false" />
                        </div>
                    </div>
                </div>
            </div>
            <div class="row no-gutters justify-content-center mb-2">
                <div class="col-6 text-center">
                    <div class="row no-gutters">
                        <div class="col-4"></div>
                        <div class="col-4">
                            <asp:Button OnClick="BTNLogin" runat="server" class="btn btn-md lfc-fossil" style="font-size:14px;width:200px" Text="Autentificare" />
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div id="Div2" runat="server">
            <div class="row no-gutters">
                <div class="col-5">
                    <div class="form-inline my-2 my-lg-0 ml-auto">
                        <asp:TextBox class="form-control mr-1" ID="inputMarca" runat="server" placeholder="Nr marca" style="font-size:14px;width:150px" />
                        <asp:Button ID="btnCheck" OnClick="Check_User" runat="server" class="btn btn-md lfc-fossil ml-1 mr-1" style="font-size:14px;width:150px" Text="Cauta" />
                        <asp:Button ID="btnAll" OnClick="Get_All" runat="server" class="btn btn-md lfc-fossil ml-1" style="font-size:14px;width:150px" Text="Arata tot" />
                    </div>
                </div>
                <div class="col-7 text-right">
                    <asp:Button ID="btnActualizare" OnClick="Compartiment" runat="server" class="btn btn-md lfc-fossil ml-1 hidden" style="font-size:14px;width:200px" Text="Actualizare compartiment" />
                </div>
            </div>
        <hr />
        </div>
        
        <asp:Button ID="btnTest" OnClick="Test" runat="server" class="btn btn-md lfc-fossil ml-1" style="font-size:14px;width:150px" Text="Test" />
        <asp:TextBox class="form-control mr-1 lfc-green hidden" ID="txtTest" runat="server" placeholder="Text Test" style="font-size:14px;width:150px" />

        <div id="DivUser" runat="server" class="hidden">
            <div class="row no-gutters">
                <div class="col-1">
                    <asp:Label Text="Marca" class="form-control lfc-green" runat="server" />
                </div>
                <div class="col-2">
                    <asp:Label Text="Nume" class="form-control lfc-green" runat="server" />
                </div>
                <div class="col-2">
                    <asp:Label Text="Prenume" class="form-control lfc-green" runat="server" />
                </div>
                <div class="col-2">
                    <asp:Label Text="Cod compartiment" class="form-control lfc-green" runat="server" />
                </div>
                <div class="col-3">
                    <asp:Label Text="Compartiment" class="form-control lfc-green" runat="server" />
                </div>
                <div class="col-2">
                    <asp:Label Text="Functie" class="form-control lfc-green" runat="server" />
                </div>
            </div>
            <div class="row no-gutters">
                <div class="col-1">
                    <asp:TextBox id="txtMarca" class="form-control bg-white" runat="server" ReadOnly="true" />
                </div>
                <div class="col-2">
                    <asp:TextBox id="txtNume" class="form-control bg-white" runat="server" ReadOnly="true" />
                </div>
                <div class="col-2">
                    <asp:TextBox id="txtPrenume" class="form-control bg-white" runat="server" ReadOnly="true" />
                </div>
                <div class="col-2">
                    <asp:TextBox id="txtCodCompartiment" class="form-control bg-white" runat="server" ReadOnly="true" />
                </div>
                <div class="col-3">
                    <asp:TextBox id="txtCompartiment" class="form-control bg-white" runat="server" ReadOnly="true" />
                </div>
                <div class="col-2">
                    <asp:TextBox id="txtFunctie" class="form-control bg-white" runat="server" ReadOnly="true" />
                </div>
            </div>
            <div class="row no-gutters mt-2">
                <asp:Button OnClick="Test_Redirect" runat="server" class="btn btn-md lfc-fossil ml-1" style="font-size:14px;width:150px" Text="Incepe testul" />
            </div>
        </div>

        <div class="row no-gutters">
            <asp:DataList ID="DataList1" class="hidden" runat="server" style="width:100%" OnItemCommand="DataList1_ItemCommand" DataKeyField="Marca">  
                <HeaderTemplate> 
                    <div class="row no-gutters">
                        <div class="col-1">
                            <asp:Label Text="Marca" class="form-control lfc-green" runat="server" />
                        </div>
                        <div class="col-2">
                            <asp:Label Text="Nume" class="form-control lfc-green" runat="server" />
                        </div>
                        <div class="col-2">
                            <asp:Label Text="Prenume" class="form-control lfc-green" runat="server" />
                        </div>
                        <div class="col-2">
                            <asp:Label Text="Cod compartiment" class="form-control lfc-green" runat="server" />
                        </div>
                        <div class="col-3">
                            <asp:Label Text="Compartiment" class="form-control lfc-green" runat="server" />
                        </div>
                        <div class="col-2">
                            <asp:Label Text="Functie" class="form-control lfc-green" runat="server" />
                        </div>
                    </div>
                </HeaderTemplate>
                    <ItemTemplate>
                        <div class="row no-gutters">
                            <div class="col-1">
                                <asp:Button ID="Button2" class="btn btn-link form-control border" runat="server" Text='<%#Eval("Marca")%>' CommandName="click" CommandArgument='<%#Eval("Marca")%>'/><br />
                                <asp:TextBox ID="txtMarca" CssClass="hidden" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"Marca")%>' />
                            </div>
                            <div class="col-2">
                                <asp:TextBox ID="txtNume" class="form-control" runat="server" Text='<%#Eval("Nume")%>' /> 
                            </div>
                            <div class="col-2">
                                <asp:TextBox ID="txtPrenume" class="form-control" runat="server" Text='<%#Eval("Prenume")%>' />
                            </div>
                            <div class="col-2">
                                <asp:TextBox ID="txtCodCompartiment" class="form-control text-uppercase" runat="server" Text='<%#Eval("CodCompartiment")%>' />
                            </div>
                            <div class="col-3">
                                <asp:TextBox ID="txtCompartiment" class="form-control" runat="server" Text='<%#Eval("Compartiment")%>' />
                            </div>
                            <div class="col-2">
                                <asp:TextBox ID="txtFunctie" class="form-control" runat="server" Text='<%#Eval("Functie")%>' /> 
                            </div>
                        </div>
                </ItemTemplate>  
            </asp:DataList>
        </div>

        <br /><br /><br />
    </div>
</asp:Content>

code behind

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Instruire
{
    public partial class Home : Page
    {
        private readonly string connString = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Marca"] != null)
            {
                inputMarca.Text = Session["Marca"].ToString();
                DivUser.Attributes["class"] = DivUser.Attributes["class"].Replace("hidden", "").Trim();
                GetUserData();
                DataList1.Attributes.Add("class", "hidden");
            }
            else
            {
                inputMarca.Text = string.Empty;
                DivUser.Attributes.Add("class", "hidden");
            }

            if (HttpContext.Current.User != null && HttpContext.Current.User.Identity.IsAuthenticated)
            {
                DivLogin.Attributes.Add("class", "hidden");
                btnActualizare.Attributes["class"] = btnActualizare.Attributes["class"].Replace("hidden", "").Trim();
            }
            if (Session["SMPSI"] != null)
            {
                DivLogin.Attributes["class"] = DivLogin.Attributes["class"].Replace("hidden", "").Trim();
                DivUser.Attributes.Add("class", "hidden");
            }
        }

        protected void Check_User(object sender, EventArgs e)
        {
            GetUserData();
        }
        protected void Test_Redirect(object sender, EventArgs e)
        {
            Response.Redirect("/UserPages/Testare.aspx");
        }
        protected void Compartiment(object sender, EventArgs e)
        {

        }
        protected void GetUserData()
        {
            using (SqlConnection conn = new SqlConnection(connString))
            {
                string sqlQuery = "SELECT * FROM tblUser WHERE Marca=@Marca";
                using (SqlCommand cmd = new SqlCommand(sqlQuery, conn))
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.AddWithValue("@Marca", inputMarca.Text);

                    conn.Open();
                    using (SqlDataReader rdr = cmd.ExecuteReader())
                    {
                        if (rdr.HasRows)
                        {
                            while (rdr.Read())
                            {
                                txtMarca.Text = rdr["Marca"].ToString();
                                txtNume.Text = rdr["Nume"].ToString();
                                txtPrenume.Text = rdr["Prenume"].ToString();
                                txtCodCompartiment.Text = rdr["CodCompartiment"].ToString();
                                txtCompartiment.Text = rdr["Compartiment"].ToString();
                                txtFunctie.Text = rdr["Functie"].ToString();
                            }
                            int id = Convert.ToInt32((inputMarca.Text).ToString());

                            Session["Marca"] = id;

                        }
                        else
                        {
                            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Nu este niciun user cu marca asta')", true);
                            return;
                        }
                    }
                    conn.Close();
                }
            }
        }

        protected void Get_All(object sender, EventArgs e)
        {
            GetAll();
        }
        private void GetAll()
        {
            using (SqlConnection conn = new SqlConnection(connString))
            {
                string sqlQuery = "SELECT * FROM tblUser";
                using (SqlCommand cmd = new SqlCommand(sqlQuery, conn))
                {
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    conn.Open();
                    DataSet ds = new DataSet();
                    da.Fill(ds);
                    DataList1.DataSource = ds.Tables[0];
                    DataList1.DataBind();
                }
            }
            Session.Remove("Marca");
            DivUser.Attributes.Add("class", "hidden");
            DataList1.Attributes["class"] = DataList1.Attributes["class"].Replace("hidden", "").Trim();
        }
        protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName == "click")
            {
                int id = Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex].ToString());

                string marca = id.ToString();
                Session["Marca"] = marca;

                Response.Redirect("/Home.aspx");
            }
        }
        private bool ValidateUser(string userName, string passWord)
        {
            string lookupPassword = null;

            if ((null == userName) || (0 == userName.Length) || (userName.Length > 15))
            {
                System.Diagnostics.Trace.WriteLine("[ValidateUser] Input validation of userName failed.");
                return false;
            }

            if ((null == passWord) || (0 == passWord.Length) || (passWord.Length > 25))
            {
                System.Diagnostics.Trace.WriteLine("[ValidateUser] Input validation of passWord failed.");
                return false;
            }

            try
            {
                using (SqlConnection conn = new SqlConnection(connString))
                {
                    conn.Open();
                    string sqlQuery = "SELECT password FROM tblAdmin WHERE username=@userName";

                    using (SqlCommand cmd = new SqlCommand(sqlQuery, conn))
                    {
                        cmd.Parameters.Add("@userName", SqlDbType.VarChar, 25);
                        cmd.Parameters["@userName"].Value = userName;

                        lookupPassword = (string)cmd.ExecuteScalar();

                        cmd.Dispose();
                    }
                    conn.Dispose();
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine("[ValidateUser] Exception " + ex.Message);
            }

            if (null == lookupPassword)
            {
                return false;
            }

            return (0 == string.Compare(lookupPassword, passWord, false));
        }
        protected void BTNLogin(object sender, EventArgs e)
        {
            if (ValidateUser(txtUserName.Text, txtUserPass.Text))
                FormsAuthentication.SetAuthCookie(txtUserName.Text, chkPersistCookie.Checked);
                Response.Redirect("Home.aspx");
        }
        protected void Test(object sender, EventArgs e)
        {
            txtTest.Attributes["class"] = txtTest.Attributes["class"].Replace("hidden", "");
        }
    }
}

 


Answers (2)