CustomValidator issue -- Maybe better location here to post the following:

Discussion in 'ASP .Net Web Controls' started by jmclej@hotmail.com, Oct 19, 2005.

  1. Guest

    Here is what I have :


    <asp:templatecolumn HeaderText="HrBegin">
    <itemtemplate>
    <asp:Label id="lblHrBegin" runat="server" Text='<%#
    DataBinder.Eval(Container.DataItem, "HrBegin") %>' Width="100%">
    </asp:label>
    </itemtemplate>
    <footertemplate>
    <asp:textbox id="tbHrBeginInsert" runat="server"
    Width="100%"></asp:textbox>
    <asp:CustomValidator id="Customvalidator1"
    ControlToValidate="tbHrBeginInsert" OnServerValidate="ServerValidation"

    Display="None" runat="server"></asp:CustomValidator>
    </footertemplate>
    <edititemtemplate>
    <asp:textbox id="tbHrBegin" runat="server" Text='<%#
    DataBinder.Eval(Container, "DataItem.HrBegin") %>'
    Width="100%"></asp:textbox>
    <asp:CustomValidator id="Customvalidator2"
    ControlToValidate="tbHrBegin" OnServerValidate="ServerValidation"
    Display="None" runat="server"></asp:CustomValidator>
    </edititemtemplate>
    </asp:templatecolumn>


    Code Behind:


    void ServerValidation(object source, ServerValidateEventArgs args) {
    try
    {
    Regex r = new
    Regex("(([0-1][0-9])|(2[0-3])):([0-5][0-9]):([0-5][0-9])"); //Only
    allow "02:03:04"
    Match m = r.Match(args.Value);
    // Test whether the value entered into the text box is in a
    correct
    hour format.
    if (m.Success)
    args.IsValid = true;
    else
    args.IsValid = false;


    }


    catch(Exception ex)
    {
    args.IsValid = false;


    }
    }


    I put the ServerValidation() function on the code behind because I
    thought that this was the way of doing server-side validation. But it
    actually works only if I put this function inside a script block on the

    aspx page, which looks like then client-side validation, no? Maybe I
    need to add something in the InitializeComponent() method?
    But anyway, even like this there is still something that is not good.
    It's that I get all the time the same text to be validated, that is the

    content of my itemtemplate. What should I do to make ServerValidation()

    to test the new data entered in the textbox tbHrBeginInsert or
    tbHrBegin?
    Thank you very much for your help.
    JULIEN
    , Oct 19, 2005
    #1
    1. Advertising

  2. RE: CustomValidator issue -- Maybe better location here to post the fo

    I tried your code:
    1) it works in the codebehind just as fine. You just might needed to
    declare the method protected,

    2) The reason it validates the same data instead of the entered data is that
    you are probably databinding during the page_load. You should databind only
    if(!Page.IsPostBack )

    Here is my modification of your code:
    private void Page_Load(object sender, System.EventArgs e)
    {
    if(!Page.IsPostBack ) DataGrid_Databind();
    }
    private void DataGrid_Databind()
    {
    DataTable dt = new DataTable("Company");
    DataRow dr;
    dt.Columns.Add( new DataColumn("ID", typeof(Int32)));
    dt.Columns.Add(new DataColumn("Company", typeof(string)));
    dt.Columns.Add(new DataColumn("HrBegin", typeof(string)));
    for(int i=1;i<10;i++)
    {
    dr = dt.NewRow ();
    dr[0] = i;
    dr[1] = "Company " +i;
    dr[2] = System.DateTime.Now.ToShortTimeString ();
    dt.Rows.Add (dr);
    }
    datagrid1.DataSource = dt;
    datagrid1.DataBind ();
    }

    protected void ServerValidation(object source, ServerValidateEventArgs args)
    {
    try
    {
    Regex r = new
    Regex("(([0-1][0-9])|(2[0-3])):([0-5][0-9]):([0-5][0-9])"); //Only allow
    "02:03:04"
    Match m = r.Match(args.Value);
    // Test whether the value entered into the text box is in a correct hour
    format.
    if (m.Success)
    args.IsValid = true;
    else
    args.IsValid = false;
    }
    catch(Exception ex)
    {
    args.IsValid = false;
    Response.Write (ex.ToString() );
    }
    }
    --
    HTH,
    Phillip Williams
    http://www.societopia.net
    http://www.webswapp.com


    "" wrote:

    > Here is what I have :
    >
    >
    > <asp:templatecolumn HeaderText="HrBegin">
    > <itemtemplate>
    > <asp:Label id="lblHrBegin" runat="server" Text='<%#
    > DataBinder.Eval(Container.DataItem, "HrBegin") %>' Width="100%">
    > </asp:label>
    > </itemtemplate>
    > <footertemplate>
    > <asp:textbox id="tbHrBeginInsert" runat="server"
    > Width="100%"></asp:textbox>
    > <asp:CustomValidator id="Customvalidator1"
    > ControlToValidate="tbHrBeginInsert" OnServerValidate="ServerValidation"
    >
    > Display="None" runat="server"></asp:CustomValidator>
    > </footertemplate>
    > <edititemtemplate>
    > <asp:textbox id="tbHrBegin" runat="server" Text='<%#
    > DataBinder.Eval(Container, "DataItem.HrBegin") %>'
    > Width="100%"></asp:textbox>
    > <asp:CustomValidator id="Customvalidator2"
    > ControlToValidate="tbHrBegin" OnServerValidate="ServerValidation"
    > Display="None" runat="server"></asp:CustomValidator>
    > </edititemtemplate>
    > </asp:templatecolumn>
    >
    >
    > Code Behind:
    >
    >
    > void ServerValidation(object source, ServerValidateEventArgs args) {
    > try
    > {
    > Regex r = new
    > Regex("(([0-1][0-9])|(2[0-3])):([0-5][0-9]):([0-5][0-9])"); //Only
    > allow "02:03:04"
    > Match m = r.Match(args.Value);
    > // Test whether the value entered into the text box is in a
    > correct
    > hour format.
    > if (m.Success)
    > args.IsValid = true;
    > else
    > args.IsValid = false;
    >
    >
    > }
    >
    >
    > catch(Exception ex)
    > {
    > args.IsValid = false;
    >
    >
    > }
    > }
    >
    >
    > I put the ServerValidation() function on the code behind because I
    > thought that this was the way of doing server-side validation. But it
    > actually works only if I put this function inside a script block on the
    >
    > aspx page, which looks like then client-side validation, no? Maybe I
    > need to add something in the InitializeComponent() method?
    > But anyway, even like this there is still something that is not good.
    > It's that I get all the time the same text to be validated, that is the
    >
    > content of my itemtemplate. What should I do to make ServerValidation()
    >
    > to test the new data entered in the textbox tbHrBeginInsert or
    > tbHrBegin?
    > Thank you very much for your help.
    > JULIEN
    >
    >
    Phillip Williams, Oct 19, 2005
    #2
    1. Advertising

  3. Guest

    Re: CustomValidator issue -- Maybe better location here to post the fo

    Thanks for your answer Phillip.
    It is true that it works better with the method being declared
    protected. But still, the footertemplate and edititemtemplate don't
    react the same way, and just adding the test on !Page.IsPostBack is not
    enough.
    I still have places where the validation is not done yet, well and a
    lot of post are done to the server which really confuse me.
    Maybe to be clearer, I can actually post my all page and see if it
    works on your environment if you don't mind too much...?
    Thanks for telling me if you have trouble the nor not.
    NB : By the way I've found a RegularExpressionValidator aspx component,
    it simplifies the code, but it doesn't work better in my code...
    JULIEN
    So here is TimeZone.aspx:
    Code:
    <%@ Register TagPrefix="PL" Namespace="Com.Cl.Capricorn.CapriMon.PL"
    Assembly="CapriMonASP" %>
    <%@ Page language="c#" Codebehind="TimeZone.aspx.cs"
    AutoEventWireup="false"
    Inherits="Com.Cl.Capricorn.CapriMon.PL.TimeZone" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <html>
    <!--#include file="inc/header.inc"-->
    <body>
    <form id="Form1" method="post" runat="server">
    <!--#include file="inc/titleimage.inc"-->
    <!--#include file="inc/sitemenu.inc"-->
    <!--#include file="inc/appmenu.inc"-->
    <div id="content">
    <div id="contentTable">
    <asp:datagrid id="TimeZoneDG" runat="server"
    AutoGenerateColumns="False" BorderColor="#CC9966"
    BorderStyle="Double" BorderWidth="1px" BackColor="White"
    CellPadding="4" PageSize="16" AllowSorting="True"
    Width="600px" AllowPaging="True">
    <footerstyle forecolor="#330099"
    backcolor="#FFFFCC"></footerstyle>
    <selecteditemstyle font-bold="True" horizontalalign="Center"
    forecolor="#663399" backcolor="#FFCC66"></selecteditemstyle>
    <edititemstyle horizontalalign="Center" forecolor="#663399"
    backcolor="#FFCC66"></edititemstyle>
    <alternatingitemstyle horizontalalign="Center"
    backcolor="AntiqueWhite"></alternatingitemstyle>
    <itemstyle horizontalalign="Center" forecolor="Black"
    backcolor="White"></itemstyle>
    <headerstyle font-size="Small" horizontalalign="Center"
    forecolor="#FFFFCC" backcolor="#990000"></headerstyle>
    <columns>
    <asp:templatecolumn>
    <headertemplate>
    <asp:linkbutton Font-Size=10 Font-Bold=True ForeColor=#0033ff
    id="btnAddNew" runat="server" CausesValidation="false"
    CommandName="Insert" Text="<img border=0
    src=images/newxp.gif>"></asp:linkbutton>
    </headertemplate>
    <itemtemplate>
    <asp:linkbutton Font-Size=10 Font-Bold=True ForeColor=#0033ff
    id="btnEdit" runat="server" Text="<img border=0
    src=images/icon-pencil.gif>" CommandName="Edit"
    CausesValidation="false"></asp:linkbutton>
    </itemtemplate>
    <footertemplate>
    <asp:linkbutton Font-Size=10 Font-Bold=True ForeColor=#0033ff
    id="btnValidNew" runat="server" CausesValidation="false"
    CommandName="CommitInsert"
    Text="<img border=0 src=images/icon-floppy.gif><a
    name='edit'/>"></asp:linkbutton>
    </footertemplate>
    <edititemtemplate>
    <asp:linkbutton Font-Size=10 Font-Bold=True ForeColor=#0033ff
    id="btnValidEdit" runat="server" Text="<img border=0
    src=images/icon-floppy.gif><a name='edit'/>"
    CommandName="Update"></asp:linkbutton>&nbsp;
    <asp:linkbutton Font-Size=10 Font-Bold=True ForeColor=#0033ff
    id="btnCancelEdit" runat="server" Text="<img border=0
    src=images/icon-pencil-x.gif>"
    CommandName="Cancel"
    CausesValidation="false"></asp:linkbutton>
    </edititemtemplate>
    </asp:templatecolumn>
    <asp:templatecolumn>
    <itemtemplate>
    <pl:confirmbutton id="theDeleteButton" runat="server"
    CommandName="Delete" Text="<img border=0
    src=images/icon-delete.gif>"></pl:confirmbutton>
    </itemtemplate>
    <footertemplate>
    <asp:linkbutton Font-Size=10 Font-Bold=True ForeColor=#0033ff
    id="btnCancelNew" runat="server" Text="<img border=0
    src=images/icon-pencil-x.gif>"
    CommandName="RollbackInsert"></asp:linkbutton>
    </footertemplate>
    <edititemtemplate>
    </edititemtemplate>
    </asp:templatecolumn>
    <asp:templatecolumn Visible="False" HeaderText="Code">
    <itemstyle horizontalalign="Center" width="40px"></itemstyle>
    <itemtemplate>
    <asp:label id=lblCdTimeZone runat="server" Text='<%#
    DataBinder.Eval(Container.DataItem, "CdTimeZone") %>' Width="100%">
    </asp:label>
    </itemtemplate>
    <footertemplate>
    <asp:textbox id="tbCdTimeZoneInsert" runat="server"
    Width="100%"></asp:textbox>
    </footertemplate>
    </asp:templatecolumn>
    <asp:templatecolumn HeaderText="TimeZone">
    <itemstyle horizontalalign="Center" width="405px"></itemstyle>
    <itemtemplate>
    <asp:Label id=lblLbTimeZone runat="server" Text='<%#
    DataBinder.Eval(Container.DataItem, "LbTimeZone") %>' Width="100%">
    </asp:label>
    </itemtemplate>
    <footertemplate>
    <asp:textbox id="tbLbTimeZoneInsert" runat="server"
    Width="100%"></asp:textbox>
    </footertemplate>
    <edititemtemplate>
    <asp:textbox id="tbLbTimeZone" runat="server" Text='<%#
    DataBinder.Eval(Container, "DataItem.LbTimeZone") %>' Width="100%">
    </asp:textbox>
    </edititemtemplate>
    </asp:templatecolumn>
    <asp:templatecolumn HeaderText="HrBegin (hh:mm:ss)">
    <itemstyle horizontalalign="Center" width="120px"></itemstyle>
    <itemtemplate>
    <asp:Label id="lblHrBegin" runat="server" Text='<%#
    DataBinder.Eval(Container.DataItem, "HrBegin") %>' Width="100%">
    </asp:label>
    </itemtemplate>
    <footertemplate>
    <asp:textbox id="tbHrBeginInsert" runat="server" Width="100%">
    </asp:textbox>
    <asp:CustomValidator ID="CustomValidator1"
    ControlToValidate="tbHrBeginInsert" Runat="server"
    OnServerValidate="ServerValidation">
    </asp:CustomValidator>
    </footertemplate>
    <edititemtemplate>
    <asp:textbox id="tbHrBegin" runat="server" Text='<%#
    DataBinder.Eval(Container, "DataItem.HrBegin") %>' Width="100%">
    </asp:textbox>
    <asp:CustomValidator ID="CustomValidator2"
    ControlToValidate="tbHrBegin" Runat="server"
    OnServerValidate="ServerValidation">
    </asp:CustomValidator>
    </edititemtemplate>
    </asp:templatecolumn>
    </columns>
    <pagerstyle horizontalalign="Center" forecolor="#330099"
    backcolor="#FFFFCC" mode="NumericPages"></pagerstyle>
    </asp:datagrid>
    <!--#include file="inc/copyright.inc"-->
    </div>
    </div>
    </form>
    </body>
    </html>
    
    And here is the code behind TimeZone.aspx.cs:
    Code:
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using Com.Calyon.Capricorn.CapriMon.DAL;
    using System.Text.RegularExpressions;
    
    namespace Com.Cl.Capricorn.CapriMon.PL
    {
    /// <summary>
    /// Summary description for TimeZone.
    /// </summary>
    public class TimeZone : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.DataGrid TimeZoneDG;
    private DataSet DSTimeZone;
    
    private void Page_Load(object sender, System.EventArgs e)
    {
    BindGrid();
    }
    
    private void BindGrid()
    {
    BindGrid(false);
    }
    
    private void BindGrid(bool showFooter)
    {
    if (ViewState["CurrentPageIndex"] != null)
    TimeZoneDG.CurrentPageIndex =
    System.Int32.Parse(ViewState["CurrentPageIndex"].ToString());
    else
    TimeZoneDG.CurrentPageIndex = 0;
    BindGrid(showFooter, TimeZoneDG.CurrentPageIndex, true);
    }
    
    private void BindGrid(int PageIndex)
    {
    BindGrid(false, PageIndex, true);
    }
    
    private void BindGrid(bool showFooter, int PageIndex)
    {
    BindGrid(showFooter, PageIndex, true);
    }
    
    private void BindGrid(bool showFooter, bool reload)
    {
    if (ViewState["CurrentPageIndex"] != null)
    TimeZoneDG.CurrentPageIndex =
    System.Int32.Parse(ViewState["CurrentPageIndex"].ToString());
    else
    TimeZoneDG.CurrentPageIndex = 0;
    BindGrid(showFooter, TimeZoneDG.CurrentPageIndex, reload);
    }
    
    private void BindGrid(bool showFooter, int PageIndex, bool
    reload)/*reload the data from the base*/
    {
    if (reload)
    {
    DSTimeZone = DataSetFactory.TimeZoneComplete;//Call a stored
    procedure that retrieves a fex row containing CdTimeZone, LbTimeZone,
    HrBegin
    TimeZoneDG.DataSource = DSTimeZone.Tables[0].DefaultView;
    }
    
    if (ViewState["Sort"] != null)
    DSTimeZone.Tables[0].DefaultView.Sort = ViewState["Sort"] as
    string;
    
    TimeZoneDG.CurrentPageIndex = PageIndex;
    ViewState.Add("CurrentPageIndex", TimeZoneDG.CurrentPageIndex);
    
    TimeZoneDG.ShowFooter = showFooter;
    TimeZoneDG.DataBind();
    }
    
    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    //
    InitializeComponent();
    base.OnInit(e);
    }
    
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.TimeZoneDG.ItemCommand += new
    System.Web.UI.WebControls.DataGridCommandEventHandler(this.TimeZoneDG_ItemCommand);
    this.TimeZoneDG.PageIndexChanged += new
    System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.TimeZoneDG_PageIndexChanged);
    this.TimeZoneDG.CancelCommand += new
    System.Web.UI.WebControls.DataGridCommandEventHandler(this.TimeZoneDG_CancelCommand);
    this.TimeZoneDG.EditCommand += new
    System.Web.UI.WebControls.DataGridCommandEventHandler(this.TimeZoneDG_EditCommand);
    this.TimeZoneDG.SortCommand += new
    System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.TimeZoneDG_SortCommand);
    this.TimeZoneDG.UpdateCommand += new
    System.Web.UI.WebControls.DataGridCommandEventHandler(this.TimeZoneDG_UpdateCommand);
    this.TimeZoneDG.DeleteCommand += new
    System.Web.UI.WebControls.DataGridCommandEventHandler(this.TimeZoneDG_DeleteCommand);
    this.Load += new System.EventHandler(this.Page_Load);
    
    }
    #endregion
    
    private void TimeZoneDG_EditCommand(object source,
    System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    if (Session["Rights"].ToString() ==
    Session["AuthorizedKeyword"].ToString())
    {
    TimeZoneDG.EditItemIndex = e.Item.ItemIndex;
    BindGrid();
    }
    else
    Response.Write("<body><script>alert(\"" + "You are not authorized
    to perform this action" + "\");</script></body>");
    
    }
    
    private void TimeZoneDG_CancelCommand(object source,
    System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    if (Session["Rights"].ToString() ==
    Session["AuthorizedKeyword"].ToString())
    {
    TimeZoneDG.EditItemIndex = -1;
    BindGrid();
    }
    else
    Response.Write("<body><script>alert(\"" + "You are not authorized
    to perform this action" + "\");</script></body>");
    
    }
    
    private void TimeZoneDG_UpdateCommand(object source,
    System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    if (Page.IsValid)
    {
    if (Session["Rights"].ToString() ==
    Session["AuthorizedKeyword"].ToString())
    {
    string CdTimeZone =
    ((Label)e.Item.FindControl("lblCdTimeZone")).Text;
    string LbTimeZone =
    Page.Request.Form.GetValues(((TextBox)e.Item.FindControl("tbLbTimeZone")).UniqueID)[0];
    string HrBegin =
    Page.Request.Form.GetValues(((TextBox)e.Item.FindControl("tbHrBegin")).UniqueID)[0];
    
    TimeZoneModifier.Update(CdTimeZone, LbTimeZone, HrBegin);//Call a
    stored procedure updating the specified row
    TimeZoneDG.EditItemIndex = -1;
    BindGrid(TimeZoneDG.CurrentPageIndex);
    }
    else
    Response.Write("<body><script>alert(\"" + "You are not authorized
    to perform this action" + "\");</script></body>");
    }
    else
    Response.Write("<body><script>alert(\"" + "The format of your time
    must be hh:mm:ss" + "\");</script></body>");
    
    }
    
    private void TimeZoneDG_ItemCommand(object source,
    System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    switch (e.CommandName)
    {
    case "Insert":
    if (Session["Rights"].ToString() ==
    Session["AuthorizedKeyword"].ToString())
    {
    TimeZoneDG.EditItemIndex=-1;
    BindGrid(true);
    }
    else
    Response.Write("<body><script>alert(\"" + "You are not authorized
    to perform this action" + "\");</script></body>");
    
    break;
    case "CommitInsert":
    if (Page.IsValid)
    {
    if (Session["Rights"].ToString() ==
    Session["AuthorizedKeyword"].ToString())
    {
    string LbTimeZone =
    Page.Request.Form.GetValues(((TextBox)e.Item.FindControl("tbLbTimeZoneInsert")).UniqueID)[0];
    string HrBegin =
    Page.Request.Form.GetValues(((TextBox)e.Item.FindControl("tbHrBeginInsert")).UniqueID)[0];
    TimeZoneModifier.Insert(LbTimeZone, HrBegin);//Call a stored
    procedure inserting the new row
    ViewState.Remove("Sort");
    TimeZoneDG.EditItemIndex = -1;
    BindGrid();
    }
    else
    Response.Write("<body><script>alert(\"" + "You are not
    authorized to perform this action" + "\");</script></body>");
    }
    else
    Response.Write("<body><script>alert(\"" + "The format of your
    time must be hh:mm:ss" + "\");</script></body>");
    
    break;
    default:
    break;
    }
    
    }
    
    private void TimeZoneDG_SortCommand(object source,
    System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
    {
    setSort(e.SortExpression);
    TimeZoneDG.EditItemIndex = -1;
    BindGrid(false);
    }
    
    private void setSort(string sortExpression)
    {
    if (ViewState["Sort"] != null && (ViewState["Sort"] as
    string).StartsWith(sortExpression))
    {
    if ((ViewState["Sort"] as string).Substring(sortExpression.Length,
    4) == " ASC")
    DSTimeZone.Tables[0].DefaultView.Sort = sortExpression + " DESC";
    else
    DSTimeZone.Tables[0].DefaultView.Sort = sortExpression + " ASC";
    }
    else
    {
    DSTimeZone.Tables[0].DefaultView.Sort = sortExpression + " ASC";
    }
    ViewState.Add("Sort", DSTimeZone.Tables[0].DefaultView.Sort);
    }
    
    private void TimeZoneDG_PageIndexChanged(object source,
    System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
    {
    TimeZoneDG.EditItemIndex = -1;
    BindGrid(false, e.NewPageIndex, false);
    }
    
    private void TimeZoneDG_DeleteCommand(object source,
    System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    if (Session["Rights"].ToString() ==
    Session["AuthorizedKeyword"].ToString())
    {
    if (ViewState["Sort"] != null)
    DSTimeZone.Tables[0].DefaultView.Sort = ViewState["Sort"] as
    string;
    string CdTimeZone =
    ((Label)e.Item.FindControl("lblCdTimeZone")).Text;
    TimeZoneModifier.Delete(CdTimeZone);//Call a stored procedure
    deleting the specified row
    TimeZoneDG.EditItemIndex = -1;
    BindGrid();
    }
    else
    Response.Write("<body><script>alert(\"" + "You are not authorized
    to perform this action" + "\");</script></body>");
    
    }
    
    protected void ServerValidation(object source,
    ServerValidateEventArgs args)
    {
    try
    {
    //Regex r = new
    Regex("(((([0-1][0-9])|(2[0-3])))|([0-9])):(([0-5][0-9])|([0-9])):(([0-5][0-9])|([0-9]))");
    //Should allow "2:3:4"
    Regex r = new
    Regex("(([0-1][0-9])|(2[0-3])):([0-5][0-9]):([0-5][0-9])"); //Only
    allow "02:03:04"
    
    Match m = r.Match(args.Value);
    
    // Test whether the value entered into the text box is in a correct
    hour format.
    if (m.Success)
    args.IsValid = true;
    else
    args.IsValid = false;
    }
    catch(Exception ex)
    {
    args.IsValid = false;
    }
    }
    
    }
    }
    
    , Oct 19, 2005
    #3
  4. Re: CustomValidator issue -- Maybe better location here to post th

    Hi Julien,

    You are welcome. However, I tried to run the code you posted but it seems
    that it has several dependencies on components that are not included. I
    could not run it as is. Is it possible to simplify your implementation to a
    concept-level code rather than the full implementation, such as it can be
    examined in less than half-an-hour? (I cannot afford to spend more than an
    hour on one code from the newsgroups)

    --
    HTH,
    Phillip Williams
    http://www.societopia.net
    http://www.webswapp.com
    Phillip Williams, Oct 20, 2005
    #4
  5. Guest

    Re: CustomValidator issue -- Maybe better location here to post th

    I understand perfectly that you can't spend all your time on solving
    pbs.
    So here is a much lighter version of my code, that can be run as is,
    which I think should work but doesn't. If you can tell why, it'd be of
    course very helpful for me.

    The aspx page :
    Code:
    <%@ Page language="c#" Codebehind="TimeZone.aspx.cs"
    AutoEventWireup="false" Inherits="TEST.TimeZone" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <body>
    <form id="Form1" method="post" runat="server">
    <asp:datagrid id="TimeZoneDG" runat="server"
    AutoGenerateColumns="False" BorderColor="#CC9966" PageSize="16"
    BorderWidth="1px" BackColor="White" CellPadding="4"
    AllowSorting="True" Width="300px" AllowPaging="True">
    <SelectedItemStyle ForeColor="#663399"
    BackColor="#FFCC66"></SelectedItemStyle>
    <EditItemStyle ForeColor="#663399"
    BackColor="#FFCC66"></EditItemStyle>
    <AlternatingItemStyle
    BackColor="AntiqueWhite"></AlternatingItemStyle>
    <ItemStyle ForeColor="Black" BackColor="White"></ItemStyle>
    <HeaderStyle ForeColor="#FFFFCC" BackColor="#990000"></HeaderStyle>
    <FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
    <Columns>
    <asp:TemplateColumn>
    <HeaderTemplate>
    <asp:button id="btnAddNew" runat="server"
    CausesValidation="false" CommandName="Insert" Text="new"></asp:button>
    </HeaderTemplate>
    <ItemTemplate>
    <asp:button id="btnEdit" runat="server" Text="edit"
    CommandName="Edit" CausesValidation="false"></asp:button>
    </ItemTemplate>
    <FooterTemplate>
    <asp:button id="btnValidNew" runat="server"
    CausesValidation="false" CommandName="CommitInsert"
    Text="valid"></asp:button>
    </FooterTemplate>
    <EditItemTemplate>
    <asp:button id="btnValidEdit" runat="server" Text="update"
    CommandName="Update"></asp:button>
    <asp:button id="btnCancelEdit" runat="server" Text="cancel edit"
    CommandName="Cancel" CausesValidation="false"></asp:button>
    </EditItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn>
    <ItemTemplate>
    <asp:Button id="theDeleteButton" runat="server"
    CommandName="Delete" Text="delete"></asp:Button>
    </ItemTemplate>
    <FooterTemplate>
    <asp:button id="btnCancelNew" runat="server" Text="rollback"
    CommandName="RollbackInsert"></asp:button>
    </FooterTemplate>
    <EditItemTemplate>
    </EditItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn Visible="False" HeaderText="Code">
    <ItemTemplate>
    <asp:label id=lblCdTimeZone runat="server" Text='<%#
    DataBinder.Eval(Container.DataItem, "CdTimeZone") %>' Width="100%">
    </asp:label>
    </ItemTemplate>
    <FooterTemplate>
    <asp:textbox id="tbCdTimeZoneInsert" runat="server"
    Width="100%"></asp:textbox>
    </FooterTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn SortExpression="LbTimeZone"
    HeaderText="TimeZone">
    <ItemTemplate>
    <asp:Label id=lblLbTimeZone runat="server" Text='<%#
    DataBinder.Eval(Container.DataItem, "LbTimeZone") %>' Width="100%">
    </asp:Label>
    </ItemTemplate>
    <FooterTemplate>
    <asp:textbox id="tbLbTimeZoneInsert" runat="server"
    Width="100%"></asp:textbox>
    </FooterTemplate>
    <EditItemTemplate>
    <asp:textbox id="tbLbTimeZone" runat="server" Text='<%#
    DataBinder.Eval(Container, "DataItem.LbTimeZone") %>' Width="100%">
    </asp:textbox>
    </EditItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="HrBegin (hh:mm:ss)">
    <ItemTemplate>
    <asp:Label id="lblHrBegin" runat="server" Text='<%#
    DataBinder.Eval(Container.DataItem, "HrBegin") %>' Width="100%">
    </asp:Label>
    </ItemTemplate>
    <FooterTemplate>
    <asp:textbox id="tbHrBeginInsert" runat="server"
    Width="100%"></asp:textbox>
    <asp:CustomValidator ID="CustomValidator1"
    ControlToValidate="tbHrBeginInsert" Runat="server"
    OnServerValidate="ServerValidation"></asp:CustomValidator>
    </FooterTemplate>
    <EditItemTemplate>
    <asp:textbox id="tbHrBegin" runat="server" Text='<%#
    DataBinder.Eval(Container, "DataItem.HrBegin") %>' Width="100%">
    </asp:textbox>
    <asp:CustomValidator ID="CustomValidator2"
    ControlToValidate="tbHrBegin" Runat="server"
    OnServerValidate="ServerValidation"></asp:CustomValidator>
    </EditItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    <PagerStyle ForeColor="#330099" BackColor="#FFFFCC"
    Mode="NumericPages"></PagerStyle>
    </asp:datagrid>
    </form>
    </body>
    </HTML>
    
    The codebehind:
    Code:
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Text.RegularExpressions;
    
    
    /// <summary>
    /// Summary description for TimeZone.
    /// </summary>
    ///
    namespace TEST
    {
    public class TimeZone : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.DataGrid TimeZoneDG;
    private DataSet DSTimeZone;
    
    private void TimeZone_Load(object sender, EventArgs e)
    {
    //If the DataGridCommandEventHandler don't fire try to set either
    //Page.EnableViewState or TimeZoneDG.EnableViewState to false...
    //TimeZoneDG.EnableViewState = false;
    BindGrid();
    }
    
    private void BindGrid()
    {
    BindGrid(false);
    }
    
    private void BindGrid(bool showFooter)
    {
    if (ViewState["CurrentPageIndex"] != null)
    TimeZoneDG.CurrentPageIndex =
    System.Int32.Parse(ViewState["CurrentPageIndex"].ToString());
    else
    TimeZoneDG.CurrentPageIndex = 0;
    BindGrid(showFooter, TimeZoneDG.CurrentPageIndex, true);
    }
    
    private void BindGrid(int PageIndex)
    {
    BindGrid(false, PageIndex, true);
    }
    
    private void BindGrid(bool showFooter, int PageIndex)
    {
    BindGrid(showFooter, PageIndex, true);
    }
    
    private void BindGrid(bool showFooter, bool reload)
    {
    if (ViewState["CurrentPageIndex"] != null)
    TimeZoneDG.CurrentPageIndex =
    System.Int32.Parse(ViewState["CurrentPageIndex"].ToString());
    else
    TimeZoneDG.CurrentPageIndex = 0;
    BindGrid(showFooter, TimeZoneDG.CurrentPageIndex, reload);
    }
    
    private void BindGrid(bool showFooter, int PageIndex, bool
    reload)/*reload the data from the database*/
    {
    if (reload)
    {
    DSTimeZone = GetTZDataSet();
    TimeZoneDG.DataSource = DSTimeZone.Tables[0].DefaultView;
    }
    
    if (ViewState["Sort"] != null)
    DSTimeZone.Tables[0].DefaultView.Sort = ViewState["Sort"] as
    string;
    
    TimeZoneDG.CurrentPageIndex = PageIndex;
    ViewState.Add("CurrentPageIndex", TimeZoneDG.CurrentPageIndex);
    
    TimeZoneDG.ShowFooter = showFooter;
    TimeZoneDG.DataBind();
    }
    
    private DataSet GetTZDataSet()
    {
    DataTable TZTable = new DataTable("TZ_TABLE");
    DataColumn CdTimeZone = new
    DataColumn("CdTimeZone",Type.GetType("System.Int32"));
    DataColumn LbTimeZone = new
    DataColumn("LbTimeZone",Type.GetType("System.String"));
    DataColumn HrBegin = new
    DataColumn("HrBegin",Type.GetType("System.String"));
    TZTable.Columns.Add(CdTimeZone);
    TZTable.Columns.Add(LbTimeZone);
    TZTable.Columns.Add(HrBegin);
    
    // Add five items.
    DataRow NewRow;
    for(int i = 0; i <5; i++)
    {
    NewRow = TZTable.NewRow();
    NewRow["CdTimeZone"] = i;
    NewRow["LbTimeZone"] = "Zone " + i;
    NewRow["HrBegin"] = "11:11:11";
    TZTable.Rows.Add(NewRow);
    }
    
    DataSet TZDataSet = new DataSet();
    TZDataSet.Tables.Add(TZTable);
    TZDataSet.Tables[0].PrimaryKey = new DataColumn[]
    {TZDataSet.Tables[0].Columns[0]};
    
    return TZDataSet;
    }
    
    private void TimeZoneModifier_Update(int CdTimeZone, string
    LbTimeZone, string HrBegin)
    {
    //Update one row in DSTimeZone
    }
    
    private void TimeZoneModifier_Insert(string LbTimeZone, string
    HrBegin)
    {
    //Insert one row in DSTimeZone
    }
    
    private void TimeZoneModifier_Delete(int CdTimeZone)
    {
    //Delete one row in DSTimeZone
    }
    
    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    //
    InitializeComponent();
    base.OnInit(e);
    }
    
    
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.TimeZoneDG.ItemCommand += new
    System.Web.UI.WebControls.DataGridCommandEventHandler(this.TimeZoneDG_ItemCommand);
    this.TimeZoneDG.PageIndexChanged += new
    System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.TimeZoneDG_PageIndexChanged);
    this.TimeZoneDG.CancelCommand += new
    System.Web.UI.WebControls.DataGridCommandEventHandler(this.TimeZoneDG_CancelCommand);
    this.TimeZoneDG.EditCommand += new
    System.Web.UI.WebControls.DataGridCommandEventHandler(this.TimeZoneDG_EditCommand);
    this.TimeZoneDG.SortCommand += new
    System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.TimeZoneDG_SortCommand);
    this.TimeZoneDG.UpdateCommand += new
    System.Web.UI.WebControls.DataGridCommandEventHandler(this.TimeZoneDG_UpdateCommand);
    this.TimeZoneDG.DeleteCommand += new
    System.Web.UI.WebControls.DataGridCommandEventHandler(this.TimeZoneDG_DeleteCommand);
    this.Load += new System.EventHandler(this.TimeZone_Load);
    
    }
    #endregion
    
    
    
    private void setSort(string sortExpression)
    {
    if (ViewState["Sort"] != null && (ViewState["Sort"] as
    string).StartsWith(sortExpression))
    {
    if ((ViewState["Sort"] as string).Substring(sortExpression.Length,
    4) == " ASC")
    DSTimeZone.Tables[0].DefaultView.Sort = sortExpression + " DESC";
    else
    DSTimeZone.Tables[0].DefaultView.Sort = sortExpression + " ASC";
    }
    else
    {
    DSTimeZone.Tables[0].DefaultView.Sort = sortExpression + " ASC";
    }
    ViewState.Add("Sort", DSTimeZone.Tables[0].DefaultView.Sort);
    }
    
    
    protected void ServerValidation(object source,
    ServerValidateEventArgs args)
    {
    try
    {
    Regex r = new
    Regex("(([0-1][0-9])|(2[0-3])):([0-5][0-9]):([0-5][0-9])"); //Only
    allow "02:03:04" Format
    Match m = r.Match(args.Value);
    // Test whether the value entered into the text box is in a correct
    hour format.
    if (m.Success)
    args.IsValid = true;
    else
    args.IsValid = false;
    }
    catch(Exception ex)
    {
    args.IsValid = false;
    }
    }
    
    private void TimeZoneDG_CancelCommand(object source,
    System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    TimeZoneDG.EditItemIndex = -1;
    BindGrid();
    }
    
    private void TimeZoneDG_DeleteCommand(object source,
    System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    if (ViewState["Sort"] != null)
    DSTimeZone.Tables[0].DefaultView.Sort = ViewState["Sort"] as
    string;
    string CdTimeZone =
    ((Label)e.Item.FindControl("lblCdTimeZone")).Text;
    TimeZoneModifier_Delete(Int32.Parse(CdTimeZone));
    TimeZoneDG.EditItemIndex = -1;
    BindGrid();
    }
    
    private void TimeZoneDG_EditCommand(object source,
    System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    TimeZoneDG.EditItemIndex = e.Item.ItemIndex;
    BindGrid();
    }
    
    private void TimeZoneDG_ItemCommand(object source,
    System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    switch (e.CommandName)
    {
    case "Insert":
    TimeZoneDG.EditItemIndex=-1;
    BindGrid(true);
    break;
    case "CommitInsert":
    if (Page.IsValid)
    {
    string LbTimeZone =
    Page.Request.Form.GetValues(((TextBox)e.Item.FindControl("tbLbTimeZoneInsert")).UniqueID)[0];
    
    string HrBegin =
    Page.Request.Form.GetValues(((TextBox)e.Item.FindControl("tbHrBeginInsert")).UniqueID)[0];
    
    TimeZoneModifier_Insert(LbTimeZone, HrBegin);
    ViewState.Remove("Sort");
    TimeZoneDG.EditItemIndex = -1;
    BindGrid();
    }
    else
    Response.Write("<body><script>alert(\"" + "The format of your
    time must be hh:mm:ss" + "\");</script></body>");
    break;
    default:
    break;
    }
    }
    
    private void TimeZoneDG_SortCommand(object source,
    System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
    {
    setSort(e.SortExpression);
    TimeZoneDG.EditItemIndex = -1;
    BindGrid(false);
    }
    
    private void TimeZoneDG_UpdateCommand(object source,
    System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    if (Page.IsValid)
    {
    string CdTimeZone =
    ((Label)e.Item.FindControl("lblCdTimeZone")).Text;
    string LbTimeZone =
    Page.Request.Form.GetValues(((TextBox)e.Item.FindControl("tbLbTimeZone")).UniqueID)[0];
    
    string HrBegin =
    Page.Request.Form.GetValues(((TextBox)e.Item.FindControl("tbHrBegin")).UniqueID)[0];
    
    TimeZoneModifier_Update(Int32.Parse(CdTimeZone), LbTimeZone,
    HrBegin);
    TimeZoneDG.EditItemIndex = -1;
    BindGrid(TimeZoneDG.CurrentPageIndex);
    }
    else
    Response.Write("<body><script>alert(\"" + "The format of your time
    must be hh:mm:ss" + "\");</script></body>");
    }
    
    private void TimeZoneDG_PageIndexChanged(object source,
    System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
    {
    TimeZoneDG.EditItemIndex = -1;
    BindGrid(false, e.NewPageIndex, false);
    }
    
    }
    }
    
    , Oct 20, 2005
    #5
  6. Re: CustomValidator issue -- Maybe better location here to post th

    Hi Julien,

    I cut and paste your code in my project, ran it, nothing happened when I
    clicked on the buttons so I went inside the load eventhandler and changed it
    to:

    private void TimeZone_Load(object sender, EventArgs e)
    {
    if (!Page.IsPostBack ) BindGrid();
    else Page.Validate ();
    }


    Then the buttons worked. I clicked on edit, typed an entry, clicked update,
    got the validation error message, clicked on New, got a new row, typed a new
    time, clicked valid, and got the validation message.

    Could it be the Page.Validate() that you were missing in your full
    implementation?

    --
    HTH,
    Phillip Williams
    http://www.societopia.net
    http://www.webswapp.com


    "" wrote:

    > I understand perfectly that you can't spend all your time on solving
    > pbs.
    > So here is a much lighter version of my code, that can be run as is,
    > which I think should work but doesn't. If you can tell why, it'd be of
    > course very helpful for me.
    >
    > The aspx page :
    >
    Code:
    > <%@ Page language="c#" Codebehind="TimeZone.aspx.cs"
    > AutoEventWireup="false" Inherits="TEST.TimeZone" %>
    > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    > <HTML>
    > 	<body>
    > 		<form id="Form1" method="post" runat="server">
    > 			<asp:datagrid id="TimeZoneDG" runat="server"
    > AutoGenerateColumns="False" BorderColor="#CC9966" PageSize="16"
    > 				BorderWidth="1px" BackColor="White" CellPadding="4"
    > AllowSorting="True" Width="300px" AllowPaging="True">
    > 				<SelectedItemStyle ForeColor="#663399"
    > BackColor="#FFCC66"></SelectedItemStyle>
    > 				<EditItemStyle ForeColor="#663399"
    > BackColor="#FFCC66"></EditItemStyle>
    > 				<AlternatingItemStyle
    > BackColor="AntiqueWhite"></AlternatingItemStyle>
    > 				<ItemStyle ForeColor="Black" BackColor="White"></ItemStyle>
    > 				<HeaderStyle ForeColor="#FFFFCC" BackColor="#990000"></HeaderStyle>
    > 				<FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
    > 				<Columns>
    > 					<asp:TemplateColumn>
    > 						<HeaderTemplate>
    > 							<asp:button id="btnAddNew" runat="server"
    > CausesValidation="false" CommandName="Insert" Text="new"></asp:button>
    > 						</HeaderTemplate>
    > 						<ItemTemplate>
    > 							<asp:button id="btnEdit" runat="server" Text="edit"
    > CommandName="Edit" CausesValidation="false"></asp:button>
    > 						</ItemTemplate>
    > 						<FooterTemplate>
    > 							<asp:button id="btnValidNew" runat="server"
    > CausesValidation="false" CommandName="CommitInsert"
    > 								Text="valid"></asp:button>
    > 						</FooterTemplate>
    > 						<EditItemTemplate>
    > 							<asp:button id="btnValidEdit" runat="server" Text="update"
    > CommandName="Update"></asp:button>
    > 							<asp:button id="btnCancelEdit" runat="server" Text="cancel edit"
    > CommandName="Cancel" CausesValidation="false"></asp:button>
    > 						</EditItemTemplate>
    > 					</asp:TemplateColumn>
    > 					<asp:TemplateColumn>
    > 						<ItemTemplate>
    > 							<asp:Button id="theDeleteButton" runat="server"
    > CommandName="Delete" Text="delete"></asp:Button>
    > 						</ItemTemplate>
    > 						<FooterTemplate>
    > 							<asp:button id="btnCancelNew" runat="server" Text="rollback"
    > CommandName="RollbackInsert"></asp:button>
    > 						</FooterTemplate>
    > 						<EditItemTemplate>
    > 						</EditItemTemplate>
    > 					</asp:TemplateColumn>
    > 					<asp:TemplateColumn Visible="False" HeaderText="Code">
    > 						<ItemTemplate>
    > 							<asp:label id=lblCdTimeZone runat="server" Text='<%#
    > DataBinder.Eval(Container.DataItem, "CdTimeZone") %>' Width="100%">
    > 							</asp:label>
    > 						</ItemTemplate>
    > 						<FooterTemplate>
    > 							<asp:textbox id="tbCdTimeZoneInsert" runat="server"
    > Width="100%"></asp:textbox>
    > 						</FooterTemplate>
    > 					</asp:TemplateColumn>
    > 					<asp:TemplateColumn SortExpression="LbTimeZone"
    > HeaderText="TimeZone">
    > 						<ItemTemplate>
    > 							<asp:Label id=lblLbTimeZone runat="server" Text='<%#
    > DataBinder.Eval(Container.DataItem, "LbTimeZone") %>' Width="100%">
    > 							</asp:Label>
    > 						</ItemTemplate>
    > 						<FooterTemplate>
    > 							<asp:textbox id="tbLbTimeZoneInsert" runat="server"
    > Width="100%"></asp:textbox>
    > 						</FooterTemplate>
    > 						<EditItemTemplate>
    > 							<asp:textbox id="tbLbTimeZone" runat="server" Text='<%#
    > DataBinder.Eval(Container, "DataItem.LbTimeZone") %>' Width="100%">
    > 							</asp:textbox>
    > 						</EditItemTemplate>
    > 					</asp:TemplateColumn>
    > 					<asp:TemplateColumn HeaderText="HrBegin (hh:mm:ss)">
    > 						<ItemTemplate>
    > 							<asp:Label id="lblHrBegin" runat="server" Text='<%#
    > DataBinder.Eval(Container.DataItem, "HrBegin") %>' Width="100%">
    > 							</asp:Label>
    > 						</ItemTemplate>
    > 						<FooterTemplate>
    > 							<asp:textbox id="tbHrBeginInsert" runat="server"
    > Width="100%"></asp:textbox>
    > 							<asp:CustomValidator ID="CustomValidator1"
    > ControlToValidate="tbHrBeginInsert" Runat="server"
    > OnServerValidate="ServerValidation"></asp:CustomValidator>
    > 						</FooterTemplate>
    > 						<EditItemTemplate>
    > 							<asp:textbox id="tbHrBegin" runat="server" Text='<%#
    > DataBinder.Eval(Container, "DataItem.HrBegin") %>' Width="100%">
    > 							</asp:textbox>
    > 							<asp:CustomValidator ID="CustomValidator2"
    > ControlToValidate="tbHrBegin" Runat="server"
    > OnServerValidate="ServerValidation"></asp:CustomValidator>
    > 						</EditItemTemplate>
    > 					</asp:TemplateColumn>
    > 				</Columns>
    > 				<PagerStyle ForeColor="#330099" BackColor="#FFFFCC"
    > Mode="NumericPages"></PagerStyle>
    > 			</asp:datagrid>
    > 		</form>
    > 	</body>
    > </HTML>
    > 
    >
    > The codebehind:
    >
    Code:
    > using System;
    > using System.Collections;
    > using System.ComponentModel;
    > using System.Data;
    > using System.Drawing;
    > using System.Web;
    > using System.Web.SessionState;
    > using System.Web.UI;
    > using System.Web.UI.WebControls;
    > using System.Web.UI.HtmlControls;
    > using System.Text.RegularExpressions;
    >
    >
    > 	/// <summary>
    > 	/// Summary description for TimeZone.
    > 	/// </summary>
    > 	///
    > namespace TEST
    > {
    > 	public class TimeZone : System.Web.UI.Page
    > 	{
    > 		protected System.Web.UI.WebControls.DataGrid TimeZoneDG;
    > 		private DataSet DSTimeZone;
    >
    > 		private void TimeZone_Load(object sender, EventArgs e)
    > 		{
    > 			//If the DataGridCommandEventHandler don't fire try to set either
    > 			//Page.EnableViewState or TimeZoneDG.EnableViewState to false...
    > 			//TimeZoneDG.EnableViewState = false;
    > 			BindGrid();
    > 		}
    >
    > 		private void BindGrid()
    > 		{
    > 			BindGrid(false);
    > 		}
    >
    > 		private void BindGrid(bool showFooter)
    > 		{
    > 			if (ViewState["CurrentPageIndex"] != null)
    > 				TimeZoneDG.CurrentPageIndex =
    > 					System.Int32.Parse(ViewState["CurrentPageIndex"].ToString());
    > 			else
    > 				TimeZoneDG.CurrentPageIndex = 0;
    > 			BindGrid(showFooter, TimeZoneDG.CurrentPageIndex, true);
    > 		}
    >
    > 		private void BindGrid(int PageIndex)
    > 		{
    > 			BindGrid(false, PageIndex, true);
    > 		}
    >
    > 		private void BindGrid(bool showFooter, int PageIndex)
    > 		{
    > 			BindGrid(showFooter, PageIndex, true);
    > 		}
    >
    > 		private void BindGrid(bool showFooter, bool reload)
    > 		{
    > 			if (ViewState["CurrentPageIndex"] != null)
    > 				TimeZoneDG.CurrentPageIndex =
    > 					System.Int32.Parse(ViewState["CurrentPageIndex"].ToString());
    > 			else
    > 				TimeZoneDG.CurrentPageIndex = 0;
    > 			BindGrid(showFooter, TimeZoneDG.CurrentPageIndex, reload);
    > 		}
    >
    > 		private void BindGrid(bool showFooter, int PageIndex, bool
    > reload)/*reload the data from the database*/
    > 		{
    > 			if (reload)
    > 			{
    > 				DSTimeZone = GetTZDataSet();
    > 				TimeZoneDG.DataSource = DSTimeZone.Tables[0].DefaultView;
    > 			}
    >
    > 			if (ViewState["Sort"] != null)
    > 				DSTimeZone.Tables[0].DefaultView.Sort = ViewState["Sort"] as
    > string;
    >
    > 			TimeZoneDG.CurrentPageIndex = PageIndex;
    > 			ViewState.Add("CurrentPageIndex", TimeZoneDG.CurrentPageIndex);
    >
    > 			TimeZoneDG.ShowFooter = showFooter;
    > 			TimeZoneDG.DataBind();
    > 		}
    >
    > 		private DataSet GetTZDataSet()
    > 		{
    > 			DataTable TZTable = new DataTable("TZ_TABLE");
    > 			DataColumn CdTimeZone = new
    > DataColumn("CdTimeZone",Type.GetType("System.Int32"));
    > 			DataColumn LbTimeZone = new
    > DataColumn("LbTimeZone",Type.GetType("System.String"));
    > 			DataColumn HrBegin = new
    > DataColumn("HrBegin",Type.GetType("System.String"));
    > 			TZTable.Columns.Add(CdTimeZone);
    > 			TZTable.Columns.Add(LbTimeZone);
    > 			TZTable.Columns.Add(HrBegin);
    >
    > 			// Add five items.
    > 			DataRow NewRow;
    > 			for(int i = 0; i <5; i++)
    > 			{
    > 				NewRow = TZTable.NewRow();
    > 				NewRow["CdTimeZone"] = i;
    > 				NewRow["LbTimeZone"] = "Zone " + i;
    > 				NewRow["HrBegin"] = "11:11:11";
    > 				TZTable.Rows.Add(NewRow);
    > 			}
    >
    > 			DataSet TZDataSet = new DataSet();
    > 			TZDataSet.Tables.Add(TZTable);
    > 			TZDataSet.Tables[0].PrimaryKey = new DataColumn[]
    > {TZDataSet.Tables[0].Columns[0]};
    >
    > 			return TZDataSet;
    > 		}
    >
    > 		private void TimeZoneModifier_Update(int CdTimeZone, string
    > LbTimeZone, string HrBegin)
    > 		{
    > 			//Update one row in DSTimeZone
    > 		}
    >
    > 		private void TimeZoneModifier_Insert(string LbTimeZone, string
    > HrBegin)
    > 		{
    > 			//Insert one row in DSTimeZone
    > 		}
    >
    > 		private void TimeZoneModifier_Delete(int CdTimeZone)
    > 		{
    > 			//Delete one row in DSTimeZone
    > 		}
    >
    > 		#region Web Form Designer generated code
    > 		override protected void OnInit(EventArgs e)
    > 		{
    > 			//
    > 			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
    > 			//
    > 			InitializeComponent();
    > 			base.OnInit(e);
    > 		}
    >
    >
    > 		/// <summary>
    > 		/// Required method for Designer support - do not modify
    > 		/// the contents of this method with the code editor.
    > 		/// </summary>
    > 		private void InitializeComponent()
    > 		{
    > 			this.TimeZoneDG.ItemCommand += new
    > System.Web.UI.WebControls.DataGridCommandEventHandler(this.TimeZoneDG_ItemCommand);
    > 			this.TimeZoneDG.PageIndexChanged += new
    > System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.TimeZoneDG_PageIndexChanged);
    > 			this.TimeZoneDG.CancelCommand += new
    > System.Web.UI.WebControls.DataGridCommandEventHandler(this.TimeZoneDG_CancelCommand);
    > 			this.TimeZoneDG.EditCommand += new
    > System.Web.UI.WebControls.DataGridCommandEventHandler(this.TimeZoneDG_EditCommand);
    > 			this.TimeZoneDG.SortCommand += new
    > System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.TimeZoneDG_SortCommand);
    > 			this.TimeZoneDG.UpdateCommand += new
    > System.Web.UI.WebControls.DataGridCommandEventHandler(this.TimeZoneDG_UpdateCommand);
    > 			this.TimeZoneDG.DeleteCommand += new
    > System.Web.UI.WebControls.DataGridCommandEventHandler(this.TimeZoneDG_DeleteCommand);
    > 			this.Load += new System.EventHandler(this.TimeZone_Load);
    >
    > 		}
    > 		#endregion
    >
    >
    >
    > 		private void setSort(string sortExpression)
    > 		{
    > 			if (ViewState["Sort"] != null && (ViewState["Sort"] as
    > string).StartsWith(sortExpression))[/color]
    Phillip Williams, Oct 20, 2005
    #6
  7. Guest

    Re: CustomValidator issue -- Maybe better location here to post th

    Hey, thanks for your answer!
    I have tried it and it makes it work better. But I don't understand why
    I should add explicitly the Page.Validate() as the control is supposed
    to do the validation already by itself! And there is also the thing
    that if you try this code and press the delete button, you'll get an
    Page_Validators script problem. So all that is still very fuzzy.. I
    have countered the problem by having a normal textbox and then a
    function in my code behind that tests the validity of the data
    inserted. It's clean but it's a bit a shame that I canot use a control
    that is supposed to exactly this... Well if ever you have an idea that
    can explain this, I follow the subject so you can still answer anytime.
    NB : even with this simple code, it doesn't react the same depending on
    if I'm using Firefox or IE...
    Cheers,
    JULIEN
    , Oct 28, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Oliver Schalch
    Replies:
    0
    Views:
    332
    Oliver Schalch
    Oct 21, 2005
  2. Guest
    Replies:
    5
    Views:
    615
  3. Ville Vainio
    Replies:
    11
    Views:
    608
    Hamish Lawson
    Aug 10, 2004
  4. Replies:
    4
    Views:
    399
    Army1987
    Jan 27, 2008
  5. Dark Ambient

    Maybe I can ask here

    Dark Ambient, Aug 14, 2006, in forum: Ruby
    Replies:
    2
    Views:
    84
Loading...

Share This Page