2013-10-17 73 views
4
<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> 

我想顯示一個javascript消息,如果文本框是空的..我試過這段代碼。爲驗證目的需要字段驗證被添加。然後「編輯」按鈕不被解僱。請幫助我。如何在gridview文本框中添加驗證?

這是我的網格,編輯,更新,取消和取消按鈕

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(); 
} 
+0

你能顯示你的編輯按鈕代碼嗎? –

+0

可能的重複[添加驗證器到gridview文本框,在綁定字段的編輯模式下創建](http://stackoverflow.com/questions/1524296/adding-a-validator-to-the-gridview-textbox- –

+0

我添加了我的編輯按鈕代碼.. – Sambasiva

回答

1

你可以做的就是添加一個ValidationSummery與ShowMessageBox和的ValidationGroup只是在GridView

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

而改變上述TemplateField(確保EnableClientScript = true)並確保EditButton具有相同的驗證組

<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" /> 
+0

我添加了驗證摘要,只要你喜歡...但它不是效果編輯按鈕 – Sambasiva

+0

你的EditButton聲明是怎麼樣的? –

1

只需在您的TextBox中添加一個類,然後使用jquery的class finder查找控件。然後,您可以使用javascript函數檢查TextBox.val()是否爲空。