---
title: "Change text on button in datalist not working"  
description: "Change text on button in datalist not working"  
author: "Anonymous User"  
published: 2013-12-18  
updated: 2013-12-18  
canonical: https://www.mindstick.com/forum/1797/change-text-on-button-in-datalist-not-working  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# Change text on button in datalist not working

I have the following [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) and it works to change the [URL](https://www.mindstick.com/forum/436/url-redirection), but it does not change the [text](https://www.mindstick.com/blog/301635/did-people-reinvent-texting-to-express-the-full-range-of-emotions) on the [button](https://www.mindstick.com/articles/63/how-to-add-button-in-datagridview-in-csharp-dot-net). What am I [missing](https://answers.mindstick.com/qa/52138/international-missing-children-s-day-2019-was-observed-on)?

```
<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](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) 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](https://answers.mindstick.com/qa/116645/a1-wreckers-reviews-what-do-customers-say) [Video](https://www.mindstick.com/articles/290133/6-necessary-preparations-you-need-before-editing-a-video)(this doesn't work). Then when the user hits the same button it will [toggle](https://www.mindstick.com/forum/12682/jquery-toggle-if-statement) back to the quiz. I'm [trying](https://answers.mindstick.com/qa/93698/6-mistakes-couples-are-trying-to-save-money) to have one button act as a toggle to show the quiz or the video.

## Replies

### Reply by Anonymous User

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();    }}
```


---

Original Source: https://www.mindstick.com/forum/1797/change-text-on-button-in-datalist-not-working

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
