repeater nested repeater in asp.net Nested repeater :
DataBase SQL:
CREATE TABLE [dbo].[Employee](
[ID] [int] PRIMARY KEY IDENTITY(1,1) NOT NULL,
[EmpName] [varchar](100) NULL,
[EmpSalary] [varchar](100) NULL,
[Address] [varchar](100) NULL
)
CREATE TABLE [dbo].[EmpComment](
[CommentID] [int] Primary Key IDENTITY(1,1) NOT NULL,
[Emp_ComID] [int] NULL,
[CommentDescription] [varchar](max) NULL,
[CommentDate] [datetime] NULL
)
-------------------------------------
CSS Style:
<style type="text/css">
.lbl
{
font-family: Verdana;
font-size: 12px;
margin-left: 20px;
}
.Pad
{
padding: 10px,2px,0px,0px
}
.tdComment
{
border: 1px dashed #52A8FF
background-color: White;
width: 500px; padding: 5px;
}
</style>
aspx :
<div>
<div style="float: left">
<asp:Repeater ID="outerRep" runat="server"
OnItemDataBound="outerRep_ItemDataBound"
OnItemCommand="outerRep_ItemCommand">
<ItemTemplate>
<table style="background-color: Aqua; width: 600px">
<tr>
<td style="height: 90px; width: 20%; border-right: 3px solid silver"
align="center" valign="center">
<div style="margin-bottom: 0px; margin-left: 10px; margin-right: 10px;">
<img width="120px" height="100px" src="Images/1374710102_128 (20).png" />
</div>
</td>
<td>
<table>
<tr>
<td class="pad">
<asp:Label ID="lblName" runat="server" CssClass="lbl" Text="Name :"></asp:Label>
<asp:Label ID="Label3" runat="server" CssClass="lbl"
Text='<%#Eval("EmpName")%>'></asp:Label>
</td>
</tr>
<td class="pad">
<asp:Label ID="Label4" runat="server" CssClass="lbl" Text="Salary :"></asp:Labe> <asp:Label ID="Label5" runat="server" CssClass="lbl"
Text='<%#Eval("EmpSalary")%>'></asp:Label>
</td>
</tr>
<td class="pad">
<asp:Label ID="Label6" runat="server" CssClass="lbl" Text="Address :"></asp:Label>
asp:Label ID="Label7" runat="server" CssClass="lbl"
Text='<%#Eval("Address")%>'></asp:Label>
</td>
</tr>
</table>
</td> </tr>
</table>
<table style="background-color: Aqua; width: 600px; padding: 10px">
Nested Repeater :
<asp:Repeater ID="innerRep" runat="server">
<ItemTemplate>
<tr>
<td class="tdComment">
<asp:Label Style="margin-left: 130px; font-size: 12px; color: #52A8FF" ID="Label2"
runat="server" Text="Commented on:"></asp:Label>
<asp:Label Style="font-size: 12px; color: #52A8FF" ID="Label1" runat="server" Text='<%# Eval("CommentDate")%>'></asp:Label>
<br />
<asp:Label ID="hlProductName" runat="server"
Text='<%# Eval("CommentDescription")%>'></asp:Label>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
<table style="background-color: Aqua; width: 600px">
<tr>
<td>
<asp:LinkButton ID="lnkComment" Style="color: Red; margin-left: 400px"
runat="server" CommandArgument='<%#Eval("ID")%>'
CommandName="Comment">Comments</asp:LinkButton>
</td>
</tr>
<tr>
<td>
<asp:TextBox ID="txtRptComment" Style="margin-left: 30px" runat="server"
TextMode="MultiLine" Width="350px" Height="80px" Visible="false">
</asp:TextBox>
<br />
<asp:LinkButton Style="margin: 75px" ID="lnlCmtSubmit" runat="server" Visible="false"
CommandArgument='<%#Eval("ID")%>'
CommandName="CmtSubmit">Submit</asp:LinkButton>
<asp:LinkButton ID="lnkCancel" CausesValidation="false" runat="server" Visible="false" CommandArgument='<%#Eval("ID")%>'
CommandName="CmtCancel">Cancel</asp:LinkButton>
</td> </tr> </table>
<br />
<br />
</ItemTemplate>
</asp:Repeater>
</div>
</div>
----------------------------------------------
aspx:CS
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
protected void outerRep_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item &&
e.Item.ItemType == ListItemType.AlternatingItem)
return;
DataRowView drv = e.Item.DataItem as DataRowView;
Repeater innerRep = e.Item.FindControl("innerRep") as Repeater;
Inner Repeater:
innerRep.DataSource = drv.CreateChildView("CategoriesRelation");
innerRep.DataBind();
LinkButton lnkCom = (LinkButton)e.Item.FindControl("lnkComment");
LinkButton lnkComSub = (LinkButton)e.Item.FindControl("lnlCmtSubmit");
LinkButton lnkComCan = (LinkButton)e.Item.FindControl("lnkCancel");
TextBox txtCom = (TextBox)e.Item.FindControl("txtRptComment");
}
public void BindData()
{
string Select;
Select = "";
//Select Record
da = new SqlDataAdapter("SELECT * FROM Employee ORDER BY ID Desc;SELECT * FROM EmpComment EmpCom INNER JOIN Employee Emp ON
Emp.ID=EmpCom.Emp_ComID ORDER BY EmpCom.Emp_ComID DESC", Conn);
DataSet ds2 = new DataSet();
da.Fill(ds2);
ds2.Relations.Add(new DataRelation("CategoriesRelation",
ds2.Tables[0].Columns["ID"],
ds2.Tables[1].Columns["Emp_ComID"]));
outerRep.DataSource = ds2.Tables[0];
outerRep.DataBind();
}
protected void outerRep_ItemCommand(object source, RepeaterCommandEventArgs e)
{
LinkButton lnkCom = (LinkButton)e.Item.FindControl("lnkComment");
LinkButton lnkComSub = (LinkButton)e.Item.FindControl("lnlCmtSubmit");
LinkButton lnkComCan = (LinkButton)e.Item.FindControl("lnkCancel");
TextBox txtCom = (TextBox)e.Item.FindControl("txtRptComment");
Link Button CommantName and Command Argument:
if (e.CommandName == "Comment")
{
txtCom.Visible = true;
lnkComSub.Visible = true;
lnkComCan.Visible = true;
}
if (e.CommandName == "CmtSubmit")
{
string Insert;
//iNSERT Record
Insert = "Insert Into
EmpComment(Emp_ComID,CommentDate,CommentDescription)Values
('"+e.CommandArgument.ToString()+"',getdate(),'" + txtCom.Text + "')";
Cmd = new SqlCommand(Insert, Conn);
Conn.Open();
Cmd.ExecuteNonQuery();
Conn.Close();
BindData();
}
if (e.CommandName == "CmtCancel")
{
//Cancel
BindData();
}
}
Screen Nested Repeater :
//Comment
No comments:
Post a Comment