C# ASP.NETC-将变量连接到访问控件
我的原始函数如下,但是,我想修改它,以便我可以传入一个数字来更新必要的控件,例如getQuestionDetails3将更新lblQuestion3.Text和lblQuestion3Type.Text 然而,我的尝试似乎只在我按该顺序执行getQuestionDetails1、getQuestionDetails2、getQuestionDetails3时起作用。如果我尝试先执行getQuestionDetails3,则什么也不会发生。你知道为什么吗 原始代码:C# ASP.NETC-将变量连接到访问控件,c#,asp.net,variables,web-controls,C#,Asp.net,Variables,Web Controls,我的原始函数如下,但是,我想修改它,以便我可以传入一个数字来更新必要的控件,例如getQuestionDetails3将更新lblQuestion3.Text和lblQuestion3Type.Text 然而,我的尝试似乎只在我按该顺序执行getQuestionDetails1、getQuestionDetails2、getQuestionDetails3时起作用。如果我尝试先执行getQuestionDetails3,则什么也不会发生。你知道为什么吗 原始代码: protected void
protected void getQuestionDetails()
{
string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
MySqlConnection conn = new MySqlConnection(connStr);
MySqlDataReader reader;
string cmdText = "SELECT * FROM questions WHERE question_id=@QuestionID";
MySqlCommand cmd = new MySqlCommand(cmdText, conn);
cmd.Parameters.Add("@QuestionID", MySqlDbType.Int32);
cmd.Parameters["@QuestionID"].Value = Convert.ToInt32(ddlQuestion1IDs.SelectedValue);
try
{
conn.Open();
reader = cmd.ExecuteReader();
if (reader.Read())
{
lblQuestion1.Text = reader["question"].ToString();
if (reader["type"].ToString().Equals("T"))
{
lblQuestion1Type.Text = "simple question - one word answer";
}
else if (reader["type"].ToString().Equals("R"))
{
lblQuestion1Type.Text = "multiple choice - one correct answer";
}
else if (reader["type"].ToString().Equals("C"))
{
lblQuestion1Type.Text = "multiple choice - more than one correct answer";
}
}
reader.Close();
}
catch
{
lblError.Text = "Database connection error - failed to get question details.";
}
finally
{
conn.Close();
}
}
我的尝试:
protected void getQuestionDetails(string a)
{
string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
MySqlConnection conn = new MySqlConnection(connStr);
MySqlDataReader reader;
string cmdText = "SELECT * FROM questions WHERE question_id=@QuestionID";
MySqlCommand cmd = new MySqlCommand(cmdText, conn);
cmd.Parameters.Add("@QuestionID", MySqlDbType.Int32);
cmd.Parameters["@QuestionID"].Value = Convert.ToInt32(((DropDownList)this.FindControl("ddlQuestion" + a + "IDs")).SelectedValue);
try
{
conn.Open();
reader = cmd.ExecuteReader();
if (reader.Read())
{
((Label)this.FindControl("lblQuestion" + a)).Text = reader["question"].ToString();
if (reader["type"].ToString().Equals("T"))
{
((Label)this.FindControl("lblQuestion" + a + "Type")).Text = "simple question - one word answer";
}
else if (reader["type"].ToString().Equals("R"))
{
((Label)this.FindControl("lblQuestion" + a + "Type")).Text = "multiple choice - one correct answer";
}
else if (reader["type"].ToString().Equals("C"))
{
((Label)this.FindControl("lblQuestion" + a + "Type")).Text = "multiple choice - more than one correct answer";
}
}
reader.Close();
}
catch
{
lblError.Text = "Database connection error - failed to get question details.";
}
finally
{
conn.Close();
}
}
你为什么不把你所有的问题都写出来。。在单个数组中?为什么不作为字符串传递?而不是打电话到处乱跑?你有没有仔细检查代码,看看发生了什么?您是否有控制DDLQuestion1ID、DDLQuestion2ID和DDLQuestion3IDetc@christiandev我已经修改了代码。我愿意。我已经更新了原来的帖子来进一步描述这个问题。getQuestionDetails3仅在运行getQuestionDetails1和getQuestionDetails2后工作。是否每次调用getQuestionDetails方法时都更改下拉列表?旁注:您应该在C中使用CamelCase,因此GetQuestionDetails更为相关:不直接解决您的问题,但您可以使用字典链接reader[type]可能具有的值,这样您就不需要使用3个ifs。另外,使用string.Format生成控件名,因为它更具可读性。请不要使用单字母变量名,这只会使代码更难阅读。另外,从问题中选择*会让我畏缩,请不要这样做。只选择需要的列,因为这样可以加快代码的速度。