首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >RadioButtonList SelectedItem返回NULL

RadioButtonList SelectedItem返回NULL
EN

Stack Overflow用户
提问于 2019-10-02 04:12:20
回答 1查看 247关注 0票数 0

我有这个RadioButton,但它不返回值,总是为null或为空。

无线电就是这个:

代码语言:javascript
运行
复制
<asp:RadioButtonList ID="rblParcelamento" runat="server"/>

我用下面的代码填充RadioButtonList

代码语言:javascript
运行
复制
if (!IsPostBack)
{
    if (decimal.Parse(txtValorTotal.InnerHtml) < 500)
    {
        string val = string.Format("{0:#.00}", Convert.ToDouble(txtValorTotal.InnerHtml) * 0.987);
        rblParcelamento.Items.Add(new ListItem("1 x " + string.Format("{0:#.00}", val) + " - Prazo: 7 dias", "1"));
    }
    else
    {
        string val = string.Format("{0:#.00}", Convert.ToDouble(txtValorTotal.InnerHtml) * 0.987);
        rblParcelamento.Items.Add(new ListItem("1 x " + string.Format("{0:#.00}", val) + " - Prazo: 28 dias", "1"));
    }
    if (decimal.Parse(txtValorTotal.InnerHtml) >= 1000)
    {
        string val = string.Format("{0:#.00}", decimal.Parse(txtValorTotal.InnerHtml) / 2);
        rblParcelamento.Items.Add(new ListItem("2 x " + val + " - Prazo: 21/35 ddl", "2"));
    }
    if (decimal.Parse(txtValorTotal.InnerHtml) >= 1500)
    {
        string val = string.Format("{0:#.00}", decimal.Parse(txtValorTotal.InnerHtml) / 3);
        rblParcelamento.Items.Add(new ListItem("3 x " + val + " - Prazo: 21/28/35 ddl", "3"));
    }
    if (decimal.Parse(txtValorTotal.InnerHtml) >= 2000)
    {
        string val = string.Format("{0:#.00}", decimal.Parse(txtValorTotal.InnerHtml) / 4);
        rblParcelamento.Items.Add(new ListItem("4 x " + val + " - Prazo: 21/28/35/42 ddl", "4"));
    }
}

当我尝试像这样捕获时,rblParcelamento.SelectedItem.Value返回null,rblParcelamento.SelectedValue也是空的。

EN

回答 1

Stack Overflow用户

发布于 2019-10-02 20:22:27

我认为问题是因为你没有在你的RadioButtonList上选择任何东西。

您需要先使用SelectedIndex.SelectedValueSelectedItem来选择它,或者在用户选择它之后获取值。

假设您想要在设置rblParcelamento.SelectedItem.Value的值之后使用它,请尝试以下操作:

代码语言:javascript
运行
复制
if (!IsPostBack)
{
    decimal total = decimal.Parse(txtValorTotal.InnerHtml);

    if (total < 500)
    {
        string val = string.Format("{0:#.00}", Convert.ToDouble(total) * 0.987);
        rblParcelamento.Items.Add(new ListItem("1 x " + string.Format("{0:#.00}", val) + " - Prazo: 7 dias", "1"));
    }
    else
    {
        string val = string.Format("{0:#.00}", Convert.ToDouble(total) * 0.987);
        rblParcelamento.Items.Add(new ListItem("1 x " + string.Format("{0:#.00}", val) + " - Prazo: 28 dias", "1"));
    }

    if (total >= 1000)
    {
        string val = string.Format("{0:#.00}", total / 2);
        rblParcelamento.Items.Add(new ListItem("2 x " + val + " - Prazo: 21/35 ddl", "2"));
    }

    if (total >= 1500)
    {
        string val = string.Format("{0:#.00}", total / 3);
        rblParcelamento.Items.Add(new ListItem("3 x " + val + " - Prazo: 21/28/35 ddl", "3"));
    }

    if (total >= 2000)
    {
        string val = string.Format("{0:#.00}", total / 4);
        rblParcelamento.Items.Add(new ListItem("4 x " + val + " - Prazo: 21/28/35/42 ddl", "4"));
    }

    rblParcelamento.SelectedIndex = 0; //you first need to select your item, in this case, the first one

    //rblParcelamento.SelectedValue = "1"; //this also works

    //rblParcelamento.SelectedItem => this could work if you save a variable with (new ListItem)
}

string rblValue = rblParcelamento.SelectedItem.Value; //then you can get the value of the selected item
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58191652

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档