在aspx网页和类之间传递值 [英] Pass Values Between aspx webpage and class
问题描述
我想知道如何从另一个CS文件中的某个aspx页面调用Label.例如:
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);
}
我在Something.aspx.cs文件中有这段代码,可以从我创建的另一个CS文件中调用一个方法,如下所示:
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();
}
}
}
}
但这不起作用.我之前可以通过会话完成此操作,但这无法正常工作.我无法调用在Something.aspx页中使用的LabelAdd.关于如何解决此问题的任何想法?谢谢.
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)
还请在您的课程中使用System.Web.UI.WebControls; 添加此
also please add this using System.Web.UI.WebControls;
in your class
这篇关于在aspx网页和类之间传递值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!