L
Luis Esteban Valencia
I have a asp.net page (C#), with a datagrid. I use template for all columns,
and have <asp:requiredfieldvalidator> in with one of the textboxes, to make
sure it's filled in. However, this validation is not firing, even when I
leave the field empty. Below please find the code:
and have <asp:requiredfieldvalidator> in with one of the textboxes, to make
sure it's filled in. However, this validation is not firing, even when I
leave the field empty. Below please find the code:
Code:
<%@ Page Language="C#" Debug="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<script language="C#" runat="server">
DataSet dsDepartments = new DataSet();
SqlConnection myConnection = new
SqlConnection("server=dbServer;database=Inventory;User
ID=appUser;Password=appPassword;");
SqlDataAdapter myCommand;
private void Page_Load(object Sender, EventArgs e)
{
if (ViewState["sortexpression"] == null)
{
ViewState["sortexpression"] = "deptName";
ViewState["sortorder"] = " ASC";
}
if (!IsPostBack)
{
ViewState["sortexpression"] = "deptName";
ViewState["sortorder"] = " ASC";
Response.Write("!IsPostBack");
BindGrid();
}
}
void doSort(object sender, DataGridSortCommandEventArgs e)
{
Response.Write("doSort");
if (ViewState["sortorder"] == " ASC" && ViewState["sortexpression"] ==
e.SortExpression)
{
ViewState["sortorder"] = " DESC";
}
else
{
ViewState["sortorder"] = " ASC";
ViewState["sortexpression"] = e.SortExpression;
}
BindGrid();
}
void doUpdate(object sender, DataGridCommandEventArgs e)
{
TextBox bName;
TextBox bNote;
TextBox bID;
bName =
(System.Web.UI.WebControls.TextBox)e.Item.FindControl("deptName");
bNote =
(System.Web.UI.WebControls.TextBox)e.Item.FindControl("deptNote");
bID = (System.Web.UI.WebControls.TextBox)e.Item.FindControl("deptID");
dgDepartments.ShowFooter = true;
dgDepartments.EditItemIndex = -1;
SqlConnection Con = new
SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["dbConn
String"]);
SqlCommand Cmd = new SqlCommand(string.Format("UPDATE dept SET deptName
= '{0}', deptNote = '{1}' WHERE deptID={2}", bName.Text, bNote.Text,
Convert.ToInt32(dgDepartments.DataKeys[e.Item.ItemIndex])), Con);
Con.Open();
Cmd.ExecuteNonQuery();
Con.Close();
BindGrid();
}
void doDelete(object sender, DataGridCommandEventArgs e)
{
long deptID =
Convert.ToInt32(dgDepartments.DataKeys[e.Item.ItemIndex]);
SqlConnection Con = new
SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["dbConn
String"]);
SqlCommand Cmd = new SqlCommand(string.Format("DELETE FROM dept WHERE
deptID={0}", deptID), Con);
Con.Open();
Cmd.ExecuteNonQuery();
Con.Close();
BindGrid();
}
void doEdit(object sender,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgDepartments.ShowFooter = false;
dgDepartments.EditItemIndex = (int)e.Item.ItemIndex;
BindGrid();
}
void doCancel(object sender, DataGridCommandEventArgs e)
{
dgDepartments.ShowFooter = true;
dgDepartments.EditItemIndex = -1;
BindGrid();
}
public void BindGrid()
{
myCommand = new SqlDataAdapter("select * from Dept",
myConnection);
myCommand.Fill(dsDepartments, "Dept");
DataView Source = dsDepartments.Tables["Dept"].DefaultView;
Source.Sort = ViewState["sortexpression"].ToString() +
ViewState["sortorder"].ToString();
dgDepartments.DataSource=Source;
Response.Write("BindGrid()");
dgDepartments.DataBind();
}
protected void doPage(object source,
System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
dgDepartments.CurrentPageIndex=e.NewPageIndex;
BindGrid();
}
void doInsert(object sender, DataGridCommandEventArgs e)
{
try
{
if (e.CommandName == "Insert" )
{
TextBox bName;
TextBox bNote;
bName =
(System.Web.UI.WebControls.TextBox)e.Item.FindControl("_deptName");
bNote =
(System.Web.UI.WebControls.TextBox)e.Item.FindControl("_deptNote");
SqlConnection Con = new
SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["dbConn
String"]);
SqlCommand Cmd = new SqlCommand(string.Format("INSERT INTO dept
(deptName, deptNote) VALUES ('{0}', '{1}')", bName.Text, bNote.Text), Con);
Con.Open();
Cmd.ExecuteNonQuery();
Con.Close();
BindGrid();
}
}
catch
{
}
}
</script>
<head>
<link href="../stylesheet.css" rel="stylesheet" type="text/css">
</head>
<body>
<H1>Departments</H1>
<form runat="server" id="form1">
<asp:DataGrid
AllowPaging="true"
AllowSorting="true"
AutoGenerateColumns="false"
CellPadding="3"
CellSpacing="0"
PageSize="10"
Width="100%"
DataKeyField="deptID"
PagerStyle-Mode="NextPrev"
PagerStyle-NextPageText="Next"
PagerStyle-PrevPageText="Previous"
PagerStyle-HorizontalAlign="Center"
PagerStyle-Position="TopAndBottom"
runat="server"
ID="dgDepartments"
ShowFooter="true"
ShowHeader="true"
runat="server"
OnPageIndexChanged="doPage"
onUpdateCommand="doUpdate"
onEditCommand="doEdit"
onDeleteCommand="doDelete"
onCancelCommand="doCancel"
onItemCommand="doInsert"
onSortCommand="doSort"<HeaderStyle HorizontalAlign="center" BackColor="#E8EBFD"
ForeColor="#3D3DB6" Font-Name="Verdana, Arial, Helvetica, sans-serif"
Font-Bold="true" Font-Size="smaller" />
<ItemStyle BackColor="#F2F2F2" Font-Name="Verdana, Arial, Helvetica,
sans-serif" Font-Size="smaller" />
<AlternatingItemStyle BackColor="#E5E5E5" Font-Name="Verdana, Arial,
Helvetica, sans-serif" Font-Size="smaller" />
<FooterStyle HorizontalAlign="left" BackColor="#E8EBFD"
ForeColor="#3D3DB6" Font-Name="Verdana, Arial, Helvetica, sans-serif"
Font-Bold="true" Font-Size="smaller" />
<PagerStyle BackColor="white" Font-Name="Verdana, Arial, Helvetica,
sans-serif" Font-Size="smaller" />
<Columns>
<asp:templatecolumn HeaderText="ID" Visible="true"
SortExpression="deptID">
<itemtemplate>
<%#DataBinder.Eval(Container.DataItem, "deptID") %>
</itemtemplate>
<edititemtemplate>
<%#DataBinder.Eval(Container.DataItem,"deptID") %>
</edititemtemplate>
</asp:templatecolumn>
<asp:TemplateColumn HeaderText="Name" SortExpression="deptName"
Visible="True">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "deptName") %>
</ItemTemplate>
<EditItemTemplate>
<asp:textbox text='<%# DataBinder.Eval(Container.DataItem,
"deptName") %>' id="deptName" runat="Server" cssClass="Text" />
<asp:requiredfieldvalidator runat="server" id="Name"
Visible="true" ControlToValidate="deptName" Enabled="true"
ErrorMessage="Required"
Display="Dynamic">Required</asp:requiredfieldvalidator>
</EditItemTemplate>
<footertemplate><asp:textbox id="_deptName" runat="server"
CssClass="Text" />
<asp:requiredfieldvalidator runat="server" id="_Name"
ControlToValidate="_deptName" Error="Fill in a name" Enabled="true"
Display="Dynamic">Required</asp:requiredfieldvalidator>
</footertemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Note" SortExpression="deptNote"
Visible="True">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "deptNote") %>
</ItemTemplate>
<EditItemTemplate>
<asp:textbox cssClass="Text" text='<%#
DataBinder.Eval(Container.DataItem, "deptNote") %>' id="deptNote"
runat="server" />
</EditItemTemplate>
<footertemplate>
<asp:textbox id="_deptNote" runat="server" CssClass="Text" />
</footertemplate>
</asp:TemplateColumn>
<asp:templatecolumn HeaderText="Edit">
<itemtemplate><asp:button CssClass="Text" CommandName="edit"
runat="server" CausesValidation="false" ID="edit"
Text="Edit"/> <asp:button CssClass="Text" CommandName="delete"
runat="server" ID="delete" Text="Delete" /></itemtemplate>
<edititemtemplate><asp:button CssClass="Text" CommandName="update"
runat="server" CausesValidation="true" ID="update" Text="Save"
/> <asp:button CssClass="Text" CommandName="cancel" runat="server"
CausesValidation="false" ID="cancel" Text="Cancel"/></edititemtemplate>
<footertemplate><asp:button CssClass="Text" Commandname = "Insert"
runat="server" ID="add" Text="Add" /></footertemplate>
</asp:templatecolumn>
</Columns>
</asp:DataGrid>
</form>
</body>
</html>