简体   繁体   English

更改数据列表中按钮上的文本不起作用

[英]Change text on button in datalist not working

I have the following code and it works to change the URL, but it does not change the text on the button. 我有以下代码,它可以更改URL,但不会更改按钮上的文本。 What am I missing? 我想念什么? The button is in a datalist and I want to change the Text on the button when pressed. 该按钮在数据列表中,我想在按下按钮时更改按钮上的“文本”。 What is supposed to happen is the user hits the Quiz button and the URL should change to the quizURL(this works). 应该发生的情况是用户单击“测验”按钮,URL应该更改为quizURL(这可行)。 At the same time the text on the button should change to say Video(this doesn't work). 同时,按钮上的文本应更改为Video(这不起作用)。 Then when the user hits the same button it will toggle back to the quiz. 然后,当用户单击相同的按钮时,它将切换回测验。 I'm trying to have one button act as a toggle to show the quiz or the video. 我正在尝试让一个按钮充当切换以显示测验或视频。

<asp:Button ID="btnQuizVid" runat="server" Text="Quiz" CommandName="quiz" CssClass="buttonStyleGrey" />


protected void trainingDataList_ItemCommand(object source, DataListCommandEventArgs e)
{
    List<Material> dataset = null;
    string btnText = ((Button)(trainingDataList.Items[e.Item.ItemIndex].FindControl("btnQuizVid"))).Text;

    if (e.CommandName == "quiz")
    {
        dataset = BasicCRUDtoolkit.GetMaterialByProfFocus(hdnTypeSelect.Value);
        Button tmpBtn = e.Item.FindControl("btnQuizVid") as Button;

        if (btnText == "Quiz")
        {
            tmpBtn.Text = "Video";
            tmpBtn.DataBind();
            dataset[e.Item.ItemIndex].videoURL = dataset[e.Item.ItemIndex].quizURL;

        }
        else
        {
            tmpBtn.Text = "Quiz";
            dataset[e.Item.ItemIndex].videoURL = dataset[e.Item.ItemIndex].videoURL;

        }

        trainingDataList.DataSource = dataset;
        trainingDataList.DataBind();
    }

}

You are missing the OnCommand value in your button markup, thus the CommandName value you are setting in the markup does not know what to call when fired. 您在按钮标记中缺少OnCommand值,因此在标记中设置的CommandName值不知道触发时要调用的内容。 Add the OnCommand value to your button markup, like this: OnCommand值添加到您的按钮标记中,如下所示:

<asp:Button ID="btnQuizVid" runat="server" Text="Quiz" CommandName="quiz" 
            CssClass="buttonStyleGrey" 
            OnCommand="trainingDataList_ItemCommand" />

Get the button control once and do not call .DataBind() on the button as the change will be reflected just by changing the Text property, because the code is executing before the HTML is sent to the browser. 只需获取一次按钮控件,并且不要在按钮上调用.DataBind() ,因为更改将仅通过更改Text属性反映出来,因为代码是在HTML发送到浏览器之前执行的。 Change your code to this: 将代码更改为此:

protected void trainingDataList_ItemCommand(object source, 
                                            DataListCommandEventArgs e)
{
    List<Material> dataset = null;
    var theButton = trainingDataList.Items[e.Item.ItemIndex]
                                    .FindControl("btnQuizVid") as Button;

    if (e.CommandName == "quiz")
    {
        dataset = BasicCRUDtoolkit.GetMaterialByProfFocus(hdnTypeSelect.Value);

        if (theButton.Text == "Quiz")
        {
            theButton.Text = "Video";
            dataset[e.Item.ItemIndex].videoURL = dataset[e.Item.ItemIndex].quizURL;
        }
        else
        {
            theButton.Text = "Quiz";
            dataset[e.Item.ItemIndex].videoURL = dataset[e.Item.ItemIndex].videoURL;
        }

        trainingDataList.DataSource = dataset;
        trainingDataList.DataBind();
    }
}

I guess you need to do Button tmpBtn = e.Item.FindControl("btnQuizVid") as Button; 我猜您需要将Button tmpBtn = e.Item.FindControl("btnQuizVid") as Button; and theButton.Text = "Video"; theButton.Text = "Video"; after DataBind() ; DataBind()

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

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