The Gridview events such as RowEditing, RowUpdating are added. The GridView is within a UserControl. The UserControl will be loaded dynamically in a Placeholder-Control on aspx. Everything is fine, the RowEditing event raises but the Rowupdating Event does not fire. The RowEditing event loads the page and the page loads the Usercontrol again now the gridview is in edit Mode but the GridView RowUpdating event is not fired when the update button is clicked.
sample code:
<asp:TemplateField HeaderText="Product ID">
<ItemTemplate>
<%# Eval("pid") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtpid" runat="Server" Text='<%# Eval("pid") %>' Columns="30"></asp:TextBox>
<asp:RequiredFieldValidator ID="req1" runat="Server" Text="*" ControlToValidate="txtpid"></asp:RequiredFieldValidator>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Produt Name">
<ItemTemplate>
<%# Eval("pname") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtpname" runat="Server" TextMode="MultiLine" Rows="10" Columns="50"
Text='<%# Eval("pname") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="req2" runat="Server" Text="*" ControlToValidate="txtpname"></asp:RequiredFieldValidator>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Price">
<ItemTemplate>
<%# Eval("price") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtprice" runat="Server" TextMode="MultiLine" Rows="10" Columns="50"
Text='<%# Eval("price") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" VerticalAlign="Top" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" HorizontalAlign="Left" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
<asp:Label ID="lblMessage" runat="Server" ForeColor="Red"></asp:Label>
code behind:
protected void Page_Load(object sender, EventArgs e) {
Page pageHolder = new Page();
UserControl userControl = (UserControl)pageHolder.LoadControl("~/WebUserControl2.ascx");
userControl.Controls.Add(GridView1);
holder2.Controls.Add(userControl);
if (!IsPostBack)
{
BindData();
}
}
/// <summary>
/// Bind data to the grid
/// </summary>
private void BindData()
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
SqlDataAdapter dAd = new SqlDataAdapter("select * from product", conn);
DataSet dSet = new DataSet();
try
{
dAd.Fill(dSet,"product");
GridView1.DataSource = dSet.Tables["product"].DefaultView;
GridView1.DataBind();
}
catch (Exception ee)
{
lblMessage.Text = ee.Message.ToString();
}
finally
{
dSet.Dispose();
dAd.Dispose();
conn.Close();
conn.Dispose();
}
}
/// <summary>
/// fires when edit link is clicked
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void EditRecord(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindData();
}
/// <summary>
/// fires when cancel link is clicked
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void CancelRecord(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindData();
}
/// <summary>
/// fires when update link is clicked
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void UpdateRecord(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
//int autoid = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
TextBox tPageName = (TextBox)row.FindControl("txtpid");
TextBox tPageDesc = (TextBox)row.FindControl("txtpname");
TextBox dActive = (TextBox)row.FindControl("txtprice");
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
SqlCommand dCmd = new SqlCommand();
try
{
conn.Open();
dCmd.CommandText = "spUpdateData";
//dCmd.CommandType = CommandType.StoredProcedure;
// dCmd.Parameters.Add("@AutoID", SqlDbType.Int).Value = autoid;
dCmd.CommandText = @"insert into [product](pid,pname,price)values(@pid,@pname,@price)";
dCmd.Parameters.Add("@pid", SqlDbType.Char, 10).Value = tPageName.Text.Trim();
dCmd.Parameters.Add("@pname", SqlDbType.VarChar, 50).Value = tPageDesc.Text.Trim();
dCmd.Parameters.Add("@price", SqlDbType.VarChar,50).Value = dActive.Text.Trim();
dCmd.Connection = conn;
dCmd.ExecuteNonQuery();
lblMessage.Text = "Record Updated successfully.";
// Refresh the data
GridView1.EditIndex = -1;
BindData();
}
catch (SqlException ee)
{
lblMessage.Text = ee.Message;
}
finally
{
dCmd.Dispose();
conn.Close();
conn.Dispose();
}
}