动态加载的放射性to的后备更改selectedItem更改

发布于 2025-02-08 19:45:29 字数 2602 浏览 1 评论 0原文

我在设计页面中具有radiobuttonControl定义静态,但从DB动态加载了内容。但是每当我做后卫时,我的选择都会消失,并且选择了第一个动态项目 则默认情况下

例如,如果我有3个项目, Test1 Test2 test3

和我选择test2,然后单击按钮以发回。使用Test1选择页面重新加载,

您可以在此处检查错误吗?

<body>
    <form id="form1" runat="server">
        <asp:Literal ID="ltlQues" runat="server"></asp:Literal>
            <asp:RadioButtonList ID="RadioButtonList1" runat="server" Height="32px" Width="334px">
                <asp:ListItem>Test1</asp:ListItem>
                <asp:ListItem>Test2</asp:ListItem>
                <asp:ListItem>Test3</asp:ListItem>
            </asp:RadioButtonList>
        <asp:Button ID="Button1" runat="server" Text="Vote" BorderStyle="Groove" OnClick="Button1_Click" Height="26px" Width="60px" />
        <asp:HiddenField ID="QuestionId" runat="server" />
    </form>
</body>
/// <summary>
/// For page load
/// </summary>
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Session["userid"] = "123";
        loadPoll();
    }
}

/// <summary>
/// to load Active Poll
/// </summary>
private void loadPoll()
{
    try
    {
        ListItem li;
        SqlDataAdapter adaQuestionAndAnswers = dbObj.getAdapter("SelectQuestion");
        DataSet dsResults = new DataSet();
        adaQuestionAndAnswers.Fill(dsResults);

        
        bool isQuesionDataFilled = false;
        DataTable dt = dsResults.Tables[0];
        foreach (DataRow row in dt.Rows)
        {
            if (!isQuesionDataFilled)
            {
                    ltlQues.Text = row["QuestionText"].ToString();
                    QuestionId.Value = row["QuestionID"].ToString();
                    //ltlQuesOther.Text = row["AnswerText"].ToString();
                    //ltlvotecount.Text ="Total "+row["total"].ToString()+" Votes";
                    isQuesionDataFilled = true;
            }
            //add answers to RadioButtonList
            li = new ListItem();
            li.Text = row["AnswerText"].ToString();
            li.Value = row["QuestionID"].ToString();
            li.Attributes.Add("group", "contestants");
            RadioButtonList1.Items.Add(li);
        }
    }
    catch (Exception ex)
    {
        //Log the error
        Response.Write(ex.Message);
    }
}

//To Vote
protected void Button1_Click(object sender, EventArgs e)
{
    try
    {
        Response.Write(RadioButtonList1.SelectedItem.Text);
    }
    catch (Exception ex)
    {
        Response.Write(ex.Message);
    }
}

I have the Radiobuttoncontrol definition static in the design page but loading the contents dynamically from db. But whenever i do postback, my selection vanishes and the first dynamic item is selected
by default

for instance if i have 3 items
Test1
Test2
Test3

and i select Test2 and click on Button to postback. Page reloads with Test1 selected

Can you please check whats wrong here

<body>
    <form id="form1" runat="server">
        <asp:Literal ID="ltlQues" runat="server"></asp:Literal>
            <asp:RadioButtonList ID="RadioButtonList1" runat="server" Height="32px" Width="334px">
                <asp:ListItem>Test1</asp:ListItem>
                <asp:ListItem>Test2</asp:ListItem>
                <asp:ListItem>Test3</asp:ListItem>
            </asp:RadioButtonList>
        <asp:Button ID="Button1" runat="server" Text="Vote" BorderStyle="Groove" OnClick="Button1_Click" Height="26px" Width="60px" />
        <asp:HiddenField ID="QuestionId" runat="server" />
    </form>
</body>
/// <summary>
/// For page load
/// </summary>
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Session["userid"] = "123";
        loadPoll();
    }
}

/// <summary>
/// to load Active Poll
/// </summary>
private void loadPoll()
{
    try
    {
        ListItem li;
        SqlDataAdapter adaQuestionAndAnswers = dbObj.getAdapter("SelectQuestion");
        DataSet dsResults = new DataSet();
        adaQuestionAndAnswers.Fill(dsResults);

        
        bool isQuesionDataFilled = false;
        DataTable dt = dsResults.Tables[0];
        foreach (DataRow row in dt.Rows)
        {
            if (!isQuesionDataFilled)
            {
                    ltlQues.Text = row["QuestionText"].ToString();
                    QuestionId.Value = row["QuestionID"].ToString();
                    //ltlQuesOther.Text = row["AnswerText"].ToString();
                    //ltlvotecount.Text ="Total "+row["total"].ToString()+" Votes";
                    isQuesionDataFilled = true;
            }
            //add answers to RadioButtonList
            li = new ListItem();
            li.Text = row["AnswerText"].ToString();
            li.Value = row["QuestionID"].ToString();
            li.Attributes.Add("group", "contestants");
            RadioButtonList1.Items.Add(li);
        }
    }
    catch (Exception ex)
    {
        //Log the error
        Response.Write(ex.Message);
    }
}

//To Vote
protected void Button1_Click(object sender, EventArgs e)
{
    try
    {
        Response.Write(RadioButtonList1.SelectedItem.Text);
    }
    catch (Exception ex)
    {
        Response.Write(ex.Message);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文