Javascript function on submit button to validate if cells are emply for a particular column of gridview
I have binded a custom datatable to gridview. Now I want to validate if the value of cells of the column "DocumentsAttached" is Yes or No. If it is yes, then an alert message displaying " Documents are Attached". If No, a pop up box with message would you like to continue" Yes/No...If yes, is chosen my submit button should go through otherwise no.
Hope i made sense until now. Below is my aspx
<asp:GridView ID="UploadDocs" AutoGenerateColumns="False" runat="server"
EnableViewState="true" onrowdatabound="dgdUpload_RowDataBound" style="margin-top: 0px">
<Columns>
<asp:HyperLinkField DataTextField="DocName" HeaderText="Invoice File Name" DataNavigateUrlFields="FilePath" DataNavigateUrlFormatString="{0}">
</asp:HyperLinkField>
<asp:BoundField HeaderText="Document开发者_JS百科 Included" DataField="DocumentsAttached" ReadOnly="true" />
<asp:BoundField HeaderText="Identifier" DataField="Identifier" Visible="false"/>
<asp:CommandField ButtonType="Button" HeaderText="Delete" ShowDeleteButton="true"/>
</Columns>
</asp:GridView>
<asp:Button ID="btnSubmit" runat="server" Text="Save to MassUploadList" />
Can anyone help me to achieve this please.
Replace <asp:BoundField HeaderText="Document Included" DataField="DocumentsAttached" ReadOnly="true" />
with this:
<asp:TemplateField HeaderText="Document Included" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="lblDocumentsAttached" runat="server" Text='<%# Eval("DocumentsAttached")%>' CssClass="DocumentsAttached"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
and use this javascript to validate:
function validateAttachment() {
$(".DocumentsAttached").each(){
if($(this).val().toLowerCase()=="no") {
return false;
}
}
return true;
}
But validation this way is not a good idea since I can easily bypass it by just changing the column values in the DOM using firebug.
I would assume you're uploading something, or otherwise generating the datatable from codebehind. So why not do server side validation? If at least one row has DocumentAttached equal to "NO", disable submit button and add a custom validator to the page with relevant message.
The function that I used to serve my purpose is below
<script language="javascript" type="text/javascript">
function Validate() {
var cntrlname = document.getElementById('<%= gridview1.ClientID %>');
var gridcell;
var retVal = 1;
var result = 1;
for (i = 1; i < cntrlname.rows.length; i++) {
if (cntrlname.rows[i].cells[2].innerText == "No") {
retVal = 0;
}
if (retVal == 0) {
return window.confirm("One of the Attachments is missing, would you like to proceed")
}
else{
return true;
}
}
}
function window.confirm(str) {
execScript('n = msgbox("' + str + '","4132")', "vbscript");
return (n == 6);
}
The column documents attached is readonly, the user will not be able to make any changes to it.So , this displays a confirmation box if there any attachments missing, if the user is ok ...it will execute the server side code otherwise will not.
精彩评论