<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.
Anonymous User
18-Dec-2013You 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:
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: