<%--OnClick="Btn_Submit_Click"--%>
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Naimish MakwanaRank #324 · 7.7K reputationPosted Jan 11, 2024, 12:31 AM EST
To validate only the first three rows of a GridView on the server side, you can use the
RowDataBoundevent. This event is triggered for each row when it’s bound to data. Inside this event, you can check the row index and apply validation only if the row index is less than 3 (since row indices start from 0). Here’s an example:protected void gvAcademicQualification_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { if (e.Row.RowIndex < 3) { // Apply validation to the first three rows TextBox txtStream = (TextBox)e.Row.FindControl("txtStream"); TextBox txtInstituteName = (TextBox)e.Row.FindControl("txtInstituteName"); TextBox txtGrade = (TextBox)e.Row.FindControl("txtGrade"); TextBox txtYearOfPassing = (TextBox)e.Row.FindControl("txtYearOfPassing"); // Add your validation logic here } } }In the above code,
FindControlis used to access the TextBox controls in the GridView1. You can replace the comment// Add your validation logic herewith your actual validation logic. Please note that this is a basic example and you might need to adjust it according to your specific requirements.Remember to wire up this event handler to the
OnRowDataBoundevent of your GridView in the ASPX markup:Thanks
Jayraj ChhayaRank #304 · 8.2K reputationPosted Jan 11, 2024, 1:27 AM EST
To solve the problem and implement validation for the first, second, and third rows of the GridView, you can follow these steps:
Btn_Submit_Click).FindControlmethod to access the TextBox controls within each row and retrieve their values for validation.Here's an example of how you can implement the server-side validation for the first, second, and third rows of the GridView:
protected void Btn_Submit_Click(object sender, EventArgs e) { // Iterate through the GridView rows for (int i = 0; i < gvAcademicQualification.Rows.Count; i++) { // Only perform validation for the first, second, and third rows if (i < 3) { GridViewRow row = gvAcademicQualification.Rows[i]; // Find the TextBox controls within the row TextBox txtStream = (TextBox)row.FindControl("txtStream"); TextBox txtInstituteName = (TextBox)row.FindControl("txtInstituteName"); TextBox txtGrade = (TextBox)row.FindControl("txtGrade"); TextBox txtYearOfPassing = (TextBox)row.FindControl("txtYearOfPassing"); // Perform validation for the desired fields if (string.IsNullOrEmpty(txtStream.Text)) { // Handle the validation error for Stream field // You can display an error message or highlight the invalid row } if (string.IsNullOrEmpty(txtInstituteName.Text)) { // Handle the validation error for Institute Name field // You can display an error message or highlight the invalid row } if (string.IsNullOrEmpty(txtGrade.Text)) { // Handle the validation error for Grade field // You can display an error message or highlight the invalid row } if (string.IsNullOrEmpty(txtYearOfPassing.Text)) { // Handle the validation error for Year of Passing field // You can display an error message or highlight the invalid row } } } // Proceed with further processing if all required rows pass the validation }Make sure to replace the
Btn_Submit_Clickevent handler with the actual event handler name used in your code.sumit sankpal62 reputationPosted Jan 11, 2024, 1:26 AM EST
if (e.Row.RowType == DataControlRowType.DataRow)
after Checking this condition it is not entering inside it.
it is going outside.
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowIndex < 3)
{
// Apply validation to the first three rows
TextBox txtStream = (TextBox)e.Row.FindControl("txtStream");
TextBox txtInstituteName = (TextBox)e.Row.FindControl("txtInstituteName");
TextBox txtGrade = (TextBox)e.Row.FindControl("txtGrade");
TextBox txtYearOfPassing = (TextBox)e.Row.FindControl("txtYearOfPassing");
// Add your validation logic here
RequiredFieldValidator rtxtStream = FindControl("rtxtStream") as RequiredFieldValidator;
RequiredFieldValidator rtxtInstituteName = FindControl("rtxtInstituteName") as RequiredFieldValidator;
RequiredFieldValidator rtxtGrade = FindControl("rtxtGrade") as RequiredFieldValidator;
RequiredFieldValidator rtxtYearOfPassing = FindControl("rtxtYearOfPassing") as RequiredFieldValidator;
// Set the Visible property to false for the RequiredFieldValidators in the last row
if (rtxtStream != null)
rtxtStream.Visible = true;
if (rtxtInstituteName != null)
rtxtInstituteName.Visible = true;
if (rtxtGrade != null)
rtxtGrade.Visible = true;
if (rtxtYearOfPassing != null)
rtxtYearOfPassing.Visible = true;
}
if(e.Row.RowIndex == 3)
{
TextBox txtStream = (TextBox)e.Row.FindControl("txtStream");
TextBox txtInstituteName = (TextBox)e.Row.FindControl("txtInstituteName");
TextBox txtGrade = (TextBox)e.Row.FindControl("txtGrade");
TextBox txtYearOfPassing = (TextBox)e.Row.FindControl("txtYearOfPassing");
// validation logic
RequiredFieldValidator rtxtStream = FindControl("rtxtStream") as RequiredFieldValidator;
RequiredFieldValidator rtxtInstituteName = FindControl("rtxtInstituteName") as RequiredFieldValidator;
RequiredFieldValidator rtxtGrade = FindControl("rtxtGrade") as RequiredFieldValidator;
RequiredFieldValidator rtxtYearOfPassing = FindControl("rtxtYearOfPassing") as RequiredFieldValidator;
// Set the Visible property to false for the RequiredFieldValidators in the last row
if (rtxtStream != null)
rtxtStream.Visible = false;
if (rtxtInstituteName != null)
rtxtInstituteName.Visible = false;
if (rtxtGrade != null)
rtxtGrade.Visible = false;
if (rtxtYearOfPassing != null)
rtxtYearOfPassing.Visible = false;
}
}