A component of ASP.NET for creating RESTful web services that support HTTP-based communication between clients and servers.
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
alert shown on postback ([Submit] button clicked)