Share via

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 | ASP.NET API
{count} votes

Answer accepted by question author
  1. SurferOnWww 5,491 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


5 additional answers

Sort by: Most helpful
  1. 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?


  2. Jack Dang (WICLOUD CORPORATION) 14,955 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.


  3. 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>
    
    

  4. SurferOnWww 5,491 Reputation points
    2026-03-08T05:10:43.9366667+00:00

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

    Is your issue such that the alert with the message "nr rows is x" (x is Gride.Rows.Count - 1) is not shown on postback?

    If so it is because your code string script = "alert('nr rows is'" + nr + ");"; creates the string alert('nr rows is'4); which is not valid.

    Try changing your code to:

    string script = "alert('nr rows is " + nr + "');";
    

    Please note that because in your code the value of Gride.Rows.Count is 0 and line nr = i.ToString(); in the for loop will not be performed the result shown by alert will be nr rows is even after the above correction has been made.

    0 comments No comments

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.