Friday 28 December 2012

compare validation in gridview asp.net


I did a sample for :

<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns="false"OnRowDataBound="GridView1_RowDataBound">
        <Columns>
          <asp:TemplateField HeaderText="Start Date">
            <ItemTemplate>
              <asp:Label runat="server" ID="Label1" Text='<%# Eval("StartDate") %>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
              <asp:TextBox runat="server" ID="TextBox1" Text='<%# Eval("StartDate","{0:d}")  %>'></asp:TextBox>
            </EditItemTemplate>
          </asp:TemplateField>
          <asp:TemplateField HeaderText="End Date">
            <ItemTemplate>
              <asp:Label runat="server" ID="Label2" Text='<%# Eval("EndDate") %>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
              <asp:TextBox runat="server" ID="TextBox2" Text='<%# Eval("EndDate","{0:d}") %>'></asp:TextBox>
            </EditItemTemplate>
          </asp:TemplateField>
          <asp:CommandField ShowEditButton="true" />
        </Columns>
</asp:GridView>

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowState == DataControlRowState.Edit)
        {
            CompareValidator cv = new CompareValidator();
            e.Row.Cells[1].Controls.Add(cv);
            cv.ControlToValidate = "TextBox2";
            cv.Type = ValidationDataType.Date;
            cv.Operator = ValidationCompareOperator.GreaterThan;
            cv.ErrorMessage = "End date should be later than start date!";
            cv.ValueToCompare = ((TextBox)e.Row.FindControl("TextBox1")).Text;
        }
    }

You can change index or anything as your real situation.

refer from asp.net forum

1 comment:

  1. Given so much information in it. its very useful .perfect explanation about Dot net framework.Thanks for your valuable information. Dot Net Training in chennai | Dot Net Training institute in velachery

    ReplyDelete

If any doubt?then please comment in my post

How to reduce angular CLI build time

 Index: ----- I am using angular cli - 7 and I am going to tell how to reduce the build time as per my knowledge. Problem: -------- Now the ...