- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <table>
- <tr>
- <td>Student ID:</td>
- <td>
- <asp:TextBox ID="txtStID" runat="server"></asp:TextBox>
- </td>
- </tr>
- <tr>
- <td colspan="2" align="center">
- <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
- </td>
- </tr>
- </table>
- </div>
- <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
- <div>
- <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
- GridLines="None" OnRowDataBound = "OnRowDataBound">
- <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
- <EditRowStyle BackColor="#999999" />
- <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
- <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
- <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
- <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
- <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
- <SortedAscendingCellStyle BackColor="#E9E7E2" />
- <SortedAscendingHeaderStyle BackColor="#506C8C" />
- <SortedDescendingCellStyle BackColor="#FFFDF8" />
- <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
- </asp:GridView>
- </div>
- </form>
- </body>
- </html>
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- namespace GridViewWithoutDBandStateMngmt
- {
- public partial class WebForm1 : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- protected void btnSave_Click(object sender, EventArgs e)
- {
- if (txtStID.Text != string.Empty)
- {
- DataTable dt = new DataTable();
- dt.Columns.Add("Student ID");
- for (int row = 0; row < GridView1.Rows.Count; row++)
- {
- DataRow dr = dt.NewRow();
- dr[0] = GridView1.Rows[row].Cells[0].Text;
- dt.Rows.Add(dr);
- }
- dt.Rows.Add(txtStID.Text);
- GridView1.DataSource = dt;
- GridView1.DataBind();
- txtStID.Text = string.Empty;
- loop();
- }
- }
- protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
- {
- if (e.Row.RowType == DataControlRowType.DataRow)
- {
- foreach (GridViewRow row in GridView1.Rows)
- {
- string name = e.Row.Cells[0].Text;
- ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('" + name + "');", true);
- }
- }
- }
- }
- }
but is displaying only current inserted row i want to display all data from the row