I am trying to make a label disappear in another form on a button click but I cant access this label in the form i am writing the code in

Viewed 37

the form I am writing the code name is (Login) and the other form that contains the label that I want it to disappear in (WebForm1) and the label name is (LinkLabel1). here is my code


using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows.Forms;
using System.Text;
using System.Drawing;


namespace API1
{
    public partial class Login1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection sqlcon = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=""D:\VS projects\API1\database.mdf"";Integrated Security=True;Connect Timeout=30");
            string query = "Select * from users Where Username  = '" + TextBox2.Text.Trim() + "' and Password = '" + TextBox3.Text.Trim() + "'";
            SqlDataAdapter sda = new SqlDataAdapter(query, sqlcon);
            DataTable dtbl = new DataTable();
            sda.Fill(dtbl);

            if (dtbl.Rows.Count == 1)
            {
                Session["Logged"] = "1";
                SqlConnection role = new SqlConnection("\"Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=\"\"D:\\VS projects\\API1\\database.mdf\"\";Integrated Security=True;Connect Timeout=30\"");
                SqlCommand getrole = new SqlCommand("select * from users where username = '" + TextBox2.Text + "' and password= '" + TextBox3.Text + "'", role);
                SqlDataAdapter da = new SqlDataAdapter(getrole);
                DataTable dtb = new DataTable();
                sda.Fill(dtb);

                if (dtb.Rows.Count > 0)
                {
                    for (int i = 0; i < dtb.Rows.Count; i++)
                    {
                        if (dtb.Rows[i]["Role"].ToString() == "Admin")
                        {

                            WebForm1.LinkButton1.Visible = false;
                            Response.Redirect("WebForm1.aspx");

                        }
                    }
                }
                Response.Redirect("WebForm1.aspx");

            }
            else
            {
                Session["Logged"] = "0";
                MessageBox.Show("Check your username and password and try again");
            }


        }


    }
}
}

I get error "inaccessible due to its protection level" under linkbutton1 in WebForm1.LinkButton1.Visible = false;

and here is the HTML code


<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .newStyle1 {
            position: fixed;
        }
        .newStyle2 {
            position: fixed;
        }
        .auto-style2 {
            position: fixed;
            left: 133px;
            top: 71px;
        }
        .newStyle3 {
            position: fixed;
        }
        .auto-style3 {
            position: fixed;
            left: 131px;
            top: 129px;
        }
        .newStyle4 {
            position: fixed;
        }
        .auto-style4 {
            position: fixed;
            left: 10px;
            top: 190px;
        }
        .newStyle5 {
            position: fixed;
        }
        .auto-style5 {
            position: fixed;
            left: 10px;
            top: 75px;
            font-size: large;
            right: 1327px;
        }
        .newStyle6 {
            position: fixed;
        }
        .auto-style6 {
            position: fixed;
            left: 10px;
            top: 131px;
            font-size: large;
            right: 1332px;
        }
        .auto-style7 {
            font-size: x-large;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div class="auto-style7">
            <strong>Login</strong></div>
        <asp:Button ID="Button1" runat="server" CssClass="auto-style4" OnClick="Button1_Click" Text="Sign in" />
        <strong>
        <asp:Label ID="Label1" runat="server" CssClass="auto-style5" Text="Username"></asp:Label>
        <asp:Label ID="Label2" runat="server" CssClass="auto-style6" Text="Password"></asp:Label>
        </strong>
        <asp:TextBox ID="TextBox2" runat="server" CssClass="auto-style2"></asp:TextBox>
        <asp:TextBox ID="TextBox3" runat="server" CssClass="auto-style3"></asp:TextBox>
    </form>
</body>
</html>
1 Answers

You've not provided a great deal to go on here, but from your text I have assumed you are using WebForms. If that's the case then in your aspx file you'll need to add runat="server" as an attribute of the LinkLabel and give it an id` too.

For example ...

What I guess your linklabel looks like now.

<asp:LinkLabel />

What it should look like in order to be referenced in code.

<asp:LinkLabel ID="myLinkLabel" runat="server" />

Then in your back-end code you can do ...

this.myLinkLabel.Visible = false;
Related