在 DataList 元素中显示来自两个数据库表的数据 - Asp.NET
Display data from two database tables in DataList element - Asp.NET
我想在每个数据列表项中显示来自两个不同数据库表的数据,特别是有关工作申请的详细信息,然后使用申请所有者的 ID 来显示所有者的一些详细信息。谁能帮我解决这个问题。
这是数据列表的 asp.net 视图:
<asp:DataList ID="applicationsDataList" runat="server" RepeatColumns="4">
<ItemTemplate >
<div class="applicationCard">
<table>
<tr>
<td>
<asp:Label ID="lblJobIndustry" runat="server" Text=<%#Eval("jobIndustry")%>/>
</td>
</tr>
<tr>
<td>
<asp:Image runat="server" ImageUrl="/Images/temp.jpg" />
</td>
</tr>
<tr>
<td>
<asp:Label ID="LblJobPosition" runat="server" Text= <%#Eval("jobPosition")%>/>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label3" runat="server" Text=<%#Eval("firstName")%>/>
<asp:Label ID="Label5" runat="server" Text=<%#Eval("lastName")%>/>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label4" runat="server" Text=<%#Eval("jobLocation")%>/>
</td>
</tr>
</table>
</div>
</ItemTemplate>
</asp:DataList>
这是背后的代码,我不确定如何执行此操作,但给出了我的方法的想法:
string CS = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
OleDbConnection Connection = new OleDbConnection(CS);
OleDbConnection connection2 = new OleDbConnection(CS);
string sqlCom1 = "SELECT [jobSeekerID], [jobPosition], [jobIndustry] FROM [SeekerJobApplication] WHERE ([postingID] = 0)";
OleDbCommand oleCom1 = new OleDbCommand(sqlCom1, Connection);
Connection.Open();
OleDbDataReader applications = oleCom1.ExecuteReader();
while (applications.Read())
{
string jobSeekerID = applications[0].ToString();
string SqlCom2 = "SELECT [firstName], [lastName] FROM [JobSeeker] WHERE ([jobSeekerID] = " + jobSeekerID + ");";
OleDbCommand oleCom2 = new OleDbCommand(SqlCom2, connection2);
OleDbDataReader name = oleCom1.ExecuteReader();
while (name.Read())
{
string firstName = name[0].ToString();
string lastName = name[1].ToString();
}
string jobPosition = applications[1].ToString();
string jobIndustry = applications[2].ToString();
applicationsDataList.DataSource = name;
applicationsDataList.DataBind();
connection2.Close();
}
OleDbDataAdapter da = new OleDbDataAdapter(oleCom1);
DataSet dataSet = new DataSet();
da.Fill(dataSet, "SeekerJobApplication");
applicationsDataList.DataSource = applications;
applicationsDataList.DataBind();
先谢谢各位天才了!
嗯,不清楚是否要将第一个 table 中的数据显示为某种网格,然后当您单击该网格的一行时,显示该行选择的详细信息?
或者,您希望主数据 + 子数据都显示在同一个网格或视图中吗?
我的意思是,我们甚至可以说有一些数据行,然后说您点击“+”按钮来展开和显示子数据?
我发现试图嵌套两个网格视图有点混乱。但是 listview(主 table)和子 gridview 工作得很好。
所以我们有了这个标记。不是一个“大”数量,但它确实产生了一些不错的格式
所以这个:
<div style="width:44%;margin-left:30px">
<asp:ListView ID="ListView1" runat="server" DataKeyNames="ID" >
<ItemTemplate>
<tr style="">
<td><asp:Button ID="cmdView" runat="server" Text="+" OnClick="cmdView_Click" /></td>
<td><asp:Label ID="HotelNameLabel" runat="server" Text='<%# Eval("HotelName") %>' /></td>
<td><asp:Label ID="CityLabel" runat="server" Text='<%# Eval("City") %>' /></td>
<td><asp:Label ID="ProvinceLabel" runat="server" Text='<%# Eval("Province") %>' /></td>
<td><asp:Label ID="DescriptionLabel" runat="server" Text='<%# Eval("Description") %>' /></td>
</tr>
<tr>
<td colspan="5">
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" CssClass="table table-hover" style="display:none;margin-left:20px" >
<Columns>
<asp:BoundField DataField="Firstname" HeaderText="Firstname" />
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:BoundField DataField="City" HeaderText="City" />
</Columns>
</asp:GridView>
</td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table id="itemPlaceholderContainer" runat="server" class = "table table-hover" >
<tr runat="server" style="">
<th runat="server">View</th>
<th runat="server">HotelName</th>
<th runat="server">City</th>
<th runat="server">Province</th>
<th runat="server">Description</th>
</tr>
<tr id="itemPlaceholder" runat="server">
</tr>
</table>
</LayoutTemplate>
</asp:ListView>
</div>
好的,我们显示这个的代码是这样的:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
LoadGrid();
}
public void LoadGrid()
{
ListView1.DataSource = MyRst("SELECT * FROM tblHotels ORDER BY HotelName");
ListView1.DataBind();
}
我们现在有这个:
所以,现在我们只需要用于显示子数据的“+”按钮的代码。
该按钮代码如下所示:
protected void cmdView_Click(object sender, EventArgs e)
{
Button cmd = (Button)sender;
ListViewDataItem gVRow = (ListViewDataItem)cmd.Parent;
GridView gChild = (GridView)gVRow.FindControl("GridView2"); // pluck out the grid for this row
if (gChild.Style["display"] == "normal")
{
// if grid is already display, then hide it, and exit
gChild.Style["display"] = "none";
return;
}
gChild.Style["display"] = "normal";
// only re-load if never loaded
if (gChild.Rows.Count == 0)
{
int HotelPK = (int)ListView1.DataKeys[gVRow.DataItemIndex]["ID"];
gChild.DataSource = MyRst("SELECT * from tblPeople where hotel_id = " + HotelPK);
gChild.DataBind();
}
}
现在当我们点击 + 时,我们会看到:
真的很酷吗?
列表视图“记住”我们设置的样式状态(hide/show)。
因此,如果您单击展开的 +,它将隐藏。
所以,我可以自由扩展更多的行,这样说:
当然,我有一个“帮助程序”例程,它 returns 一个数据 table 用于给定的 sql(厌倦了一遍又一遍地输入该代码)。所以 MyRst() 是这样的:
public DataTable MyRst(string strSQL)
{
var rst = new DataTable();
using (OleDbConnection con = new OleDbConnection(Properties.Settings.Default.AccessDB))
{
using (OleDbCommand cmdSQL = new OleDbCommand(strSQL, con))
{
con.Open();
// fill items table
rst.Load(cmdSQL.ExecuteReader());
}
}
return rst;
}
因此,我们编写的代码量非常少。
而且我们甚至没有太多加价。
但结果非常惊人 - 扩展折叠网格,而且代码非常少。
现在,我想您可以让网格开始展开,但是在上面,我们有非常好的性能,因为我们只在单击“+”按钮时拉出 + 显示子行。甚至更好,因为列表视图确实记得它的状态?然后,如果我们展开之前已经展开的行,我不会重新加载。所以这个设置在性能方面非常好。
请注意,我确实假设包含了 bootstrap,因此包含了 class = "table table-hover"。这使得两个网格看起来非常漂亮。
我想在每个数据列表项中显示来自两个不同数据库表的数据,特别是有关工作申请的详细信息,然后使用申请所有者的 ID 来显示所有者的一些详细信息。谁能帮我解决这个问题。
这是数据列表的 asp.net 视图:
<asp:DataList ID="applicationsDataList" runat="server" RepeatColumns="4">
<ItemTemplate >
<div class="applicationCard">
<table>
<tr>
<td>
<asp:Label ID="lblJobIndustry" runat="server" Text=<%#Eval("jobIndustry")%>/>
</td>
</tr>
<tr>
<td>
<asp:Image runat="server" ImageUrl="/Images/temp.jpg" />
</td>
</tr>
<tr>
<td>
<asp:Label ID="LblJobPosition" runat="server" Text= <%#Eval("jobPosition")%>/>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label3" runat="server" Text=<%#Eval("firstName")%>/>
<asp:Label ID="Label5" runat="server" Text=<%#Eval("lastName")%>/>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label4" runat="server" Text=<%#Eval("jobLocation")%>/>
</td>
</tr>
</table>
</div>
</ItemTemplate>
</asp:DataList>
这是背后的代码,我不确定如何执行此操作,但给出了我的方法的想法:
string CS = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
OleDbConnection Connection = new OleDbConnection(CS);
OleDbConnection connection2 = new OleDbConnection(CS);
string sqlCom1 = "SELECT [jobSeekerID], [jobPosition], [jobIndustry] FROM [SeekerJobApplication] WHERE ([postingID] = 0)";
OleDbCommand oleCom1 = new OleDbCommand(sqlCom1, Connection);
Connection.Open();
OleDbDataReader applications = oleCom1.ExecuteReader();
while (applications.Read())
{
string jobSeekerID = applications[0].ToString();
string SqlCom2 = "SELECT [firstName], [lastName] FROM [JobSeeker] WHERE ([jobSeekerID] = " + jobSeekerID + ");";
OleDbCommand oleCom2 = new OleDbCommand(SqlCom2, connection2);
OleDbDataReader name = oleCom1.ExecuteReader();
while (name.Read())
{
string firstName = name[0].ToString();
string lastName = name[1].ToString();
}
string jobPosition = applications[1].ToString();
string jobIndustry = applications[2].ToString();
applicationsDataList.DataSource = name;
applicationsDataList.DataBind();
connection2.Close();
}
OleDbDataAdapter da = new OleDbDataAdapter(oleCom1);
DataSet dataSet = new DataSet();
da.Fill(dataSet, "SeekerJobApplication");
applicationsDataList.DataSource = applications;
applicationsDataList.DataBind();
先谢谢各位天才了!
嗯,不清楚是否要将第一个 table 中的数据显示为某种网格,然后当您单击该网格的一行时,显示该行选择的详细信息?
或者,您希望主数据 + 子数据都显示在同一个网格或视图中吗?
我的意思是,我们甚至可以说有一些数据行,然后说您点击“+”按钮来展开和显示子数据?
我发现试图嵌套两个网格视图有点混乱。但是 listview(主 table)和子 gridview 工作得很好。
所以我们有了这个标记。不是一个“大”数量,但它确实产生了一些不错的格式
所以这个:
<div style="width:44%;margin-left:30px">
<asp:ListView ID="ListView1" runat="server" DataKeyNames="ID" >
<ItemTemplate>
<tr style="">
<td><asp:Button ID="cmdView" runat="server" Text="+" OnClick="cmdView_Click" /></td>
<td><asp:Label ID="HotelNameLabel" runat="server" Text='<%# Eval("HotelName") %>' /></td>
<td><asp:Label ID="CityLabel" runat="server" Text='<%# Eval("City") %>' /></td>
<td><asp:Label ID="ProvinceLabel" runat="server" Text='<%# Eval("Province") %>' /></td>
<td><asp:Label ID="DescriptionLabel" runat="server" Text='<%# Eval("Description") %>' /></td>
</tr>
<tr>
<td colspan="5">
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" CssClass="table table-hover" style="display:none;margin-left:20px" >
<Columns>
<asp:BoundField DataField="Firstname" HeaderText="Firstname" />
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:BoundField DataField="City" HeaderText="City" />
</Columns>
</asp:GridView>
</td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table id="itemPlaceholderContainer" runat="server" class = "table table-hover" >
<tr runat="server" style="">
<th runat="server">View</th>
<th runat="server">HotelName</th>
<th runat="server">City</th>
<th runat="server">Province</th>
<th runat="server">Description</th>
</tr>
<tr id="itemPlaceholder" runat="server">
</tr>
</table>
</LayoutTemplate>
</asp:ListView>
</div>
好的,我们显示这个的代码是这样的:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
LoadGrid();
}
public void LoadGrid()
{
ListView1.DataSource = MyRst("SELECT * FROM tblHotels ORDER BY HotelName");
ListView1.DataBind();
}
我们现在有这个:
所以,现在我们只需要用于显示子数据的“+”按钮的代码。
该按钮代码如下所示:
protected void cmdView_Click(object sender, EventArgs e)
{
Button cmd = (Button)sender;
ListViewDataItem gVRow = (ListViewDataItem)cmd.Parent;
GridView gChild = (GridView)gVRow.FindControl("GridView2"); // pluck out the grid for this row
if (gChild.Style["display"] == "normal")
{
// if grid is already display, then hide it, and exit
gChild.Style["display"] = "none";
return;
}
gChild.Style["display"] = "normal";
// only re-load if never loaded
if (gChild.Rows.Count == 0)
{
int HotelPK = (int)ListView1.DataKeys[gVRow.DataItemIndex]["ID"];
gChild.DataSource = MyRst("SELECT * from tblPeople where hotel_id = " + HotelPK);
gChild.DataBind();
}
}
现在当我们点击 + 时,我们会看到:
真的很酷吗?
列表视图“记住”我们设置的样式状态(hide/show)。
因此,如果您单击展开的 +,它将隐藏。
所以,我可以自由扩展更多的行,这样说:
当然,我有一个“帮助程序”例程,它 returns 一个数据 table 用于给定的 sql(厌倦了一遍又一遍地输入该代码)。所以 MyRst() 是这样的:
public DataTable MyRst(string strSQL)
{
var rst = new DataTable();
using (OleDbConnection con = new OleDbConnection(Properties.Settings.Default.AccessDB))
{
using (OleDbCommand cmdSQL = new OleDbCommand(strSQL, con))
{
con.Open();
// fill items table
rst.Load(cmdSQL.ExecuteReader());
}
}
return rst;
}
因此,我们编写的代码量非常少。
而且我们甚至没有太多加价。
但结果非常惊人 - 扩展折叠网格,而且代码非常少。
现在,我想您可以让网格开始展开,但是在上面,我们有非常好的性能,因为我们只在单击“+”按钮时拉出 + 显示子行。甚至更好,因为列表视图确实记得它的状态?然后,如果我们展开之前已经展开的行,我不会重新加载。所以这个设置在性能方面非常好。
请注意,我确实假设包含了 bootstrap,因此包含了 class = "table table-hover"。这使得两个网格看起来非常漂亮。