|
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: -
<%@ 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>
-
--
LUIS ESTEBAN VALENCIA
MICROSOFT DCE 3.
MIEMBRO ACTIVO DE ALIANZADEV http://spaces.msn.com/members/extremed/ | |
Share:
|
There are problems trying to implement cell by cell validation using the
grid's Validating event architecture. The problem is that the grid is not the
object handling the data. Instead, a TextBox or some other control is the
control managing the changing of the cell contents. One way to implement the
validation at the grid level is to handle the CurrentCellChanged event, and
if the previous cell's value is not proper, then return to that cell.
Also be sure that your client scripting is running ok. Any Client scripting
that have errors, causes no client validation.
Fernando Hunth
Senior Developer
Huddle Group S.A. fe******@huddle.com.ar
Huddle Group S.A. | Enterprise Technology Services
Microsoft Certified Partner
Ciudad de la Paz 2719
· Piso 6D (C1428CPU)
· Ciudad de Buenos Aires · Argentina www.huddle.com.ar
"Luis Esteban Valencia" wrote: 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:
- <%@ 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>
-
-- LUIS ESTEBAN VALENCIA MICROSOFT DCE 3. MIEMBRO ACTIVO DE ALIANZADEV http://spaces.msn.com/members/extremed/ | | |
How Could I Implement it on the code I have?'
more or less?? a snippet of code?
--
LUIS ESTEBAN VALENCIA
MICROSOFT DCE 3.
MIEMBRO ACTIVO DE ALIANZADEV http://spaces.msn.com/members/extremed/
"Fernando Hunth" <Fe***********@discussions.microsoft.com> escribió en el
mensaje news:FD**********************************@microsof t.com... There are problems trying to implement cell by cell validation using the grid's Validating event architecture. The problem is that the grid is not
the object handling the data. Instead, a TextBox or some other control is the control managing the changing of the cell contents. One way to implement
the validation at the grid level is to handle the CurrentCellChanged event,
and if the previous cell's value is not proper, then return to that cell.
Also be sure that your client scripting is running ok. Any Client
scripting that have errors, causes no client validation.
Fernando Hunth Senior Developer Huddle Group S.A. fe******@huddle.com.ar Huddle Group S.A. | Enterprise Technology Services Microsoft Certified Partner
Ciudad de la Paz 2719
· Piso 6D (C1428CPU)
· Ciudad de Buenos Aires · Argentina www.huddle.com.ar
"Luis Esteban Valencia" wrote:
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:
- > <%@ 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>
- >
-- LUIS ESTEBAN VALENCIA MICROSOFT DCE 3. MIEMBRO ACTIVO DE ALIANZADEV http://spaces.msn.com/members/extremed/ | | |
Take a look at http://www21.brinkster.com/fhunth/De...AQDatagrid.asp
"Luis Esteban Valencia" wrote: How Could I Implement it on the code I have?' more or less?? a snippet of code?
-- LUIS ESTEBAN VALENCIA MICROSOFT DCE 3. MIEMBRO ACTIVO DE ALIANZADEV http://spaces.msn.com/members/extremed/ "Fernando Hunth" <Fe***********@discussions.microsoft.com> escribió en el mensaje news:FD**********************************@microsof t.com... There are problems trying to implement cell by cell validation using the grid's Validating event architecture. The problem is that the grid is not the object handling the data. Instead, a TextBox or some other control is the control managing the changing of the cell contents. One way to implement the validation at the grid level is to handle the CurrentCellChanged event, and if the previous cell's value is not proper, then return to that cell.
Also be sure that your client scripting is running ok. Any Client scripting that have errors, causes no client validation.
Fernando Hunth Senior Developer Huddle Group S.A. fe******@huddle.com.ar Huddle Group S.A. | Enterprise Technology Services Microsoft Certified Partner
Ciudad de la Paz 2719
· Piso 6D (C1428CPU)
· Ciudad de Buenos Aires · Argentina www.huddle.com.ar
"Luis Esteban Valencia" wrote:
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:
- > > <%@ 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>
- > >
-- LUIS ESTEBAN VALENCIA MICROSOFT DCE 3. MIEMBRO ACTIVO DE ALIANZADEV http://spaces.msn.com/members/extremed/ | | |
I didnt find anything related. and it says Windows Forms Datagrid???
--
LUIS ESTEBAN VALENCIA
MICROSOFT DCE 3.
MIEMBRO ACTIVO DE ALIANZADEV http://spaces.msn.com/members/extremed/
"Fernando Hunth" <Fe***********@discussions.microsoft.com> escribió en el
mensaje news:1C**********************************@microsof t.com... Take a look at
http://www21.brinkster.com/fhunth/De...AQDatagrid.asp "Luis Esteban Valencia" wrote:
How Could I Implement it on the code I have?' more or less?? a snippet of code?
-- LUIS ESTEBAN VALENCIA MICROSOFT DCE 3. MIEMBRO ACTIVO DE ALIANZADEV http://spaces.msn.com/members/extremed/ "Fernando Hunth" <Fe***********@discussions.microsoft.com> escribió en
el mensaje news:FD**********************************@microsof t.com... There are problems trying to implement cell by cell validation using
the grid's Validating event architecture. The problem is that the grid is
not the object handling the data. Instead, a TextBox or some other control is
the control managing the changing of the cell contents. One way to
implement the validation at the grid level is to handle the CurrentCellChanged
event, and if the previous cell's value is not proper, then return to that cell.
Also be sure that your client scripting is running ok. Any Client scripting that have errors, causes no client validation.
Fernando Hunth Senior Developer Huddle Group S.A. fe******@huddle.com.ar Huddle Group S.A. | Enterprise Technology Services Microsoft Certified Partner
Ciudad de la Paz 2719
· Piso 6D (C1428CPU)
· Ciudad de Buenos Aires · Argentina www.huddle.com.ar
"Luis Esteban Valencia" wrote:
> 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: > > - > > > <%@ 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>
- > > >
> > > > -- > LUIS ESTEBAN VALENCIA > MICROSOFT DCE 3. > MIEMBRO ACTIVO DE ALIANZADEV > http://spaces.msn.com/members/extremed/ > > > | | This discussion thread is closed Replies have been disabled for this discussion. Similar topics
reply
views
Thread by Daniel Armstrong |
last post: by
|
7 posts
views
Thread by Scott Schluer |
last post: by
|
1 post
views
Thread by news.microsoft.com |
last post: by
|
4 posts
views
Thread by jobz |
last post: by
|
3 posts
views
Thread by John Blair |
last post: by
|
5 posts
views
Thread by Jason |
last post: by
|
1 post
views
Thread by margant@gmail.com |
last post: by
|
2 posts
views
Thread by Mike Surcouf |
last post: by
|
2 posts
views
Thread by Stuart Whiteford |
last post: by
| | | | | | | | | | |