0
Answer

GridView Events is not firing in ASP.NET

Bineesh  VP

Bineesh VP

10y
1.6k
1
Sir, In my Web application , some GridView events is not firing.
 
They are:-
 
Row_Deleting()
 
Row_Updating()
 
Row_CancelingEdit()
 
Row_Editing()
 
Please go through the code I given below and find out where the mistake lies.
 
and here the  html code of my application:-
 



<table class="table">
<tr>
<td>

</td>
<td>
<asp:Label ID="lblMessage" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblClassName" runat="server" Text="Class Name"></asp:Label>
</td>
<td >
<asp:TextBox ID="txtClassName" CssClass="controlWidth" runat="server"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="reqValClassName" runat="server" ControlToValidate="txtClassName" CssClass="errorVal" ErrorMessage="*"></asp:RequiredFieldValidator>
</td>
</tr>
<tr >
<td >
<asp:Label ID="lblNoOfSeats" runat="server" Text="No Of Seats"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtNoOfSeats" CssClass="controlWidth" runat="server"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="reqValNoOfSeats" ControlToValidate="txtNoOfSeats" runat="server" ErrorMessage="*" CssClass="errorVal" ToolTip="This TextBox cannot be Empty"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnSave" CssClass="button" runat="server" Text="Save"
onclick="btnSave_Click" />
</td>
<td>
<asp:Button ID="btnDelete" CssClass="button" runat="server" Text="Delete" />
<asp:Button ID="btnClear" CssClass="button" runat="server" Text="Clear"
onclick="btnClear_Click" />


</td>
</tr>
</table>
</fieldset>
<div align="center">

<asp:GridView ID="gvClass" CssClass="Grid" runat="server" BackColor="White" AutoGenerateColumns="false"
BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3"
ForeColor="Black" GridLines="Vertical"
onrowcancelingedit="gvClass_RowCancelingEdit" onrowcommand="gvClass_RowCommand"
onrowdeleting="gvClass_RowDeleting" onrowediting="gvClass_RowEditing"
onrowupdating="gvClass_RowUpdating" onselectedindexchanging="gvClass_SelectedIndexChanging"
>
<AlternatingRowStyle BackColor="#CCCCCC" />
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#808080" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
<RowStyle VerticalAlign="Middle" HorizontalAlign="Center" />

<Columns>
<asp:TemplateField HeaderText="S.I.No">
<ItemTemplate>
<%#Container.DataItemIndex+1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Class Id" Visible="false">
<ItemTemplate>
<asp:Label ID="lblClassId" runat="server" Text='<%#Eval("classId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ClassName">
<ItemTemplate>
<asp:Label ID="lblClassName" runat="server" Text='<%#Eval("className") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtClassName" runat="server" Text='<%#Eval("className") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="No of Seats">
<ItemTemplate>
<asp:Label ID="lblnoOfSeats" runat="server" Text='<%#Eval("noOfSeats") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtnoOfSeats" runat="server" Text='<%#Eval("noOfSeats") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:LinkButton ID="lnkbtnEdit" Text="Edit" CommandName="Edit" runat="server">Edit</asp:LinkButton>
<br />
<span onclick="return confirm('Are you sure you want to delete this record?')">

<asp:LinkButton ID="lnkBtnDelete" Text="Delete" runat="server" CommandName="Delete">Delete</asp:LinkButton>
</span>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="lnkBtnUpdate" CommandName="Update" runat="server">Update</asp:LinkButton>

<br />
<asp:LinkButton ID="lnkBtnCancel" CommandName="Cancel" runat="server">Cancel</asp:LinkButton>

</EditItemTemplate>

</asp:TemplateField>
</Columns>
</asp:GridView>

</div>
</div>
</form>
</body>
</html>
And the C# code to function the GridEvents :-
 
protected void gvClass_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
gvClass.PageIndex = e.NewSelectedIndex;
GridFill();
}

protected void gvClass_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gvClass.EditIndex = -1;
GridFill();
}

protected void gvClass_RowCommand(object sender, GridViewCommandEventArgs e)
{

}
public void ClassDelete(string decClassId)
{
try
{
if (sqlCon.State == ConnectionState.Closed)
{
sqlCon.Open();
}
SqlCommand cmd = new SqlCommand("ClassDelete", sqlCon);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@classId", SqlDbType.Decimal));
cmd.Parameters["@classId"].Value = Convert.ToInt32(decClassId.ToString());
SqlDataAdapter sqlDa = new SqlDataAdapter(cmd);
DataSet dSet = new DataSet();
sqlDa.Fill(dSet);
}
catch (Exception)
{

throw;
}
}
protected void gvClass_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
Label lblCLASSID = (Label)gvClass.Rows[e.RowIndex].FindControl("lblClassId");
string CLASSID = lblCLASSID.Text;
classSP spClass = new classSP();
ClassDelete(CLASSID);
HttpContext.Current.Response.Write("<script>alert('Data Deleted Succefully')</script>");
gvClass.EditIndex = -1;
GridFill();

}

protected void gvClass_RowEditing(object sender, GridViewEditEventArgs e)
{
gvClass.EditIndex = e.NewEditIndex;
GridFill();
}
public void ClassUpdate(string CLASSID, string CLASSNAME, string NoOfSEATS)
{
try
{
if (sqlCon.State == ConnectionState.Closed)
{
sqlCon.Open();
}
SqlCommand cmd = new SqlCommand("ClassEdit", sqlCon);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@classId", SqlDbType.Decimal));
cmd.Parameters.Add(new SqlParameter("@className", SqlDbType.VarChar));
cmd.Parameters.Add(new SqlParameter("@noOfSeats", SqlDbType.Int));
cmd.Parameters["@classId"].Value = Convert.ToDecimal(CLASSID.ToString());
cmd.Parameters["@className"].Value = CLASSNAME;
cmd.Parameters["@noOfSeats"].Value = Convert.ToInt32(NoOfSEATS.ToString());
SqlDataAdapter sqlda = new SqlDataAdapter(cmd);
DataSet dSet = new DataSet();
sqlda.Fill(dSet);

}
catch (Exception)
{

throw;
}
}
protected void gvClass_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Label lblCLASSID = (Label)gvClass.Rows[e.RowIndex].FindControl("lblClassId");
TextBox txtCLASSNAME = (TextBox)gvClass.Rows[e.RowIndex].FindControl("txtClassName");
TextBox txtSeats = (TextBox)gvClass.Rows[e.RowIndex].FindControl("txtnoOfSeats");
string CLID = lblCLASSID.Text;
string CLName = txtCLASSNAME.Text;
string NoSeats = txtSeats.Text;
classSP spClass = new classSP();
ClassUpdate(CLID, CLName, NoSeats);
HttpContext.Current.Response.Write("<script>alert('Data Updated Succefully')</script>");
gvClass.EditIndex = -1;
GridFill();