manish kedar

manish kedar

  • NA
  • 229
  • 11.5k

Validation on Gridview

Dec 4 2017 8:12 AM
Hello sir,
 
I am trying to validate on gridview textbox and dropdownlist if the checkbox is checked.
I tried by javascript and jquery the validation applied but the postback happed.
 
How to validate this on server side. 
 
 
jquery code:
<script type="text/javascript">
$(document).ready(function () {
$(document).on('click', '#btnSave', function () {
var trs = $('[id*=gv_Toppers]').closest('tr:not(":has(th)")');
$(trs).each(function () {
if ($(this).find('[id*=chkToppers]')[0].checked) {
var marks = $(this).find("[id*=txtMarks]").val().trim();
var rank = $(this).find("[id*=ddlRank]").val();
if (marks == "" || rank == "0") {
alert("Required");
return false;
}
}
});
});
});
</script>
source code:
<asp:GridView ID="gv_Toppers" runat="server" class="table table-striped table-bordered table-hover"
AutoGenerateColumns="false" DataKeyNames="pk_student_m" OnRowDataBound="gv_Toppers_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<div class="tooltip-demo">
<asp:ImageButton ID="imgbtnDelete" CssClass="DeleteImageClass" CommandName="Delete" Text="Edit" runat="server" ImageUrl="~/Images/deleteimg.gif" data-toggle="tooltip" data-placement="bottom" ToolTip="Delete" Height="20px" Width="20px" CommandArgument='<%# Eval("pk_student_m") %>'/>
</div>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:CheckBox ID="chkToppers" runat="server" AutoPostBack="false" onclick="javascript:CheckCheck();"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField >
<ItemTemplate>
<%--<asp:Label ID="lblSTID" runat="server" Text='<%# Bind("pk_student_m")%>'></asp:Label>--%>
<asp:HiddenField ID="hiddenField1" runat="server" Value='<%# Bind("pk_student_m")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Admission No">
<ItemTemplate>
<asp:Label ID="lblAdmissionNo" runat="server" Text='<%# Bind("Admission")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Student Name">
<ItemTemplate>
<asp:Label ID="lblStudentName" runat="server" Text='<%# Bind("StudentName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Session">
<ItemTemplate>
<asp:Label ID="lblSession" runat="server" Text='<%# Bind("Session") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Class">
<ItemTemplate>
<asp:Label ID="lblClass" runat="server" Text='<%# Bind("Class") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Marks">
<ItemTemplate>
<asp:TextBox ID="txtMarks" runat="server" CssClass="form-control" ToolTip="ObtainMarks/TotalMarks" PlaceHolder="Ex.- 250/500" ></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Rank">
<ItemTemplate>
<asp:DropDownList ID="ddlRank" runat="server" CssClass="form-control">
<asp:ListItem Text="Select Rank" Value="0" Selected="True"/>
<asp:ListItem Text="First Rank" Value="1st" />
<asp:ListItem Text="Second Rank" Value="2nd"/>
<asp:ListItem Text="Third Rank" Value="3rd" />
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btnSave" runat="server" Text="Save" CssClass="btn btn-primary" ClientIDMode="Static" Visible="false" OnClick="btnSave_Click"/>
 

Answers (1)