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">
    <ItemTemplate>
    <asp:Label ID="lblEmpID" Width="100px" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "itemcode") %>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox ID="lblEditEmpID" Width="100px" CssClass="stdControls" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "itemcode") %>'></asp:TextBox>
    </EditItemTemplate>
    <FooterTemplate>
    <asp:TextBox ID="txtAddEmpID" onkeypress="return isNumberKey(event,this)" runat="server" AutoPostBack="true" OnTextChanged="txtAddEmpID_TextChanged" CssClass="stdControls" Width="100px"></asp:TextBox>
    </FooterTemplate>
    </asp:TemplateField>
  • #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
    http://stackoverflow.com/questions/15030817/get-gridview-datakeys-values-in-javascript

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

  • #764596
    Hi,

    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.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #764609
    Hi,
    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.
    Regards,
    Shashikant Gurav
    shashikantgurav22@gmail.com


Sign In to post your comments