How to add confirmation to gridview textbox?

<asp:TemplateField HeaderText="Quantity">
    <ItemTemplate>
        <asp:Label ID="lbl_quantity" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"quantity") %>'>
        </asp:Label>

    </ItemTemplate>

    <EditItemTemplate>
        <asp:TextBox runat="server" ID="txtquantity" Width="90px" onkeypress="return validatenumerics(event);" Text='<%# DataBinder.Eval(Container.DataItem,"quantity") %>'></asp:TextBox>
        <asp:RequiredFieldValidator ID="RFquantity" runat="server" ControlToValidate="txtquntity" Display="None" ErrorMessage="This field is required value" ValidationGroup="quantity">
        </asp:RequiredFieldValidator>
        <ajaxToolkit:ValidatorCalloutExtender ID="VCquantity" runat="Server" TargetControlID="RFquantity"/>

    </EditItemTemplate>
</asp:TemplateField>

I want to show javascript message if textbox is empty. I tried this code. for validation purposes, the addition of a field is required. Then the “change” button does not start. Please help me.

This is my grid with an edit, update, cancel and cancel button

  protected void gvproducts_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
    gvproducts.EditIndex = -1;
    grid();

}
protected void gvproducts_RowEditing(object sender, GridViewEditEventArgs e)
{
    gvproducts.EditIndex = e.NewEditIndex;
    grid();
}
protected void gvproducts_RowUpdating(object sender, GridViewUpdateEventArgs e)
{

    TextBox txtquantity = (TextBox)gvproducts.Rows[e.RowIndex].Cells[4].Controls[1];

    int st = Convert.ToInt32(gvproducts.DataKeys[e.RowIndex].Values[0].ToString());


    objsupplyPL.quantity = Convert.ToInt32(txtquantity.Text.ToString());

    objsupplyPL.sno = st;
    DataTable scmpurchase = new DataTable();
    scmpurchase = objsupplyBAL.updatepurchase(objsupplyPL);

    ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "UpdateDetails", "alert('Update Successful');", true);
    gvproducts.EditIndex = -1;
    grid();
}
+4
source share
2 answers

What you can do is add a ValidationSummery with ShowMessageBox and ValidationGroup just above the GridView

<asp:ValidationSummary ID="ValidationSummary1" runat="server" ShowMessageBox="true" ShowSummary="false" ValidationGroup="quantity" />

And change the TemplateField to this (make sure EnableClientScript = true) and make sure the EditButton has the same validationgroup

<asp:TemplateField HeaderText="Quantity">
    <ItemTemplate>
        <asp:Label ID="lbl_quantity" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"quantity") %>' />
    </ItemTemplate>
    <EditItemTemplate>
        <asp:TextBox runat="server" ID="txtquantity" Width="90px" Text='<%# DataBinder.Eval(Container.DataItem,"quantity") %>' ValidationGroup="quantity"  />
        <asp:RequiredFieldValidator ID="RFquantity" runat="server" ControlToValidate="txtquantity" EnableClientScript="true"  Display="None" ErrorMessage="This field is required value" ValidationGroup="quantity" />
    </EditItemTemplate>
</asp:TemplateField>        
<asp:CommandField ShowEditButton="True" ValidationGroup="quantity" />
+1
source

TextBox finder jquery. , TextBox.val() javascript.

+1

All Articles