我正在尝试创建一个动态的多选问题测验,其中我们有不同的类别,其中有多个问题及其各自的选项。我需要按照用户存储选定的选项。
下面是我用来绑定数据的aspx代码:
<div id="contact-form">
<input type="text" class="form-control form-control-custom" tabindex="-1"
id="text-field" name="text-field">
<asp:ListView ID="lv_cat" runat="server" OnItemDataBound="lv_cat_ItemDataBound">
<EmptyDataTemplate></EmptyDataTemplate>
<ItemTemplate>
<h4 style="border: 2px solid #000; background-color: #ffd281;"><%# Eval("Cat_name") %></h4>
<asp:Label ID="lbl_Cat_id" runat="server" Text='<%# Eval("cat_id") %>' hidden="true"></asp:Label>
<asp:ListView ID="Lv_question" runat="server" OnItemDataBound="Lv_question_ItemDataBound">
<EmptyDataTemplate></EmptyDataTemplate>
<ItemTemplate>
<div class="row">
<p style="text-align: left; font-weight: 600;"><%# Eval("Question") %></p>
<asp:Label ID="lbl_Q_Id" runat="server" Text='<%# Eval("Q_id") %>' hidden="true"></asp:Label>
<asp:RadioButtonList ID="Rbl_options" runat="server" Style="text-align: left;">
</asp:RadioButtonList>
</div>
</ItemTemplate>
</asp:ListView>
</ItemTemplate>
</asp:ListView>
</div>
<!-- /End Contact Form -->
<br />
<!-- Submit Button -->
<div class="btn-row">
<div class="form-group">
<asp:Button ID="Button2" class="btn btn-dark" runat="server" Text=" Submit"></asp:Button>
</div>
</div>
以下是用于绑定数据的aspx.cs代码。 现在我想获取用户提交的详细信息。
private void getProjectdetails()
{
try
{
BAL_Projects balprojects = new BAL_Projects();
balprojects.P_id = p_id;
ds = balprojects.get_data_By_project_Id(str);
if (ds.Tables[0].Rows.Count > 0)
{
lbl_Project.Text = ds.Tables[0].Rows[0]["pname"].ToString();
}
}
catch (Exception ex)
{
throw ex;
}
}
protected void lv_cat_ItemDataBound(object sender, ListViewItemEventArgs e)
{
try
{
Label cat_id = (Label)e.Item.FindControl("lbl_Cat_id");
balQuestions = new BAL_Questions();
balQuestions.Cat_id = cat_id.Text;
ds1 = balQuestions.get_data_questions_By_category_ID(str);
ListView ListView2 = e.Item.FindControl("Lv_question") as ListView;
if (ds1.Tables[0].Rows.Count > 0)
{
ListView2.DataSource = ds1.Tables[0];
ListView2.DataBind();
}
}
catch (Exception ex)
{
throw ex;
}
}
protected void Lv_question_ItemDataBound(object sender, ListViewItemEventArgs e)
{
try
{
Label Q_id = (Label)e.Item.FindControl("lbl_Q_Id");
BAL_options bal_options = new BAL_options();
ds = bal_options.get_options_for_Question(str, Q_id.Text.ToString());
if (ds.Tables[0].Rows.Count > 0)
{
RadioButtonList rbl_1 = (RadioButtonList)e.Item.FindControl("Rbl_options");
rbl_1.DataSource = ds;
rbl_1.DataTextField = "answers";
rbl_1.DataValueField = "A_id";
rbl_1.DataBind();
}
}
catch (Exception ex)
{
throw ex;
}
}
private void getCategorydata()
{
try
{
BAL_category bal_category = new BAL_category();
bal_category.p_id = p_id;
ds = bal_category.get_data_category_By_Project_ID(str);
if (ds.Tables[0].Rows.Count > 0)
{
lv_cat.DataSource = ds;
lv_cat.DataBind();
}
}
catch (Exception ex) { }
}
我也尝试了以下代码:
public void btnsubmit_Click(object sender, EventArgs e)
{
try
{
foreach (ListViewItem item in lv_cat.Items)
{
ListView listQ = (ListView)item.FindControl("lv_Question");
foreach (ListViewItem item1 in listQ.Items)
{
Label QID = (Label)item1.FindControl("lbl_Q_Id");
RadioButtonList rbl1 = (RadioButtonList)item1.FindControl("rbl_options");
string test = rbl1.SelectedValue.ToString();
}
}
}
catch (Exception ex)
{
throw ex;
}
}
答案 0 :(得分:0)
基本上与将数据绑定到ListView ItemDataBound中的Rbl_options
RadioButtonList时相同。在按钮上单击循环ListView中的所有项目,找到项目中的RadioButton并使用FindControl。
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack == false)
{
//bind the datasource for the listview
Lv_question.DataSource = source;
Lv_question.DataBind();
}
}
protected void Lv_question_ItemDataBound(object sender, ListViewItemEventArgs e)
{
//use findcontrol to locate the radiobuttonlist and cast is back
RadioButtonList rbl = e.Item.FindControl("Rbl_options") as RadioButtonList;
//add some dummy listitems
for (int i = 0; i < 3; i++)
{
rbl.Items.Add(new ListItem() { Text = "Item " + i, Value = i.ToString() });
}
}
protected void Button1_Click(object sender, EventArgs e)
{
//loop all the items in the listview
foreach (var item in Lv_question.Items)
{
//use findcontrol again to locate the radiobuttonlist in the listview item
RadioButtonList rbl = item.FindControl("Rbl_options") as RadioButtonList;
//show results
Label1.Text += rbl.SelectedValue + "<br>";
}
}
使演示完成的aspx
<asp:ListView ID="Lv_question" runat="server" OnItemDataBound="Lv_question_ItemDataBound">
<ItemTemplate>
<asp:RadioButtonList ID="Rbl_options" runat="server"></asp:RadioButtonList>
</ItemTemplate>
</asp:ListView>