简体   繁体   English

更改对象发送者控件的CSS类

[英]changing CSS class for object sender control

i'm making a workbook creator in C#.net ( using visual studio ) 我正在C#.net(使用Visual Studio)中创建工作簿创建者

the book is build from the text part and the question part. 本书是由文本部分和问题部分组成的。

all the answers for the question are inside the text and the user need to click on the right answer. 问题的所有答案都在文本内,用户需要单击正确的答案。 if he's right then the word become green and if he's wrong it become red. 如果他是对的,那么这个词就会变成绿色,如果他错了,那么它就会变成红色。

i'm creating the clickeable text with LINKBUTTON, i gave the link button CssStyle class and after the user clicking the word i want to change the class for this link to a different class. 我正在使用LINKBUTTON创建clickeable文本,给了链接按钮CssStyle类,并且在用户单击该单词后,我想将此链接的类更改为其他类。

this is the code i using for creating the linksbutton: 这是我用来创建链接按钮的代码:

  public void createQusetion(Panel lefttext, Panel question, string text, string          
   questionText, string answer)
{

    string[] Qbuttonstext = text.Split(' ');
    _numberWords = Qbuttonstext.Length;
    for (int i = 0; i < _numberWords; i++)
    {
        LinkButton answerButton = new LinkButton();
        if (Qbuttonstext[i] == answer)
        {
            answerButton.ID = "answer" + i;

        }
        else
        {
            answerButton.ID = "word" + i.ToString();
        }
        answerButton.Text = Qbuttonstext[i].ToString() + " ";
        answerButton.CssClass = "textbuttonB4";

        answerButton.Click += new EventHandler(checkAnswer);

        lefttext.Controls.Add(answerButton);
    }


}

and for the checking the question: 并检查问题:

 private void checkAnswer(object sender, System.EventArgs e)
{
    for (int i = 0; i < _numberWords; i++)
    {
        if (((Control)sender).ID.ToString() != null)
        {
            if (((Control)sender).ID.ToString() == "answer" + i.ToString())
            {
                ((Control)sender).CssClass = "textbuttonRight";

            }
            else
            {
                ((Control)sender).CssClass = "textbuttonwrong";

            }
        }
    }
}

the VS2010 giving me misatake for the : ((Control)sender).CssClass . VS2010让我误会了:((Control)sender).CssClass。

what is the right way? 正确的方法是什么?

You can do a type-independent control this way. 您可以通过这种方式进行类型无关的控件。 It will run for all controls have Id and CssClass Properties. 它将为所有具有Id和CssClass属性的控件运行。

    private void checkAnswer(object sender, System.EventArgs e)
    {
        var cssClass = sender.GetType().GetProperty("CssClass");
        var id = sender.GetType().GetProperty("ID").GetValue(sender, null);
        for (int i = 0; i < _numberWords; i++)
        {
            if (id!=null)
            {
                if (id.ToString() == "answer" + i.ToString())
                {
                    cssClass.SetValue(sender, "textbuttonRight", null);
                }
                else
                {
                    cssClass.SetValue(sender, "textbuttonRight", null);
                }
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM