我试过了:
System.Web.UI.Page FormPage;
object FormControl = FormPage.FindControl("lblUserNames");
object literalControl = FormPage.FindControl("litMessages");
但它引发了一个错误:"使用未分配的本地变量' FormPage'"
那么如何在静态方法中设置文本框/标签值?
整个来源:
[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);
}
从数据库获取数据到数据集并将其绑定到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;
}
}
}
}
}
答案 0 :(得分:0)
您可以尝试设置它吗?
FormPage page = new FormPage();
object FormControl = page.FindControl("lblUserNames");
object literalControl = page.FindControl("litMessages");