tostring()

zal wel 20 Reputation points
2026-03-08T02:30:36.1733333+00:00

for some reason the script doesn't work and the tostring

<%@ Page Language="C#" AutoEventWireup="True" %>
 <%@ Import Namespace="System"%>
    <script runat="server"> 
void Page_Load(Object sender, EventArgs e) 
      {
if(IsPostBack)  
{      
string nr = "";
for (int i = 0; i < Gride.Rows.Count; i++)
{
      nr =  i.ToString();
}
  string script = "alert('nr rows is'" + nr + ");";
ClientScript.RegisterStartupScript(this.GetType(), "PopupScript", script, true);
             }
}
</script>
<head runat="server">
</head>
<body>
  <form id="form1" runat="server">
<asp:GridView ID='Gride'>
<Columns>
<asp:TemplateField>
<ItemTemplate>
<label>hote</label>  
                    </ItemTemplate>
                </asp:TemplateField>
  <asp:TemplateField>                    
<ItemTemplate>
<label>Hotel</label>  
                    </ItemTemplate>
                </asp:TemplateField>
</Columns>
 </asp:GridView>
<input type="submit" value="Submit" class="submit" />  
</form>
</body>

Developer technologies | ASP.NET Core | ASP.NET API

Answer accepted by question author
SurferOnWww 6,031 Reputation points
2026-03-10T02:41:35.7+00:00

i would like to access the two rows, so the labels. i understand there is no datasource. Is this also possible?

Be aware that there is no row in your GridView as I told you repeatedly. Why do you think you can access to row that does not exist? It is impossible.

Are you sure you are talking about row? Not column? You can access the GridView column because you define the TemplateField as the GridView column even when there is no row. In your code because there are 2 TemplateFields, GridView1.Columns.Count counts 2. GridView1.Columns[0] returns the first TemplateField of your GridView and GridView1.Columns[1] second one.

Although I do not know what you mean by "access the two rows", if you want to show the alert with the message "nr rows is 2" on postback, you have to create 2 rows in your GridView.

Shown below is sample:

.aspx.cs

using System;
using System.Collections.Generic;

namespace WebForms1
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                string nr = GridView1.Rows.Count.ToString();
                string script = "alert('nr rows is " + nr + "');";
                ClientScript.RegisterStartupScript(this.GetType(), "PopupScript", script, true);
            }

            // Create 2 rows and bind them to the GridView
            if (!IsPostBack)
            {
                GridView1.DataSource = new List<Hotel>
                {
                    new Hotel { Id = 1, Name = "Hotel A", Address = "123 Main St", RoomCharge = 100.00m },
                    new Hotel { Id = 2, Name = "Hotel B", Address = "456 Elm St", RoomCharge = 150.00m }
                };
                GridView1.DataBind();
            }
        }        
    }

    public class Hotel
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Address { get; set; }
        public decimal RoomCharge { get; set; }
    }
}

.aspx

<%@ Page Language="C#" AutoEventWireup="true"
    CodeBehind="WebForm2.aspx.cs"
    Inherits="WebForms1.WebForm2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:GridView ID="GridView1" runat="server"></asp:GridView>

        <input type="submit" value="Submit" class="submit" />
    </form>
</body>
</html>

Initial Display on Chrome

enter image description here

alert shown on postback ([Submit] button clicked)

enter image description here

Was this answer helpful?


5 additional answers

Sort by: Newest
  1. AgaveJoe 31,181 Reputation points
    2026-03-12T12:06:31.7066667+00:00

    It looks like there are a few fundamental issues preventing your code from working as expected. To help us guide you better, could you clarify your goal? Here is why the current approach isn't working:

    Naming Container Issues: The line Gridview1.FindControl("label1") will always return null. Because the Label is inside a TemplateField, it exists within the naming container of the individual row, not the GridView itself. You must use row.FindControl("label1") inside your loop.

    Data Binding Lifecycle: For Gridview1.Rows to have any data to loop through during Page_Load, the GridView must already be bound to a data source. If you haven't bound the data yet, the row count will be zero.

    Overwrite Logic: Even if the loop worked, your labeltext variable is being overwritten in every iteration. By the time the script is registered, it will only ever show the text from the very last row in the grid.

    Control vs. Template: Your code currently treats the GridView like a flat structure, but ASP.NET templates require you to drill down into the specific row index to find controls.

    To help us provide a working code snippet, could you explain:

    1. Are you trying to alert the text from every row, or just one specific row?
    2. Where is your data being bound to the GridView?

    Was this answer helpful?

    0 comments No comments

  2. zal wel 20 Reputation points
    2026-03-09T10:19:52.3466667+00:00

    thanx for the answer. i would like to access the two rows, so the labels. i understand there is no datasource. Is this also possible?

    Was this answer helpful?


  3. Jack Dang (WICLOUD CORPORATION) 18,980 Reputation points Microsoft External Staff Moderator
    2026-03-09T03:55:54.8033333+00:00

    Hi @zal wel ,

    Thanks for reaching out.

    From the code you shared, it looks like the issue is not really related to ToString(). The main point is that the GridView does not currently have any rows, so there is nothing to iterate through.

    In your example, the GridView is declared with columns but it is not bound to any data source. Because of that, Grid1.Rows.Count will be 0, and the loop that tries to access the rows will never run.

    If your goal is simply to check how many rows exist in the GridView, you don't need a loop. You can directly use the Rows.Count property. For example:

    if (IsPostBack)
    {
        int count = Grid1.Rows.Count;
        string script = "alert('nr rows is " + count + "');";
        ClientScript.RegisterStartupScript(this.GetType(), "PopupScript", script, true);
    }
    

    However, for this to return a number greater than zero, the GridView must first be populated with data (for example through data binding). Otherwise the row count will remain 0.

    If you found my response helpful or informative in any way, I would greatly appreciate it if you could follow this guidance provide feedback. 

    Thank you.

    Was this answer helpful?


  4. zal wel 20 Reputation points
    2026-03-08T23:42:10.6666667+00:00

    The tostringvthing works now. Still i can't access the rows

    <%@ Page Language="C#" AutoEventWireup="True" %>
     
        <script runat="server"> 
    void Page_Load(Object sender, EventArgs e) 
          {
    if(IsPostBack)  
    {      
    string nr = "";
    for (int i = 0; i < 2; i++)
    {
         
         nr =  i.ToString();
    }
      string script = "alert('nr rows is " + nr + "');";
    Response.Write(script);
    ClientScript.RegisterStartupScript(this.GetType(), "PopupScript", script, true);
                 }
    }
    </script>
    <head runat="server">
    </head>
    <body>
      <form id="form1" runat="server">
    <asp:GridView ID='Grid1'>
    <Columns>
    <asp:TemplateField>
    <ItemTemplate>
    <label>hote</label>  
                        </ItemTemplate>
                    </asp:TemplateField>
      <asp:TemplateField>                    
    <ItemTemplate>
    <label>Hotel</label>  
                        </ItemTemplate>
                    </asp:TemplateField>
    </Columns>
     </asp:GridView>
    <input type="submit" value="Submit" class="submit" />  
    </form>
    </body>
    
    

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.