【问题标题】:How to randomly add data from a database to a button's text attribute如何将数据库中的数据随机添加到按钮的文本属性中
【发布时间】:2014-01-18 00:19:35
【问题描述】:

我正在做一个测验项目。我正在使用 4 个带有 CSS 样式的 asp 按钮,并且我使用这 4 个按钮作为答案,在按钮的文本字段中有正确或错误的答案。我已经阅读并找到了另一篇与此非常相似的帖子,但答案是使用字典列表。我有点确定我不需要使用列表,我应该能够随机分配 text 属性。这是我用来将答案添加到按钮文本属性的代码,但我需要随机化答案的位置,因为始终在同一个按钮中获得正确答案是不正确的。

lblQuestion.Text = ds.Tables[0].Rows[myNum]["Question"].ToString();
btn1.Text = ds.Tables[0].Rows[myNum]["CorrectAnswer"].ToString();
btn2.Text = ds.Tables[0].Rows[myNum]["WrongAnswer1"].ToString();
btn3.Text = ds.Tables[0].Rows[myNum]["WrongAnswer2"].ToString();
btn4.Text = ds.Tables[0].Rows[myNum]["WrongAnswer3"].ToString(); 

每个按钮都有一个 onlick 方法来切换到下一个问题,并将按钮文本更改为下一个答案。此代码在每个 buttonclick 事件中...

protected void btn1_Click(object sender, EventArgs e)
{
    lblQuestion.Text = ds.Tables[0].Rows[myNum]["Question"].ToString();
    btn1.Text = ds.Tables[0].Rows[myNum]["CorrectAnswer"].ToString();
    btn2.Text = ds.Tables[0].Rows[myNum]["WrongAnswer1"].ToString();
    btn3.Text = ds.Tables[0].Rows[myNum]["WrongAnswer2"].ToString();
    btn4.Text = ds.Tables[0].Rows[myNum]["WrongAnswer3"].ToString();

    myNum = myNum + 1;
 }

我不确定,但也许我需要能够使用一种方法来随机化所有按钮文本而不是编码到每个 buttonclick 事件中?

谢谢

【问题讨论】:

  • 在这里您需要为所有 4 个按钮编写相同的代码...然后只有这样您才能做到这一点...但不要使用按钮,而是使用 4 个单选按钮和一个提交按钮..我认为这对你来说很容易......
  • The reason why I chose a button is because its text is already centered and when the answer is selected it automatically moves onto the next question and set of answers, So I couldn't use a submit button.我可以用 4 个单选按钮做同样的事情,但是在两者之间,无论我是设置单选按钮还是按钮的文本,两者之间应该没有太大区别。要么会做我需要的,但它能够随机分配正确答案和错误答案给文本属性的问题。除非我在您的评论中遗漏了什么??
  • 是的,您也可以通过无线电检查和按钮来完成,但至少在这里您应该有机会错误地进行测验,如果他/她按错了按钮,那么会发生什么? ?你有没有想过???正因为如此,我告诉使用带有单个按钮的单选按钮。那将是个好主意....至少再给一次测验的机会来思考他/她的答案..
  • 是的,我已经考虑过了,无论他们选择正确答案还是错误答案,测验都会一直移动到最后一个问题,然后它会显示结果。
  • 这就是我的意思,伙计....这对理想的测验不利....至少他们应该得到至少一个改变来考虑他们的答案...

标签: c# asp.net linq webforms


【解决方案1】:

我编写了一个简单的子程序来演示如何随机排列字符串,然后将它们附加到按钮上。此示例仅演示如何在屏幕上的 4 个可能位置中随机分配正确答案的位置。

    static void Main()
    {

        // create array of 4 string (or answers in your case)
        //string[] array = new string[4] { "apple", "banana", "cranberry", "dragon fruit" };            
        string[] array = new string[4] {
            ds.Tables[0].Rows[myNum]["CorrectAnswer"].ToString(),
            ds.Tables[0].Rows[myNum]["WrongAnswer1"].ToString(),
            ds.Tables[0].Rows[myNum]["WrongAnswer2"].ToString(),
            ds.Tables[0].Rows[myNum]["WrongAnswer3"].ToString(),
        };

        // randomize the ordering of the items
        System.Random rnd = new System.Random();
        array = array.OrderBy(x => rnd.Next()).ToArray();

        // each time you run this, the correct answer will be in a different place:
        btn1.Text = array[0];
        btn2.Text = array[1]; 
        btn3.Text = array[2];
        btn4.Text = array[3];

    }

奖励答案,您也可以像这样将多个按钮连接到一个 Event Handler。进入处理程序后,您可以访问引发事件并执行操作的Button

protected void Page_Load(object sender, EventArgs e)
{
    // attach event handlers
    Button1.Click += ButtonClick;
    Button2.Click += ButtonClick;
    Button3.Click += ButtonClick;
    Button4.Click += ButtonClick;
}

private void ButtonClick(object sender, EventArgs e)
{
    // your code here...

    // this is the button that raised the event
    Button button = (Button)sender;    
    // check its ID? you could also check its text, perform any actions you wish, etc.    
    if (button.ID == "Button1")
    {
        System.Diagnostics.Debug.Print("Button 1 Clicked!");
    }
}

【讨论】:

  • 我明白你在说什么,只是看看你是怎么做的,看起来是正确的。现在有了数组,如何添加数据集?像这样.. ds.Tables[0].Rows[myNum]["CorrectAnswer"].ToString();等等...?
  • 编辑了示例以使用您的特定数据源。您可以从数据表、数据库等以任何您认为合适的方式获取和添加字符串。
  • 完美!这对我有很大帮助。
  • 我添加了额外的代码来演示如何将多个按钮连接到单个处理程序以减少代码。
  • 当您将 Button onclick 事件处理程序附加到新事件 ButtonClick 时,ButtonClick 现在是否会替换所有其他按钮 OnClick 事件处理程序?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-19
相关资源
最近更新 更多