简体   繁体   中英

Pass Values Between aspx webpage and class

I am wondering how to call a Label from a certain aspx page in another cs file. For example:

protected void Button1_Click(object sender, EventArgs e)
    {
        Test1.Insert1(this);
    }

This code i have in the Something.aspx.cs file to call a method from another cs file i created which looks like:

public static class Test1 
{
    public static void Insert1(System.Web.UI.Page Ins)

     {
        string conn = "Data Source=pc-...";
        System.Data.SqlClient.SqlConnection sqlConn = new System.Data.SqlClient.SqlConnection(conn);
        if (Page.IsValid)
        {
            try
            {
                sqlConn.Open();
                Ins.LabelAdd.Text = "Worked!";
                System.Data.SqlClient.SqlCommand myCommand = new System.Data.SqlClient.SqlCommand("INSERT INTO ... ", sqlConn);
                myCommand.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Ins.LabelAdd.Text = "Error! " + ex;
            }
            finally
            {
                sqlConn.Close();
            }
        }
    }
}

But this doesn't work. I was able to do this before with a session but this won't work. I can't call the LabelAdd which I used in the Something.aspx page. Any ideas on how I can fix this? Thank you.

You need to pass your control Reference like...

public static void Insert1(Label lblId)

also please add this using System.Web.UI.WebControls; in your class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM