Good Morning! Hopefully I am posting this in the correct forum location. I am currently working on a new data application that is a vb.net web based application using visual studio. I have a form with a datagrid view that I have customized to have all rows editable at all times. I need to get this form to show a blank row for inserting data if there is no data returned from the sql server table. I also need to get this form to prompt you to save your changes when you navigate away from the screen by any method. My asp and vb.net code is shown below. Any help would be appreciated as I am new to vb.net! Also if there is an easier or better way to accomplish what I am trying to do please let me know. Thank you!
Code:
<%@ Page Title="frmPublication" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeBehind="frmPublication.aspx.vb" Inherits="MCRN.frmPublication" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" DataKeyNames="PublicationID"
DataSourceID="SqlDataSource1" AllowSorting="True"
ShowHeaderWhenEmpty="True" showfooter="true"
>
<EmptyDataTemplate>
</EmptyDataTemplate>
<Columns>
<asp:TemplateField ShowHeader="True">
<FooterTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="Delete"
onclientclick="return confirm('Are you sure you want to delete?');"
Text="Delete"></asp:LinkButton>
</FooterTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="Delete" Text="Delete"
onclientclick="return confirm('Are you sure you want to delete?');"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="PublicationID" HeaderText="PublicationID"
InsertVisible="False" ReadOnly="True" SortExpression="PublicationID" />
<asp:TemplateField HeaderText="Publication" SortExpression="PublicationDesc">
<EditItemTemplate>
<asp:TextBox ID="PublicationTextBox" runat="server" Text='<%# Bind("PublicationDesc") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="PublicationTextBox" runat="server" ></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:TextBox ID="PublicationTextBox" runat="server" MaxLength="50"
Text='<%# Bind("PublicationDesc") %>' CausesValidation="True"
ValidationGroup="Required" ErrorMessage="Publication Name Required"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Unit of Cost"
SortExpression="PublicationUnitOfCost">
<EditItemTemplate>
<asp:TextBox ID="UnitOfCostTextBox" runat="server"
Text='<%# Bind("PublicationUnitOfCost") %>' ></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="UnitOfCostTextBox" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:TextBox ID="UnitOfCostTextBox" runat="server" MaxLength="10"
Text='<%# Bind("PublicationUnitOfCost") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Inactive" SortExpression="InactiveIndicator">
<EditItemTemplate>
<asp:CheckBox ID="InactiveCheckBox" runat="server"
Checked='<%# Bind("InactiveIndicator") %>' />
</EditItemTemplate>
<FooterTemplate>
<asp:CheckBox ID="InactiveCheckBox" runat="server" />
</FooterTemplate>
<ItemTemplate>
<asp:CheckBox ID="InactiveCheckBox" runat="server"
Checked='<%# Bind("InactiveIndicator") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>Code:
Imports System.Web.UI.WebControls
Imports System.ComponentModel
Public Class frmPublication
Inherits System.Web.UI.Page
' Inherits System.Web.UI.Control
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
EmptyGridFix(GridView1)
End Sub
Public Class GridView1EmptyTemplate
Implements ITemplate
Public Sub InstantiateIn(container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn
Dim cell As TableCell = TryCast(container, TableCell)
cell.Attributes.Add("style", "background-color:#5D7B9D;text-align:center;color:#fff;font-weight:bold;")
container.Controls.Add(New LiteralControl("No documents found"))
End Sub
End Class
' Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' EmptyGridFix(GridView1)
' End Sub
'
Protected Sub EmptyGridFix(ByVal grdView As GridView) ' normally executes after a grid load method
If grdView.Rows.Count = 0 AndAlso grdView.DataSource IsNot Nothing Then
Dim dt As DataTable = Nothing
' need to clone sources otherwise it will be indirectly adding to the original source
If TypeOf grdView.DataSource Is DataSet Then
dt = DirectCast(grdView.DataSource, DataSet).Tables(0).Clone()
ElseIf TypeOf grdView.DataSource Is DataTable Then
dt = DirectCast(grdView.DataSource, DataTable).Clone()
End If
If dt Is Nothing Then
Return
End If
dt.Rows.Add(dt.NewRow())
' add empty row
grdView.DataSource = dt
grdView.DataBind()
'hide(row) '
grdView.Rows(0).Visible = False
grdView.Rows(0).Controls.Clear()
End If
' normally executes at all postbacks
If grdView.Rows.Count = 1 AndAlso grdView.DataSource Is Nothing Then
Dim bIsGridEmpty As Boolean = True
'check first row that all cells empty
For i As Integer = 0 To grdView.Rows(0).Cells.Count - 1
If grdView.Rows(0).Cells(i).Text <> String.Empty Then
bIsGridEmpty = False
End If
Next
'hide(row)
If bIsGridEmpty Then
grdView.Rows(0).Visible = True
' grdView.Rows(0).Controls.Clear()
End If
End If
End Sub
Private tableCopied As Boolean = False
Private originalDataTable As System.Data.DataTable
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
If Not tableCopied Then
originalDataTable = CType(e.Row.DataItem, System.Data.DataRowView).Row.Table.Copy()
ViewState("originalValuesDataTable") = originalDataTable
tableCopied = True
End If
End If
End Sub