单选,多选的投票页面

FeelTouch Labs
博客专家认证
2009-10-31 09:49:48
还得请教高手一个问题,我想做一个投票页面,有多选,单选,按钮根据问题自动生成,但是我只能实现 ,只显示第一个的B标题和选项,其他的只显示标题不显示选项,不知道代码那里有问题,我的前台和后台的代码是
<asp:DataList ID="DataList1" runat="server" Width="100%" HeaderStyle-BackColor="#CC99ff">
<ItemTemplate >
<asp:Label ID="lblTitle" runat="server"></asp:Label><%#(Eval("voitTitle"))%>
<table id="table1" border="0" width="100%">

<tr >
<td >
<asp:RadioButtonList ID="rbl" runat="server"></asp:RadioButtonList><asp:CheckBoxList ID="cbl" runat="server"></asp:CheckBoxList>
</td>

</tr>
</table>
</ItemTemplate>

</asp:DataList>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
VotingData();
}
}


private void VotingData()
{
int m = 0;
bool IsMult = false;
string strSql = "SELECT voitID,voitTitle FROM VoteItem";
string Vote_dns = WebConfigurationManager.ConnectionStrings["VoteConnectionString"].ToString();
SqlConnection conn = new SqlConnection(Vote_dns);
DataSet ds = new DataSet();
SqlDataAdapter sa = new SqlDataAdapter(strSql, conn);
sa.Fill(ds);
DataList1.DataSource = ds;
DataList1.DataBind();
conn.Open();
SqlCommand comm = new SqlCommand(strSql, conn);
SqlDataReader dr = comm.ExecuteReader();

if (dr.Read())
{
m = dr.GetInt32(0);
}
dr.Close();

for (int n = 1; n <= DataList1.Items.Count; n++)
{ //问题序号
Label lb = ((Label)DataList1.Items[n - 1].FindControl("lblTitle"));
lb.Text = n.ToString() + "、";
}
//显示答案选项
string strSqlChoice = "SELECT vochID,vochTitle FROM VoteChoice WHERE voitID=" + m;
SqlCommand chComm = new SqlCommand(strSqlChoice, conn);
SqlDataReader chDr = chComm.ExecuteReader(CommandBehavior.CloseConnection);



//多选,单选的不同显示
if (IsMult)
{


((CheckBoxList)DataList1.Items[0].FindControl("cbl")).Visible = true;
((CheckBoxList)DataList1.Items[0].FindControl("cbl")).DataSource = chDr;
((CheckBoxList)DataList1.Items[0].FindControl("cbl")).DataTextField = "vochTitle";
((CheckBoxList)DataList1.Items[0].FindControl("cbl")).DataValueField = "vochID";
((CheckBoxList)DataList1.Items[0].FindControl("cbl")).DataBind();
((RadioButtonList)DataList1.Items[0].FindControl("rbl")).Visible = false;

}
else
{


((RadioButtonList)DataList1.Items[0].FindControl("rbl")).Visible = true;
((RadioButtonList)DataList1.Items[0].FindControl("rbl")).DataSource = chDr;
((RadioButtonList)DataList1.Items[0].FindControl("rbl")).DataTextField = "vochTitle";
((RadioButtonList)DataList1.Items[0].FindControl("rbl")).DataValueField = "vochID";
((RadioButtonList)DataList1.Items[0].FindControl("rbl")).DataBind();
((CheckBoxList)DataList1.Items[0].FindControl("cbl")).Visible = false;


}

chDr.Close();
conn.Close();

}
能指点一下吗?多谢了 在线交流
...全文
369 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
FeelTouch Labs 2009-11-01
  • 打赏
  • 举报
回复
还没有解决,上面都有错误....
FeelTouch Labs 2009-11-01
  • 打赏
  • 举报
回复
感谢大家的热情帮助,如果真能实现我追加分
RUNBEAR 2009-11-01
  • 打赏
  • 举报
回复
可不可以用<%=method()%>,这样再在后台,判断之后,拼出CheckBoxList或者RadioButtonList控件的语句啊!
happy664618843 2009-11-01
  • 打赏
  • 举报
回复
楼上都回答了 友情帮顶
daichenghua 2009-11-01
  • 打赏
  • 举报
回复
<tr>
<td colspan="3" valign="top" class="sidebar" style="text-align:left;padding-left:10px" >
    <%=ballotInfo.BallotContent%><br />
<asp:RadioButtonList ID="rbtnBallotItems" runat="server">
</asp:RadioButtonList>
<asp:CheckBoxList ID="chblBallotItems" runat="server" Visible="false">
</asp:CheckBoxList><br />
<div style="text-align:center;padding-bottom:5px">
<input type="button" name="btnBallot" id="btnBallot" runat="server" style="cursor:hand" value="提交" onclick="openResult('add');" />  
<input type="button" name="btnBallotLook" id="btnBallotLook" runat="server" style="cursor:hand" value="查看" onclick="openResult('look');" /></div>
</td>
</tr>

//绑定投票
ballotInfo = ballotDAL.GetTopOne();
if (ballotInfo.BallotType != 1)
{
ballottype = "2";
rbtnBallotItems.Visible = false;
chblBallotItems.Visible = true;
for (int k = 0; k < ballotInfo.BallotItems.Count; k++)
{
values = ballotInfo.BallotItems[k].Split(',');
ListItem item = new ListItem(values[1]);
item.Attributes.Add("relat", values[0]);
chblBallotItems.Items.Add(item);
}
}
else
{
ballottype = "1";
for (int k = 0; k < ballotInfo.BallotItems.Count; k++)
{
values = ballotInfo.BallotItems[k].Split(',');
rbtnBallotItems.Items.Add(new ListItem(values[1], values[0]));
}
}

