Repeater Using Edit Update Delete in asp.net:
aspx:
<asp:Panel ID="pnl" runat="server">
<asp:Repeater ID="rptList" runat="server"
OnItemCommand="rptList_ItemCommand">
<ItemTemplate>
<table>
<tr class="gr-Brorpttr" style="background-color: #F8F8F8">
<td align="center">
<asp:Label ID="lblPriority" runat="server"
Text='<%#Eval("Priority") %>'></asp:Label>
// Edit
<asp:DropDownList Visible="false" ID="drpPriority"
runat="server">
<asp:ListItem>High</asp:ListItem>
<asp:ListItem>Medium</asp:ListItem>
<asp:ListItem>Low</asp:ListItem>
</asp:DropDownList>
</td>
<td align="center" ID="Label3" runat="server"
Text="$"></asp:Label>
<asp:Label ID="Label2" runat="server" Text='<%#Eval("Price") %>'>
</asp:Label>
</td>
<td align="center">
<asp:Label ID="Label4" runat="server"
Text='<%#Eval("QuantityRequired") %>'></asp:Label>
// Edit
<asp:TextBox ID="txtQuantity" runat="server"
Visible="false" MaxLength="2"></asp:TextBox>
</td>
<td style="width: 100px" align="center">
// Edit Link
<asp:LinkButton ID="lnkEdit" runat="server"
CommandName="edit"
CommandArgument='<%#Eval("ID")%>'>
Edit
</asp:LinkButton>
// Update Link
<asp:LinkButton Visible="false" ID="lnkUpdate" runat="server"
CommandName="update"
CommandArgument='<%#Eval("ID")%>'>
Update
</asp:LinkButton>
// CancelLink
<asp:LinkButton Visible="false" ID="lnkCancel" runat="server"
CommandName="cancel">
cancel
</asp:LinkButton>
// Delete Link
<asp:LinkButton ID="lnkDelete" runat="server"
CommandName="delete"
CommandArgument='<%#Eval("ID")%>'>
Delete
</asp:LinkButton>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</asp:Panel>
------------------------------------------------------------
aspx:cs
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
View();
}
}
// View
public void View()
{
string Select;
Select = "Select * From Property";
da = new SqlDataAdapter(Select, Conn);
da.Fill(ds);
if (ds.Tables[0].Rows.Count != 0)
{
rptList.DataSource = ds;
rptList.DataBind();
}
}
// Edit, Update ,Delete
protected void rptList_ItemCommand(object
source,RepeaterCommandEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
string Update, Delete;
// Edit, Update ,Delete Link FindControls
LinkButton lnkEd = (LinkButton)e.Item.FindControl("lnkEdit");
LinkButton lnkUp = (LinkButton)e.Item.FindControl("lnkUpdate");
LinkButton lnkCan = (LinkButton)e.Item.FindControl("lnkCancel");
DropDownList drpPri =
(DropDownList)e.Item.FindControl("drpPriority");
TextBox txtQua = (TextBox)e.Item.FindControl("txtQuantity");
// Edit
if (e.CommandName == "edit")
{
lnkUp.Visible = true;
lnkEd.Visible = false;
lnkCan.Visible = true;
drpPri.Visible = true;
txtQua.Visible = true;
}
// Cancel
if (e.CommandName == "cancel")
{
View();
}
// UpDate
if (e.CommandName == "update")
{
Update = "Update Property Set
Priority='" + drpPri.SelectedItem.Text+ "',
QuantityRequired='"+txtQua.Text+"'
Where ID='" + e.CommandArgument+"'";
Cmd = new SqlCommand(Update, Conn);
Conn.Open();
Cmd.ExecuteNonQuery();
Conn.Close();
View();
}
// Delete
if (e.CommandName == "delete")
{
Delete = "Delete From Property Where
ID='" + e.CommandArgument + "'";
Cmd = new SqlCommand(Delete, Conn);
Conn.Open();
Cmd.ExecuteNonQuery();
Conn.Close();
View();
}
}
}
-----------------------------------------------------
Screen:
//Edit,Delete
No comments:
Post a Comment