You must Sign In to post a response.
  • Category: ASP.NET

    Details About Gridview controls

    Dear Friends
    in my web form i placed 2 text box inside gridview control. Now i tried to validate those textbox. when i was entered value in text box i will check with the database, if value already exist means it should be "Already Exist" otherwise went to else loop..

    this operation doesn't work in my code.. here i posted my code.. kindly check it reply me...

    c# code
    if (po != null)
    string icode = po.ITCODE;
    if (txtAddEmpID.Text == icode)
    Page.ClientScript.RegisterStartupScript(this.GetType(), "scriptkey", "<script>alert('This item already Exist for this Po No!');</script>");
    txtAddEmpID.BackColor = System.Drawing.Color.Red;
    txtAddEmpID.Text = "";
    txtAddName.Text = "";
    txtAddDesignation.Text = "";
    txtAddCity.Text = "";
    txtAddCountry.Text = "";
    txtAddval.Text = "";

    Aspx code:
    <asp:TemplateField HeaderText="ItemCode">
    <asp:Label ID="lblEmpID" Width="100px" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "itemcode") %>'></asp:Label>
    <asp:TextBox ID="lblEditEmpID" Width="100px" CssClass="stdControls" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "itemcode") %>'></asp:TextBox>
    <asp:TextBox ID="txtAddEmpID" onkeypress="return isNumberKey(event,this)" runat="server" AutoPostBack="true" OnTextChanged="txtAddEmpID_TextChanged" CssClass="stdControls" Width="100px"></asp:TextBox>
  • #764582
    code on 'TextChanged' event in code behind is always a poor idea, it will slow down the server/application performance, I think I is good if you use any client side scripting like JavaScript
    but if you want to check value against Database then fetch the database value on page load and add it as '.datakey' to gridview and then get it using javascript to show alert
    see below link for more detail

    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #764596

    Have you debug the code, whether the if condition succed without any fail, please debug the code and then let us know.

    Give respect to your work, Instead of trying to impress your boss.

    Blog :

  • #764609
    You are firing Text_Changed event from textbox which is inside gridview. That's why you need to get that particular textbox from particular row and then only check its text as follows:

    protected void txtAddEmpID_TextChanged(object sender, EventArgs e)
    TextBox t = (TextBox)sender;//... Get sender textbox
    if (t.Text == "icode")
    Page.ClientScript.RegisterStartupScript(this.GetType(), "scriptkey", "<script>alert('This item already Exist for this Po No!');</script>");
    t.Text = "";
    t.BackColor = System.Drawing.Color.Red;
    //... And So on.....
    Hope it helps.
    Shashikant Gurav

  • Sign In to post your comments