By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,956 Members | 1,224 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,956 IT Pros & Developers. It's quick & easy.

Validators inside datagrid do not fire

P: n/a
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:

Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="C#" Debug="true" %>
  2.  
  3. <%@ Import Namespace="System.Data" %>
  4. <%@ Import Namespace="System.Data.SqlClient" %>
  5.  
  6. <html>
  7. <script language="C#"  runat="server">
  8.  
  9. DataSet dsDepartments = new DataSet();
  10. SqlConnection myConnection = new
  11. SqlConnection("server=dbServer;database=Inventory;User
  12. ID=appUser;Password=appPassword;");
  13. SqlDataAdapter myCommand;
  14.  
  15.  
  16. private void Page_Load(object Sender, EventArgs e)
  17. {
  18. if (ViewState["sortexpression"] == null)
  19. {
  20. ViewState["sortexpression"] = "deptName";
  21. ViewState["sortorder"] = " ASC";
  22. }
  23. if (!IsPostBack)
  24. {
  25. ViewState["sortexpression"] = "deptName";
  26. ViewState["sortorder"] = " ASC";
  27. Response.Write("!IsPostBack");
  28. BindGrid();
  29. }
  30. }
  31.  
  32. void doSort(object sender, DataGridSortCommandEventArgs e)
  33. {
  34. Response.Write("doSort");
  35. if (ViewState["sortorder"] == " ASC" && ViewState["sortexpression"] ==
  36. e.SortExpression)
  37. {
  38. ViewState["sortorder"] = " DESC";
  39. }
  40. else
  41. {
  42. ViewState["sortorder"] = " ASC";
  43. ViewState["sortexpression"] = e.SortExpression;
  44. }
  45. BindGrid();
  46. }
  47. void doUpdate(object sender, DataGridCommandEventArgs e)
  48. {
  49. TextBox bName;
  50. TextBox bNote;
  51. TextBox bID;
  52. bName =
  53. (System.Web.UI.WebControls.TextBox)e.Item.FindControl("deptName");
  54. bNote =
  55. (System.Web.UI.WebControls.TextBox)e.Item.FindControl("deptNote");
  56. bID = (System.Web.UI.WebControls.TextBox)e.Item.FindControl("deptID");
  57. dgDepartments.ShowFooter = true;
  58. dgDepartments.EditItemIndex = -1;
  59. SqlConnection Con = new
  60. SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["dbConn
  61. String"]);
  62. SqlCommand Cmd = new SqlCommand(string.Format("UPDATE dept SET deptName
  63. = '{0}', deptNote = '{1}' WHERE deptID={2}", bName.Text, bNote.Text,
  64. Convert.ToInt32(dgDepartments.DataKeys[e.Item.ItemIndex])), Con);
  65. Con.Open();
  66. Cmd.ExecuteNonQuery();
  67. Con.Close();
  68. BindGrid();
  69. }
  70.  
  71. void doDelete(object sender, DataGridCommandEventArgs e)
  72. {
  73. long deptID =
  74. Convert.ToInt32(dgDepartments.DataKeys[e.Item.ItemIndex]);
  75. SqlConnection Con = new
  76. SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["dbConn
  77. String"]);
  78. SqlCommand Cmd = new SqlCommand(string.Format("DELETE FROM dept WHERE
  79. deptID={0}", deptID), Con);
  80. Con.Open();
  81. Cmd.ExecuteNonQuery();
  82. Con.Close();
  83. BindGrid();
  84. }
  85.  
  86. void doEdit(object sender,
  87. System.Web.UI.WebControls.DataGridCommandEventArgs  e)
  88. {
  89. dgDepartments.ShowFooter = false;
  90. dgDepartments.EditItemIndex = (int)e.Item.ItemIndex;
  91. BindGrid();
  92. }
  93.  
  94. void doCancel(object sender, DataGridCommandEventArgs e)
  95. {
  96. dgDepartments.ShowFooter = true;
  97. dgDepartments.EditItemIndex = -1;
  98. BindGrid();
  99. }
  100.  
  101.  
  102. public void BindGrid()
  103. {
  104. myCommand = new SqlDataAdapter("select * from Dept",
  105. myConnection);
  106. myCommand.Fill(dsDepartments, "Dept");
  107. DataView Source = dsDepartments.Tables["Dept"].DefaultView;
  108. Source.Sort = ViewState["sortexpression"].ToString() +
  109. ViewState["sortorder"].ToString();
  110. dgDepartments.DataSource=Source;
  111. Response.Write("BindGrid()");
  112. dgDepartments.DataBind();
  113. }
  114.  
  115. protected void doPage(object source,
  116. System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
  117. {
  118. dgDepartments.CurrentPageIndex=e.NewPageIndex;
  119. BindGrid();
  120. }
  121.  
  122. void doInsert(object sender, DataGridCommandEventArgs e)
  123. {
  124. try
  125. {
  126. if (e.CommandName == "Insert" )
  127. {
  128. TextBox bName;
  129. TextBox bNote;
  130. bName =
  131. (System.Web.UI.WebControls.TextBox)e.Item.FindControl("_deptName");
  132. bNote =
  133. (System.Web.UI.WebControls.TextBox)e.Item.FindControl("_deptNote");
  134. SqlConnection Con = new
  135. SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["dbConn
  136. String"]);
  137. SqlCommand Cmd = new SqlCommand(string.Format("INSERT INTO dept
  138. (deptName, deptNote) VALUES ('{0}', '{1}')", bName.Text, bNote.Text), Con);
  139. Con.Open();
  140. Cmd.ExecuteNonQuery();
  141. Con.Close();
  142. BindGrid();
  143. }
  144. }
  145. catch
  146. {
  147. }
  148. }
  149.  
  150.  
  151. </script>
  152.  
  153. <head>
  154. <link href="../stylesheet.css" rel="stylesheet" type="text/css">
  155. </head>
  156.  
  157. <body>
  158. <H1>Departments</H1>
  159. <form runat="server"  id="form1">
  160. <asp:DataGrid
  161. AllowPaging="true"
  162. AllowSorting="true"
  163. AutoGenerateColumns="false"
  164. CellPadding="3"
  165. CellSpacing="0"
  166. PageSize="10"
  167. Width="100%"
  168. DataKeyField="deptID"
  169. PagerStyle-Mode="NextPrev"
  170. PagerStyle-NextPageText="Next"
  171. PagerStyle-PrevPageText="Previous"
  172. PagerStyle-HorizontalAlign="Center"
  173. PagerStyle-Position="TopAndBottom"
  174. runat="server"
  175. ID="dgDepartments"
  176. ShowFooter="true"
  177. ShowHeader="true"
  178. runat="server"
  179. OnPageIndexChanged="doPage"
  180. onUpdateCommand="doUpdate"
  181. onEditCommand="doEdit"
  182. onDeleteCommand="doDelete"
  183. onCancelCommand="doCancel"
  184. onItemCommand="doInsert"
  185. onSortCommand="doSort"
  186. <HeaderStyle HorizontalAlign="center" BackColor="#E8EBFD"
  187. ForeColor="#3D3DB6" Font-Name="Verdana, Arial, Helvetica, sans-serif"
  188. Font-Bold="true" Font-Size="smaller" />
  189. <ItemStyle BackColor="#F2F2F2" Font-Name="Verdana, Arial, Helvetica,
  190. sans-serif" Font-Size="smaller" />
  191. <AlternatingItemStyle BackColor="#E5E5E5" Font-Name="Verdana, Arial,
  192. Helvetica, sans-serif" Font-Size="smaller" />
  193. <FooterStyle HorizontalAlign="left" BackColor="#E8EBFD"
  194. ForeColor="#3D3DB6" Font-Name="Verdana, Arial, Helvetica, sans-serif"
  195. Font-Bold="true" Font-Size="smaller" />
  196. <PagerStyle BackColor="white" Font-Name="Verdana, Arial, Helvetica,
  197. sans-serif" Font-Size="smaller" />
  198. <Columns>
  199. <asp:templatecolumn HeaderText="ID" Visible="true"
  200. SortExpression="deptID">
  201. <itemtemplate>
  202. <%#DataBinder.Eval(Container.DataItem, "deptID")  %>
  203. </itemtemplate>
  204. <edititemtemplate>
  205. <%#DataBinder.Eval(Container.DataItem,"deptID") %>
  206. </edititemtemplate>
  207. </asp:templatecolumn>
  208. <asp:TemplateColumn HeaderText="Name"  SortExpression="deptName"
  209. Visible="True">
  210. <ItemTemplate>
  211. <%# DataBinder.Eval(Container.DataItem, "deptName")  %>
  212. </ItemTemplate>
  213. <EditItemTemplate>
  214. <asp:textbox text='<%# DataBinder.Eval(Container.DataItem,
  215. "deptName") %>' id="deptName" runat="Server" cssClass="Text" />
  216. <asp:requiredfieldvalidator runat="server" id="Name"
  217. Visible="true" ControlToValidate="deptName" Enabled="true"
  218. ErrorMessage="Required"
  219. Display="Dynamic">Required</asp:requiredfieldvalidator>
  220. </EditItemTemplate>
  221. <footertemplate><asp:textbox id="_deptName" runat="server"
  222. CssClass="Text" />
  223. <asp:requiredfieldvalidator runat="server" id="_Name"
  224. ControlToValidate="_deptName" Error="Fill in a name" Enabled="true"
  225. Display="Dynamic">Required</asp:requiredfieldvalidator>
  226. </footertemplate>
  227. </asp:TemplateColumn>
  228. <asp:TemplateColumn HeaderText="Note"  SortExpression="deptNote"
  229. Visible="True">
  230. <ItemTemplate>
  231. <%# DataBinder.Eval(Container.DataItem, "deptNote") %>
  232. </ItemTemplate>
  233. <EditItemTemplate>
  234. <asp:textbox cssClass="Text" text='<%#
  235. DataBinder.Eval(Container.DataItem, "deptNote") %>' id="deptNote"
  236. runat="server" />
  237. </EditItemTemplate>
  238. <footertemplate>
  239. <asp:textbox id="_deptNote" runat="server" CssClass="Text" />
  240. </footertemplate>
  241. </asp:TemplateColumn>
  242. <asp:templatecolumn HeaderText="Edit">
  243. <itemtemplate><asp:button CssClass="Text" CommandName="edit"
  244. runat="server"  CausesValidation="false" ID="edit"
  245. Text="Edit"/>&nbsp;<asp:button CssClass="Text" CommandName="delete"
  246. runat="server" ID="delete" Text="Delete" /></itemtemplate>
  247. <edititemtemplate><asp:button CssClass="Text" CommandName="update"
  248. runat="server" CausesValidation="true" ID="update" Text="Save"
  249. />&nbsp;<asp:button CssClass="Text" CommandName="cancel" runat="server"
  250. CausesValidation="false" ID="cancel" Text="Cancel"/></edititemtemplate>
  251. <footertemplate><asp:button CssClass="Text" Commandname = "Insert"
  252. runat="server" ID="add" Text="Add" /></footertemplate>
  253. </asp:templatecolumn>
  254. </Columns>
  255. </asp:DataGrid>
  256. </form>
  257. </body>
  258. </html>
  259.  


--
LUIS ESTEBAN VALENCIA
MICROSOFT DCE 3.
MIEMBRO ACTIVO DE ALIANZADEV
http://spaces.msn.com/members/extremed/
Nov 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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:

Expand|Select|Wrap|Line Numbers
  1.  <%@ Page Language="C#" Debug="true" %>
  2.  <%@ Import Namespace="System.Data" %>
  3.  <%@ Import Namespace="System.Data.SqlClient" %>
  4.  <html>
  5.  <script language="C#"  runat="server">
  6.      DataSet dsDepartments = new DataSet();
  7.      SqlConnection myConnection = new
  8.  SqlConnection("server=dbServer;database=Inventory;User
  9.  ID=appUser;Password=appPassword;");
  10.       SqlDataAdapter myCommand;
  11.  private void Page_Load(object Sender, EventArgs e)
  12.  {
  13.       if (ViewState["sortexpression"] == null)
  14.       {
  15.            ViewState["sortexpression"] = "deptName";
  16.            ViewState["sortorder"] = " ASC";
  17.       }
  18.       if (!IsPostBack)
  19.       {
  20.            ViewState["sortexpression"] = "deptName";
  21.            ViewState["sortorder"] = " ASC";
  22.            Response.Write("!IsPostBack");
  23.            BindGrid();
  24.       }
  25.  }
  26.  void doSort(object sender, DataGridSortCommandEventArgs e)
  27.  {
  28.       Response.Write("doSort");
  29.       if (ViewState["sortorder"] == " ASC" && ViewState["sortexpression"] ==
  30.  e.SortExpression)
  31.       {
  32.            ViewState["sortorder"] = " DESC";
  33.       }
  34.       else
  35.       {
  36.            ViewState["sortorder"] = " ASC";
  37.            ViewState["sortexpression"] = e.SortExpression;
  38.       }
  39.       BindGrid();
  40.  }
  41.  void doUpdate(object sender, DataGridCommandEventArgs e)
  42.  {
  43.       TextBox bName;
  44.       TextBox bNote;
  45.       TextBox bID;
  46.       bName =
  47.  (System.Web.UI.WebControls.TextBox)e.Item.FindControl("deptName");
  48.       bNote =
  49.  (System.Web.UI.WebControls.TextBox)e.Item.FindControl("deptNote");
  50.       bID = (System.Web.UI.WebControls.TextBox)e.Item.FindControl("deptID");
  51.       dgDepartments.ShowFooter = true;
  52.       dgDepartments.EditItemIndex = -1;
  53.       SqlConnection Con = new
  54.  SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["dbConn
  55.  String"]);
  56.       SqlCommand Cmd = new SqlCommand(string.Format("UPDATE dept SET deptName
  57.  = '{0}', deptNote = '{1}' WHERE deptID={2}", bName.Text, bNote.Text,
  58.  Convert.ToInt32(dgDepartments.DataKeys[e.Item.ItemIndex])), Con);
  59.       Con.Open();
  60.       Cmd.ExecuteNonQuery();
  61.       Con.Close();
  62.       BindGrid();
  63.  }
  64.  void doDelete(object sender, DataGridCommandEventArgs e)
  65.  {
  66.       long deptID =
  67.  Convert.ToInt32(dgDepartments.DataKeys[e.Item.ItemIndex]);
  68.       SqlConnection Con = new
  69.  SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["dbConn
  70.  String"]);
  71.       SqlCommand Cmd = new SqlCommand(string.Format("DELETE FROM dept WHERE
  72.  deptID={0}", deptID), Con);
  73.       Con.Open();
  74.       Cmd.ExecuteNonQuery();
  75.       Con.Close();
  76.       BindGrid();
  77.  }
  78.  void doEdit(object sender,
  79.  System.Web.UI.WebControls.DataGridCommandEventArgs  e)
  80.  {
  81.       dgDepartments.ShowFooter = false;
  82.       dgDepartments.EditItemIndex = (int)e.Item.ItemIndex;
  83.       BindGrid();
  84.  }
  85.  void doCancel(object sender, DataGridCommandEventArgs e)
  86.  {
  87.       dgDepartments.ShowFooter = true;
  88.       dgDepartments.EditItemIndex = -1;
  89.       BindGrid();
  90.  }
  91.  public void BindGrid()
  92.  {
  93.            myCommand = new SqlDataAdapter("select * from Dept",
  94.  myConnection);
  95.          myCommand.Fill(dsDepartments, "Dept");
  96.          DataView Source = dsDepartments.Tables["Dept"].DefaultView;
  97.          Source.Sort = ViewState["sortexpression"].ToString() +
  98.  ViewState["sortorder"].ToString();
  99.          dgDepartments.DataSource=Source;
  100.            Response.Write("BindGrid()");
  101.          dgDepartments.DataBind();
  102.  }
  103.  protected void doPage(object source,
  104.  System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
  105.  {
  106.       dgDepartments.CurrentPageIndex=e.NewPageIndex;
  107.       BindGrid();
  108.  }
  109.  void doInsert(object sender, DataGridCommandEventArgs e)
  110.  {
  111.    try
  112.    {
  113.         if (e.CommandName == "Insert" )
  114.         {
  115.              TextBox bName;
  116.              TextBox bNote;
  117.              bName =
  118.  (System.Web.UI.WebControls.TextBox)e.Item.FindControl("_deptName");
  119.              bNote =
  120.  (System.Web.UI.WebControls.TextBox)e.Item.FindControl("_deptNote");
  121.              SqlConnection Con = new
  122.  SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["dbConn
  123.  String"]);
  124.              SqlCommand Cmd = new SqlCommand(string.Format("INSERT INTO dept
  125.  (deptName, deptNote) VALUES ('{0}', '{1}')", bName.Text, bNote.Text), Con);
  126.              Con.Open();
  127.              Cmd.ExecuteNonQuery();
  128.              Con.Close();
  129.              BindGrid();
  130.         }
  131.    }
  132.    catch
  133.    {
  134.    }
  135.  }
  136.  </script>
  137.  <head>
  138.  <link href="../stylesheet.css" rel="stylesheet" type="text/css">
  139.  </head>
  140.  <body>
  141.  <H1>Departments</H1>
  142.  <form runat="server"  id="form1">
  143.  <asp:DataGrid
  144.    AllowPaging="true"
  145.    AllowSorting="true"
  146.    AutoGenerateColumns="false"
  147.    CellPadding="3"
  148.    CellSpacing="0"
  149.    PageSize="10"
  150.    Width="100%"
  151.    DataKeyField="deptID"
  152.    PagerStyle-Mode="NextPrev"
  153.    PagerStyle-NextPageText="Next"
  154.    PagerStyle-PrevPageText="Previous"
  155.    PagerStyle-HorizontalAlign="Center"
  156.    PagerStyle-Position="TopAndBottom"
  157.    runat="server"
  158.    ID="dgDepartments"
  159.    ShowFooter="true"
  160.    ShowHeader="true"
  161.    runat="server"
  162.    OnPageIndexChanged="doPage"
  163.    onUpdateCommand="doUpdate"
  164.    onEditCommand="doEdit"
  165.    onDeleteCommand="doDelete"
  166.    onCancelCommand="doCancel"
  167.    onItemCommand="doInsert"
  168.    onSortCommand="doSort"
  169.  >
  •      <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/

    Nov 19 '05 #2

    P: n/a
    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:

    Expand|Select|Wrap|Line Numbers
    1.  > <%@ Page Language="C#" Debug="true" %>
    2.  >
    3.  > <%@ Import Namespace="System.Data" %>
    4.  > <%@ Import Namespace="System.Data.SqlClient" %>
    5.  >
    6.  > <html>
    7.  > <script language="C#"  runat="server">
    8.  >
    9.  >     DataSet dsDepartments = new DataSet();
    10.  >     SqlConnection myConnection = new
    11.  > SqlConnection("server=dbServer;database=Inventory;User
    12.  > ID=appUser;Password=appPassword;");
    13.  >      SqlDataAdapter myCommand;
    14.  >
    15.  >
    16.  > private void Page_Load(object Sender, EventArgs e)
    17.  > {
    18.  >      if (ViewState["sortexpression"] == null)
    19.  >      {
    20.  >           ViewState["sortexpression"] = "deptName";
    21.  >           ViewState["sortorder"] = " ASC";
    22.  >      }
    23.  >      if (!IsPostBack)
    24.  >      {
    25.  >           ViewState["sortexpression"] = "deptName";
    26.  >           ViewState["sortorder"] = " ASC";
    27.  >           Response.Write("!IsPostBack");
    28.  >           BindGrid();
    29.  >      }
    30.  > }
    31.  >
    32.  > void doSort(object sender, DataGridSortCommandEventArgs e)
    33.  > {
    34.  >      Response.Write("doSort");
    35.  >      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/

    Nov 19 '05 #3

    P: n/a
    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:

    Expand|Select|Wrap|Line Numbers
    1.  > > <%@ Page Language="C#" Debug="true" %>
    2.  > >
    3.  > > <%@ Import Namespace="System.Data" %>
    4.  > > <%@ Import Namespace="System.Data.SqlClient" %>
    5.  > >
    6.  > > <html>
    7.  > > <script language="C#"  runat="server">
    8.  > >
    9.  > >     DataSet dsDepartments = new DataSet();
    10.  > >     SqlConnection myConnection = new
    11.  > > SqlConnection("server=dbServer;database=Inventory;User
    12.  > > ID=appUser;Password=appPassword;");
    13.  > >      SqlDataAdapter myCommand;
    14.  > >
    15.  > >
    16.  > > private void Page_Load(object Sender, EventArgs e)
    17.  > > {
    18.  > >      if (ViewState["sortexpression"] == null)
    19.  > >      {
    20.  > >           ViewState["sortexpression"] = "deptName";
    21.  > >           ViewState["sortorder"] = " ASC";
    22.  > >      }
    23.  > >      if (!IsPostBack)
    24.  > >      {
    25.  > >           ViewState["sortexpression"] = "deptName";
    26.  > >           ViewState["sortorder"] = " ASC";
    27.  > >           Response.Write("!IsPostBack");
    28.  > >           BindGrid();
    29.  > >      }
    30.  > > }
    31.  > >
    32.  > > void doSort(object sender, DataGridSortCommandEventArgs e)
    33.  > > {
    34.  > >      Response.Write("doSort");
    35.  > >      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/


    Nov 19 '05 #4

    P: n/a
    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:
    >
    >
    Expand|Select|Wrap|Line Numbers
    1.  > > > <%@ Page Language="C#" Debug="true" %>
    2.  > > >
    3.  > > > <%@ Import Namespace="System.Data" %>
    4.  > > > <%@ Import Namespace="System.Data.SqlClient" %>
    5.  > > >
    6.  > > > <html>
    7.  > > > <script language="C#"  runat="server">
    8.  > > >
    9.  > > >     DataSet dsDepartments = new DataSet();
    10.  > > >     SqlConnection myConnection = new
    11.  > > > SqlConnection("server=dbServer;database=Inventory;User
    12.  > > > ID=appUser;Password=appPassword;");
    13.  > > >      SqlDataAdapter myCommand;
    14.  > > >
    15.  > > >
    16.  > > > private void Page_Load(object Sender, EventArgs e)
    17.  > > > {
    18.  > > >      if (ViewState["sortexpression"] == null)
    19.  > > >      {
    20.  > > >           ViewState["sortexpression"] = "deptName";
    21.  > > >           ViewState["sortorder"] = " ASC";
    22.  > > >      }
    23.  > > >      if (!IsPostBack)
    24.  > > >      {
    25.  > > >           ViewState["sortexpression"] = "deptName";
    26.  > > >           ViewState["sortorder"] = " ASC";
    27.  > > >           Response.Write("!IsPostBack");
    28.  > > >           BindGrid();
    29.  > > >      }
    30.  > > > }
    31.  > > >
    32.  > > > void doSort(object sender, DataGridSortCommandEventArgs e)
    33.  > > > {
    34.  > > >      Response.Write("doSort");
    35.  > > >      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/
    >
    >
    >

    Nov 19 '05 #5

    This discussion thread is closed

    Replies have been disabled for this discussion.