【问题标题】:How to find a control and change it to checked/unchecked from Code-Behind (ASP.NET)如何从代码隐藏(ASP.NET)中找到一个控件并将其更改为选中/未选中
【发布时间】:2019-11-09 15:05:31
【问题描述】:

我有这些单选按钮:

<fieldset class="rating">
    <input runat="server" type="radio" onclick="setRating(5);" id="star10" name="rating" value="5" /><label class = "full" for="star5" title="Excelsior! - 5 stars"></label>
    <input runat="server" type="radio" onclick="setRating(4.5);" id="star9" name="rating" value="4 and a half" /><label class="half" for="star4half" title="Almost Perfect - 4.5 stars"></label>
    <input runat="server" type="radio" onclick="setRating(4);" id="star8" name="rating" value="4" /><label class = "full" for="star4" title="Good - 4 stars"></label>
    <input runat="server" type="radio" onclick="setRating(3.5);" id="star7" name="rating" value="3 and a half" /><label class="half" for="star3half" title="Above Average - 3.5 stars"></label>
    <input runat="server" type="radio" onclick="setRating(3);" id="star6" name="rating" value="3" /><label class = "full" for="star3" title="Average - 3 stars"></label>
    <input runat="server" type="radio" onclick="setRating(2.5);" id="star5" name="rating" value="2 and a half" /><label class="half" for="star2half" title="Adequate - 2.5 stars"></label>
    <input runat="server" type="radio" onclick="setRating(2);" id="star4" name="rating" value="2" /><label class = "full" for="star2" title="Underwhelming - 2 stars"></label>
    <input runat="server" type="radio" onclick="setRating(1.5);" id="star3" name="rating" value="1 and a half" /><label class="half" for="star1half" title="Weak - 1.5 stars"></label>
    <input runat="server" type="radio" onclick="setRating(1);" id="star2" name="rating" value="1" /><label class = "full" for="star1" title="Bad - 1 star"></label>
    <input runat="server" type="radio" onclick="setRating(0.5);" id="star1" name="rating" value="half" /><label class="half" title="Atrocious - 0.5 stars"></label>
</fieldset>

我想遍历它们以设置它们的状态,而不必执行switch statement 之类的操作,因此我尝试了以下操作:

for (double i = 0; i < 5; i+=0.5)
{
    if(pOld.OverallRating == i)
    {
        //Stars are 1-10 but display as x/5 (where 5 is star10, 4.5/5 is star9 etc)
        Control con = FindControl("star"+i);
        ((RadioButton)con).Checked = true;
    }
}

我使用 FindControl 的方式似乎存在问题,但我不明白为什么 - 我得到一个空引用异常。

编辑1:

System.InvalidCastException: 'Unable to cast object of type 'System.Web.UI.HtmlControls.HtmlInputRadioButton' to type 'System.Web.UI.WebControls.RadioButton'.'

【问题讨论】:

    标签: c# asp.net radio-button nullreferenceexception code-behind


    【解决方案1】:

    您的输入名为 star1, star2... star10,但您的循环将 i 设置为 0、0.5、1... 5,因此您将寻找 ID 为 star0star0.5 等的控件'不存在

    假设你不想有一个代表 0 星的复选框,你会想要做这样的事情:

    for (double i = 0.5; i <= 5; i+=0.5)
    {
        if(pOld.OverallRating == i)
        {
            //Stars are 1-10 but display as x/5 (where 5 is star10, 4.5/5 is star9 etc)
            var id = i * 2;
            Control con = FindControl("star"+id);
            ((HtmlInputRadioButton)con).Checked = true;
        }
    }
    

    这将查找 id 为 star1, star2... star10 的输入

    编辑: 要回答更新后的问题 - 您正在尝试将 HtmlInputRadioButton 类型的控件转换为 RadioButton - 而不是转换为 HtmlInputRadioButton

    【讨论】:

    • 啊,你是对的,但是我现在得到的例外是(参见#EDIT1)
    • @Sanguine 您将控件转换为错误的类型 - 将 (RadioButton) 替换为 (HtmlRadioButton)
    • 似乎仍然无法正常工作。 The type or namespace name 'HtmlRadioButton' could not be found (are you missing a using directive or an assembly reference?)
    • @Sanguine 您需要在类定义的顶部添加一条 using 语句 - 类似于 using System.Web.UI.HtmlControls
    • @Sanguine 我们都有那些日子 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-15
    • 1970-01-01
    • 2013-03-20
    • 2013-02-19
    • 2011-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多