【问题标题】:How to get value from dynamically created radiobuttonlist using listview?如何使用列表视图从动态创建的单选按钮列表中获取价值?
【发布时间】:2018-07-30 13:41:45
【问题描述】:

我正在尝试创建一个动态的多项选择题测验,其中我们有不同的类别,其中有多个问题及其各自的选项。我需要根据用户存储选择的选项。

下面是我用来绑定数据的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;
        }
    }

【问题讨论】:

    标签: c# asp.net .net dynamic radiobuttonlist


    【解决方案1】:

    基本上与将数据绑定到 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>
    

    【讨论】:

    • 感谢@VDWWD 的回复,我尝试实现上述内容,但未能达到预期的输出,尤其是 Lv_question_ItemDataBound 部分。你能详细说明你到底想说什么吗?
    • 基本上我需要做的是一键捕获所有数据。并且由于显示部分正在提供所需的输出,但由于嵌套的列表视图而无法访问查找控制功能。
    • 如果嵌套部分给您带来麻烦,您可以堆叠 FindControl。或者先找父对象,再找子对象。
    • 我使用了建议的解决方案。但它没有给出选定的值。请找到相同的编辑代码。
    • 你能帮我看看哪里出错了吗? @VDWWD
    猜你喜欢
    • 1970-01-01
    • 2011-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-17
    • 1970-01-01
    • 2014-03-02
    • 1970-01-01
    相关资源
    最近更新 更多