0% found this document useful (0 votes)
70 views

How To Give Serial Number: Headertext

This document provides instructions for adding serial numbers to rows in an ASP.NET grid view, adding edit and delete buttons, and handling the row commands to update or delete data. It shows how to: 1. Add a template field to assign serial numbers automatically. 2. Add template fields with link buttons for editing and deleting, passing the unique ID as a command argument. 3. Handle the row command event to get the ID and call methods for updating or deleting the data from the database. It also provides an alternative method using a row edit command to allow editing remarks in place with text boxes and updating the database on row updating.

Uploaded by

pramodedeffdedds
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views

How To Give Serial Number: Headertext

This document provides instructions for adding serial numbers to rows in an ASP.NET grid view, adding edit and delete buttons, and handling the row commands to update or delete data. It shows how to: 1. Add a template field to assign serial numbers automatically. 2. Add template fields with link buttons for editing and deleting, passing the unique ID as a command argument. 3. Handle the row command event to get the ID and call methods for updating or deleting the data from the database. It also provides an alternative method using a row edit command to allow editing remarks in place with text boxes and updating the database on row updating.

Uploaded by

pramodedeffdedds
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

How to give serial Number


<asp:TemplateField HeaderText="Sno.">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>

2. aspx page-just create command button

<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkEdit" ForeColor="#b2b3b5" runat="server"
CommandName="edit" CommandArgument='<%# Eval("contactid") %>'
CausesValidation="false" >Edit</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkDelete" ForeColor="#b2b3b5" runat="server"
CommandName="delete" CommandArgument='<%# Eval("contactid") %>'
CausesValidation="false" >Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>

3. aspx.cs page:

protected void grdContactList_RowDataBound(object sender, GridViewRowEventArgs e)


{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton l = (LinkButton)e.Row.FindControl("lnkDelete");
l.Attributes.Add("onclick", "javascript:return confirm('Are you sure you
want to delete?')");
}
}

//edit And delete record


protected void grdContactList_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("edit"))
{
hfldContactID.Value = e.CommandArgument.ToString();
appContactList cl = new appContactList();
cl.ContactID = long.Parse(hfldContactID.Value);
cl.fetchContactDetails();
txtFName.Text = cl.FirstName;
txtLName.Text = cl.LastName;
txtEmail.Text = cl.Email;
txtContactNo.Text = cl.ContactNo;
btnAddNew.Text = "Update";
}
if (e.CommandName.Equals("delete"))
{
appContactList cl = new appContactList();
cl.ContactID = long.Parse(e.CommandArgument.ToString());
int raff=cl.deleteContact();
Response.Redirect("~/user/contact");
}
}
Another way to do using rowedit command etc.
1. .aspx page
<asp:TemplateField HeaderText="Remarks">
<EditItemTemplate>
<asp:TextBox id="txtremarks" runat="server" Text='<%#Eval("remarks")
%>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblRemark" runat="server" Text='<%#Eval("remarks")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField EditText="Add Remark" HeaderText="Add Remark"
ShowEditButton="True" />

2. .aspx.cs

protected void GVPayment_RowEditing(object sender, GridViewEditEventArgs e)


{
GVPayment.EditIndex = e.NewEditIndex;
}

protected void GVPayment_RowUpdating(object sender, GridViewUpdateEventArgs e)


{
try
{
string
pno=((HiddenField)GVPayment.Rows[e.RowIndex].FindControl("paymentno")).Value;
string uid =
((Label)GVPayment.Rows[e.RowIndex].FindControl("lbluid")).Text;
string remark =
((TextBox)GVPayment.Rows[e.RowIndex].FindControl("txtremarks")).Text;

using (SqlConnection oCon = new


SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connecti
onString"].ConnectionString))
{
string strInsertQuery = "update nsWeeklyPayment set
remarks=@remarks where uid=@uid and paymentno=@pno";
try
{
SqlCommand oCmd = new SqlCommand(strInsertQuery, oCon);
oCmd.Parameters.AddWithValue("@uid", uid);
oCmd.Parameters.AddWithValue("@remarks",remark);
oCmd.Parameters.AddWithValue("@pno", pno);
oCon.Open();
int chk = oCmd.ExecuteNonQuery();
}
catch (Exception)
{
//throw;
}
finally
{
oCon.Close();
}
}
}
catch (Exception)
{
//throw;
}
GVPayment.EditIndex = -1;
bindgride();
}
protected void GVPayment_RowCancelingEdit(object sender,
GridViewCancelEditEventArgs e)
{
GVPayment.EditIndex = -1;
bindgride();
}
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:LinkButton ID="lnkedit" runat="server"
CausesValidation="False" CommandName="Edit" CommandArgument='<%# Eval("usrid") %>'
Text="Edit"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>

You might also like