提交的页面获取值
ballotItems = Request.QueryString["ballotItems"].ToString().Split(',');
Adechen 2009-11-01
  • 打赏
  • 举报
回复

private void VotingData()
{
int[] m = new int[];
bool IsMult = false;
string strSql = "SELECT voitID,voitTitle FROM VoteItem";
string Vote_dns = WebConfigurationManager.ConnectionStrings["VoteConnectionString"].ToString();
SqlConnection conn = new SqlConnection(Vote_dns);
DataSet ds = new DataSet();
SqlDataAdapter sa = new SqlDataAdapter(strSql, conn);
sa.Fill(ds);
DataList1.DataSource = ds;
DataList1.DataBind();
conn.Open();
SqlCommand comm = new SqlCommand(strSql, conn);
using( SqlDataReader dr = comm.ExecuteReader())
{
while (dr.Read())
{
m.Add(dr.GetInt32(0));
}
}

for (int n = 1; n <= DataList1.Items.Count; n++)
{ //问题序号
Label lb = ((Label)DataList1.Items[n - 1].FindControl("lblTitle"));
lb.Text = n.ToString() + "、";
string strSqlChoice = "SELECT vochID,vochTitle FROM VoteChoice WHERE voitID=" + m[n-1];
SqlCommand chComm = new SqlCommand(strSqlChoice, conn);
SqlDataReader chDr = chComm.ExecuteReader(CommandBehavior.CloseConnection);



//多选,单选的不同显示
if (IsMult)
{


((CheckBoxList)DataList1.Items[0].FindControl("cbl")).Visible = true;
((CheckBoxList)DataList1.Items[0].FindControl("cbl")).DataSource = chDr;
((CheckBoxList)DataList1.Items[0].FindControl("cbl")).DataTextField = "vochTitle";
((CheckBoxList)DataList1.Items[0].FindControl("cbl")).DataValueField = "vochID";
((CheckBoxList)DataList1.Items[0].FindControl("cbl")).DataBind();
((RadioButtonList)DataList1.Items[0].FindControl("rbl")).Visible = false;

}
else
{


((RadioButtonList)DataList1.Items[0].FindControl("rbl")).Visible = true;
((RadioButtonList)DataList1.Items[0].FindControl("rbl")).DataSource = chDr;
((RadioButtonList)DataList1.Items[0].FindControl("rbl")).DataTextField = "vochTitle";
((RadioButtonList)DataList1.Items[0].FindControl("rbl")).DataValueField = "vochID";
((RadioButtonList)DataList1.Items[0].FindControl("rbl")).DataBind();
((CheckBoxList)DataList1.Items[0].FindControl("cbl")).Visible = false;


}

}
//显示答案选项

chDr.Close();
conn.Close();

}
wuyq11 2009-11-01
  • 打赏
  • 举报
回复
代码还是要修改下,看看链接的实例
FeelTouch Labs 2009-11-01
  • 打赏
  • 举报
回复
我试了一下,提示很多错误,也可能是我没有把你的代码放对地方.也可能这个问题确实比较难吧(我是想自动根据数据库题目是多选还是单选,而相应地显示CheckBoxList或者RadioButtonList,选项多少是不确定的,我的代码如上),吴老师看一下,再不行就不这样做了,感觉挺难的,当然,分还是会给的,呵呵
FeelTouch Labs 2009-11-01
  • 打赏
  • 举报
回复
谢谢您的回答我试下,我是想自动根据数据库题目是多选还是单选,而相应地显示CheckBoxList或者RadioButtonList
wuyq11 2009-11-01
  • 打赏
  • 举报
回复
<ItemTemplate>
<TABLE id="Table2" cellSpacing="1" cellPadding="1" width="300" border="1">
<TR>
<TD>
<asp:Label id=lbAsk runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"title") %>'>
</asp:Label></TD>
</TR>
<TR>
<TD>
<asp:RadioButtonList id=rblOption runat="server" DataSource='<%# rdlDataBind(DataBinder.Eval(Container.DataItem,"Option").ToString()) %>'>
</asp:RadioButtonList></TD>
</TR>
</TABLE>
</ItemTemplate>


public DataTable radblBind(string data)
{
DataTable dt=new DataTable();
return dt;
}

private void dlOption_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem||e.Item.ItemType==ListItemType.EditItem)
{
RadioButtonList rbt=(RadioButtonList)e.Item.FindControl("rblOption");
}
}
投票系统
FeelTouch Labs 2009-11-01
  • 打赏
  • 举报
回复
你给我说下我的.CS页面里的代码应该怎么改才好?谢谢
FeelTouch Labs 2009-11-01
  • 打赏
  • 举报
回复
是不是直接把你写的代码加到CS页面就可以了,我试了一下还是只显示第一个问题的,是这样做不?
wuyq11 2009-11-01
  • 打赏
  • 举报
回复
itemdatabound里查询控件,绑定数据
FeelTouch Labs 2009-11-01
  • 打赏
  • 举报
回复
在DataList的itemdatabound里设置事件是吗?
wuyq11 2009-10-31
  • 打赏
  • 举报
回复
在DataList的itemdatabound里设置控件
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
RadioButtonList rdb= (RadioButtonList)DataList1.Items[0].FindControl("rbl");

DataRowView drv = e.Item.DataItem as DataRowView;
DataRow dr = drv.Row;
}
FeelTouch Labs 2009-10-31
  • 打赏
  • 举报
回复
只显示第一个的标题和选项,其他的只显示标题不显示选项,不知道代码那里有问题
上面多了一个B

62,046

社区成员

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

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

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

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