Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#如何在静态方法中设置文本框/标签值?_C# - Fatal编程技术网

C#如何在静态方法中设置文本框/标签值?

C#如何在静态方法中设置文本框/标签值?,c#,C#,我试过: System.Web.UI.Page FormPage; object FormControl = FormPage.FindControl("lblUserNames"); object literalControl = FormPage.FindControl("litMessages"); 但它抛出了一个错误:“使用未分配的局部变量‘FormPage’” 那么如何在静态方法中设置textbox/label值呢 全部资料来源: [W

我试过:

System.Web.UI.Page FormPage;
            object FormControl = FormPage.FindControl("lblUserNames");
            object literalControl = FormPage.FindControl("litMessages");
但它抛出了一个错误:“使用未分配的局部变量‘FormPage’”

那么如何在静态方法中设置textbox/label值呢

全部资料来源:

[WebMethod]                                 
        public static void DeleteItem()
        {          
            HttpCookie reader = HttpContext.Current.Request.Cookies["roomId"];

            string query = "[Get_Messages]";
            SqlCommand cmd = new SqlCommand(query);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@roomId", reader.Value);

            GetData(cmd);
        }
将数据从数据库获取到dataset并将其绑定到div

private static DataSet GetData(SqlCommand cmd)
        {

            int cntr = 0;

            HttpCookie cookieUserName = HttpContext.Current.Request.Cookies["userName"];
            string username = cookieUserName.Value;


            System.Web.UI.Page FormPages;
            object FormsControl = FormPages.FindControl("lblUserNames");


            string strConnString = ConfigurationManager.ConnectionStrings["LinqChatConnectionString"].ConnectionString;
            using (SqlConnection con = new SqlConnection(strConnString))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.Connection = con;
                    sda.SelectCommand = cmd;
                    using (DataSet ds = new DataSet())
                    {
                        sda.Fill(ds, "Messages");
                        return ds;

                        if (ds.Tables[0].Rows.Count != 0)
                        {
                            StringBuilder sb = new StringBuilder();

                            int i = (ds.Tables[0].Rows.Count - 1);

                            for (int j = 0; j <= i; j++)
                            {
                                string personName = ds.Tables[0].Rows[j]["Username"] == null ? "" : ds.Tables[0].Rows[j]["Username"].ToString();
                                string gender = ds.Tables[0].Rows[j]["Sex"] == null ? "" : ds.Tables[0].Rows[j]["Sex"].ToString();
                                string message = ds.Tables[0].Rows[j]["Text"] == null ? "" : ds.Tables[0].Rows[j]["Text"].ToString();

                                if (cntr == 0)
                                {
                                    if (username == personName)
                                    {
                                        sb.Append("<div style='padding: 10px;text-align:right'>");
                                    }
                                    else
                                    {
                                        sb.Append("<div style='padding: 10px;'>");
                                    }
                                    cntr = 1;
                                }
                                else
                                {
                                    if (username == personName)
                                    {
                                        sb.Append("<div style='background-color: #EFEFEF;padding: 10px;text-align:right'>");
                                    }
                                    else
                                    {
                                        sb.Append("<div style='background-color: #EFEFEF;padding: 10px;'>");
                                    }
                                    cntr = 0;
                                }

                                ((System.Web.UI.WebControls.Label)FormControl).Text = "<span style='color: Blue;'><b line-height:22px>" + personName + "</b></span>";
                                string lblUserNames = ((System.Web.UI.WebControls.Label)FormControl).Text;
                                //lblUserNames.Text
                                ((System.Web.UI.WebControls.Label)FormControl).Visible = true;
                                //lblUserNames.Visible = true;

                                if (gender.ToLower() == "m")
                                {
                                    if (username == personName)
                                    {
                                        sb.Append(message + "</div>");
                                    }
                                    else
                                    {
                                        sb.Append("<img src='Images/manIcon.gif' style='vertical-align:middle;' alt=''>  " + lblUserNames + " " + message + "</div>");
                                    }
                                }

                                else
                                {
                                    if (username == personName)
                                    {
                                        sb.Append(message + "</div>");
                                    }
                                    else
                                    {
                                        sb.Append("<img src='Images/womanIcon.gif' style='vertical-align:middle' alt=''>  " + lblUserNames + " " + message + "</div>");
                                    }
                                }
                            }

                            ((System.Web.UI.WebControls.Literal)FormsControl).Text = sb.ToString();
                            ((System.Web.UI.WebControls.Label)FormControl).Visible = false;
                        }
                        else
                        {
                            ((System.Web.UI.WebControls.Label)FormControl).Visible = false;
                        }
                    }
                }
            }
        }
private静态数据集GetData(SqlCommand cmd)
{
int-cntr=0;
HttpCookie cookieUserName=HttpContext.Current.Request.Cookie[“用户名”];
字符串username=cookieUserName.Value;
System.Web.UI.Page表单页面;
对象FormsControl=FormPages.FindControl(“lblUserNames”);
string strConnString=ConfigurationManager.ConnectionString[“LinqChatConnectionString”]。ConnectionString;
使用(SqlConnection con=newsqlconnection(strConnString))
{
使用(SqlDataAdapter sda=newsqldataadapter())
{
cmd.Connection=con;
sda.SelectCommand=cmd;
使用(数据集ds=新数据集())
{
sda.填写(ds,“信息”);
返回ds;
if(ds.Tables[0].Rows.Count!=0)
{
StringBuilder sb=新的StringBuilder();
int i=(ds.Tables[0].Rows.Count-1);

对于(int j=0;j您可以尝试设置它吗

FormPage page = new FormPage();

object FormControl = page.FindControl("lblUserNames");
object literalControl = page.FindControl("litMessages");

您可以传递页面…或者不使用静态方法。传递页面意味着什么?如何传递?您的代码是什么样子的?FormPage()方法在哪里?您可以创建它。
FormPage Page=new FormPage();
@Armanahmed
FormPage()
不是方法。它是FormPage类的构造函数。我建议您首先学习c语言的基础知识。^^^或者那样。谢谢@M.kazemakhgary在这个类中我要声明什么?