如何获取并展示要查询两次的数据,类似二级联动

josxhn 2011-10-17 09:00:44
假设有两个表:题目表 question(id,qtxt),选项表 choiceid,qid,ctxt)
其中qid表示选项对应的题目,qtxt,ctxt分别是题目和选项的文本

首先要查询所有的题目
然后根据题目查询每个题目对应的选项

展示成为

1.question1
a)choice1
b)choice2
c)choice3

2.question2
a)choice4
b)choice5
c)choice6
d)choice7

3.question3
a)choice8
b)choice9
c)choice10

...

这样,应当如何做,这几天一直在这提问,无奈愚钝,整了几天也没整出来,快到期限了急啊,求详细解决方案……谢谢啦
...全文
139 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
josxhn 2011-10-19
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 nevermore_0923 的回复:]

QuestionID是否为空。

最后dt.Load(odr);是什么?打断点跟下。
[/Quote]

找到原因了 style="display: none;" 去掉就好了

先结贴了,谢谢nevermore一直跟进此帖
弦弦 2011-10-19
  • 打赏
  • 举报
回复
QuestionID是否为空。

最后dt.Load(odr);是什么?打断点跟下。
josxhn 2011-10-19
  • 打赏
  • 举报
回复
自己UP 一下
josxhn 2011-10-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 hj850126 的回复:]

你可以参照repeater的用法,给你个例子吧,你只需要稍作修改就可以了
[/Quote]

兄弟你的网站用户名密码多少。。。。
Ny-6000 2011-10-17
  • 打赏
  • 举报
回复
建议Repeater控件,

在绑定中,再次绑定循环嵌套
弦弦 2011-10-17
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 josxhn 的回复:]

引用 2 楼 nevermore_0923 的回复:

repeater嵌套。
第一个repeater的sql语句是:
select * from qeestion.然后在repeater1的ItemDataBound事件中

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.Alt……
[/Quote]

传递参数get或者post。在load事件中Request.QueryString[]获取参数,然后,你懂了吧、
沈勇 2011-10-17
  • 打赏
  • 举报
回复
把图片的地址保存数据库或者xml文件。然后load事件中加载出地址,在前台<%=%>这样表示
josxhn 2011-10-17
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 nevermore_0923 的回复:]

repeater嵌套。
第一个repeater的sql语句是:
select * from qeestion.然后在repeater1的ItemDataBound事件中

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
……
[/Quote]

如果要想select * from question 中穿参数应该怎么做?
比如要使 id 在用户提交的某个集合 idList 中
josxhn 2011-10-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 hj850126 的回复:]

你可以参照repeater的用法,给你个例子吧,你只需要稍作修改就可以了
[/Quote]

谢谢兄弟,我研究下,能否加个QQ,如果实在不行还想向你请教下 53941606
Im_Sorry 2011-10-17
  • 打赏
  • 举报
回复
二级分类,第一个表为一级类别,第二个表中加入一个字段,记录第一个表中类别的字段,然后绑定读取,判断就好了

其实是可以写在一个表里的。 需要设置深浅,1层2层3层用,来隔开区分! 逻辑,自己想一下,你现在是两个表
建议你用第一种方法!
弦弦 2011-10-17
  • 打赏
  • 举报
回复
repeater嵌套。
第一个repeater的sql语句是:
select * from qeestion.然后在repeater1的ItemDataBound事件中

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater rptProduct = (Repeater)e.Item.FindControl("pea3");
DataRowView rowv = (DataRowView)e.Item.DataItem;
string qid= rowv["qid"].ToString();
string sql="select * from表 where qid="+qid+"";
格桑花 2011-10-17
  • 打赏
  • 举报
回复
你可以参照repeater的用法,给你个例子吧,你只需要稍作修改就可以了
josxhn 2011-10-17
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 nevermore_0923 的回复:]

repeater嵌套。
第一个repeater的sql语句是:
select * from qeestion.然后在repeater1的ItemDataBound事件中

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
……
[/Quote]

我按你的方法做了,卡在一个地方找不到问题了,就是显示出来一级数据,但是无法显示二级数据。
麻烦看看我的代码,谢谢啦!另外我希望两级数据是同时展示,而不是需要点击才展示下一级,这个应当如何处理?


public string connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\_workshop\\ASP\\AutoPaged_test_111015\\data.mdb;";//note

protected void Page_Load(object sender, EventArgs e)
{
using (OleDbConnection oledbConn = new OleDbConnection(connstr))//note
{
oledbConn.Open();
OleDbCommand oledbCmd=new OleDbCommand("SELECT * FROM [tbQuestion] WHERE tid='2'", oledbConn);
OleDbDataReader odr = oledbCmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);//note
DataTable dt = new DataTable();
dt.Load(odr);
this.prov.DataSource = dt;
this.prov.DataBind();
oledbConn.Close();
}
}


protected void prov_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)//note
{
Repeater rpt = (Repeater)e.Item.FindControl("city");
DataRowView drv = (DataRowView)e.Item.DataItem;
string QuestionID = drv["qid"].ToString();
using (OleDbConnection oledbConn = new OleDbConnection(connstr))
{
oledbConn.Open();
OleDbCommand oledbCmd = new OleDbCommand("SELECT ctxt FROM [tbChoice] WHERE qid='" + QuestionID + "'", oledbConn);
OleDbDataReader odr = oledbCmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
DataTable dt = new DataTable();
dt.Load(odr);
rpt.DataSource = dt;
rpt.DataBind();
oledbConn.Close();
}
}
}



<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" />

<table>

<tr>
<td>
题目
</td>
</tr>

<asp:Repeater ID="prov" runat="server" OnItemDataBound="prov_ItemDataBound">
<ItemTemplate>
<tr>
<td id='prov<%#Eval("qtxt") %>' type="prov">
<%#Eval("qtxt") %>
</td>
</tr>
<tr>
<td id='city<%#Eval("qid") %>' style="display: none;">

<asp:Repeater ID="city" runat="server">
<ItemTemplate>
<table>
<tr>
<td>
<%#Eval("ctxt") %>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>

</td>
</tr>
</ItemTemplate>
</asp:Repeater>

</table>

</div>
</form>
</body>
</html>

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