Home > DeveloperSection > Forums > Change text on button in datalist not working
Ankit Singh

Total Post:341

Points:2389
Posted on    December-18-2013 12:35 AM

 C# C# 
Ratings:


 1 Reply(s)
 900  View(s)
Rate this:
I have the following code and it works to change the URL, but it does not change the text on the button. What am I missing?

<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();

    }

}

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). At the same time the text on the button should change to say Video(this doesn't work). 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. 


ezra heywood
ezra heywood

Total Post:145

Points:1019
Posted on    December-18-2013 1:17 AM

Hi Ankit,

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. Add the OnCommand value to your button markup, like this:

<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. 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();

    }

}


Don't want to miss updates? Please click the below button!

Follow MindStick