簡體   English   中英

如何使用ListView從動態創建的單選按鈕列表中獲取價值?

[英]How to get value from dynamically created radiobuttonlist using listview?

我正在嘗試創建一個動態的多項選擇題測驗,在這里我們有不同的類別,其中有多個帶有各自選項的問題。 我需要根據用戶存儲所選的選項。

下面是我用來綁定數據的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;
        }
    }

基本上,與將數據綁定到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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM