Hi,

I would like to know if there is a way to rename a label which is inside a GridView during the runtime. What I'm trying to do is: if the user chooses an option, Label.Text will be "Option 1"; if he chooses the other option, Label.Text = "Option2". Is this possible?

Thanks!

Dani AI

Generated

Yes — you can change a Label inside an ASP.NET GridView at runtime. and asked for examples, flagged the need to reference the label, and correctly described the WinForms DataGridView (different control). For WebForms GridView there are three practical patterns depending on whether you want the change at data-binding time, by changing the data, or entirely client-side.

Markup data-binding (set text while binding)

<asp:TemplateField HeaderText="Choice">
  <ItemTemplate>
    <asp:Label ID="lblChoice" runat="server"
      Text='<%# If(Eval("Choice").ToString() = "A", "Option 1", "Option 2") %>' />
  </ItemTemplate>
</asp:TemplateField>

This keeps logic in the ASPX and is best when label text depends on the row data.

Update the data source and rebind (server-side)

' update a DataTable column then rebind so the template shows the new text
For Each r As DataRow In dt.Rows
  r("DisplayText") = If(userChoice = "A", "Option 1", "Option 2")
Next
GridView1.DataSource = dt
GridView1.DataBind()

Use this when the label value is derived from underlying data (safer for many rows or when you need persistence).

Client-side change (no postback)

<script>
document.getElementById('choiceBtn').addEventListener('click', function() {
  var choice = 'A'; // read from UI
  document.querySelectorAll('.gridLabel').forEach(function(lbl) {
    lbl.textContent = choice === 'A' ? 'Option 1' : 'Option 2';
  });
});
</script>

Add CssClass="gridLabel" to the Label in the template to select it easily.

Troubleshooting notes: keep runat="server" for server access; avoid rebinding the GridView on every Page_Load (wrap initial bind in If Not IsPostBack), because DataBind() will overwrite manual changes; client-side changes won’t persist server-side unless you update the data or send the value back. If the use case is a single-row action, update that row in the data source and rebind rather than relying on fragile client IDs.

Recommended Answers

All 4 Replies

Upload a sample project demonstrating the condition on what you want to change

Hi Ana D.,
Please show your code first.

Use FindControl method of GridView to have a reference of Label control.

DataGridView control have 2 collections
One is Columns and the other is Rows.
Rows collection contains Cells collection to represent each cell

If you want to access one cell in the grid, you have to know which row and which column you want.

DataGridView1.Rows(1).Cells(2).Value

This access the value of cell in row index 1 (index start from 0) and column (2)

I hope this small code can help you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.