Company table in SQL server using asp.net web forms to show all the list of companies first with a parent company and sub-companies under the parent company, with a checkbox list to select the companies. In 1st SQL SP am getting a list of parent companies in a Datatable and this company id's from first data table( ex:1,2,3) to loop through to get a list of sub-companies in a second DataTable
I have tried using a nested repeater's for 2 data tables and not sure how to achieve this and which is the best control
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataTable dtx = Getparentfirm();
DataTable dso = GetSuboffice(dtx);
R1.DataSource = dtx;
R1.DataBind();
R2.DataSource = dso;
R2.DataBind();
}
}
private DataTable Getparentfirm()
{
using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=test;Integrated Security=True"))
{
SqlDataAdapter sda = new SqlDataAdapter("select * from company where parentcompany is NULL", conn);
DataTable dt = new DataTable();
sda.Fill(dt);
return dt;
}
}
private DataTable GetSuboffice(DataTable dt)
{
DataTable dtz = new DataTable();
using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=test;Integrated Security=True"))
foreach (DataRow row in dt.Rows)
{
SqlDataAdapter sda = new SqlDataAdapter("select id, fname , lname from customer where cid =" + row["cid"].ToString(), conn);
sda.Fill(dtz);
return dtz;
}
return null;
}
<asp:Repeater ID="R1" runat="server">
<ItemTemplate>
name:<%# Eval("companyname") %></td>
</ItemTemplate>
<asp:Repeater ID="R2" runat="server">
<ItemTemplate>
ID :<%# Eval("companyid") %>
Name:<%# Eval("companyname") %>
</ItemTemplate>
</asp:Repeater>
</asp:Repeater>
How to bind 2 Datatable,s to nested repeaters to get the expected output. Any help would be great
expected OUTPUT
parentcompanyid 1 parent company one checkbox
sub office 1.1 sub office one checkbox
sub office 1.2 sub office two checkbox
parent comapnyid 2 parent company two checkbox
sub office 2.1 sub office one checkbox
sub office 2.2 sub office two checkbox
When you are using a Repeater make sure all you content is within a template node, like <ItemTemplate>
- even a nested Repeater.
Once the markup is correct, you can attach an OnItemDataBound
event to the parent Repeater, then use the the ID of that item to get the data to bind the nested Repeater, then just bind it. Like this for example:
ASPX
<asp:Repeater ID="R1" runat="server" OnItemDataBound="R1_ItemDataBound">
<ItemTemplate>
<asp:HiddenField runat="server" ID="CompanyId" value='<%# Eval("id") %>' />
<div>name:<%# Eval("companyname") %></div>
<asp:Repeater ID="R2" runat="server">
<ItemTemplate>
ID :<%# Eval("companyid") %><br>
Name:<%# Eval("companyname") %>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
CS
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataTable dtx = Getparentfirm();
R1.DataSource = dtx;
R1.DataBind();
}
}
protected void R1_ItemDataBound(Object Sender, RepeaterItemEventArgs e) {
// Get companyId from hidden field
// or you could get it from e.Item.DataItem which should have the data for this row of data
var CompanyID = (HiddenField)e.Item.FindControl("CompanyId");
var id = Convert.ToInt32(CompanyID.Value);
var R2 = (Repeater)e.Item.FindControl("R2");
var dso = GetData(id); // Get sub companies based on this company id
R2.DataSource = dso;
R2.DataBind();
}
Something along those lines