如何找到控件并将其从代码隐藏 (ASP.NET) 更改为 checked/unchecked
How to find a control and change it to checked/unchecked from Code-Behind (ASP.NET)
我有这些单选按钮:
<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'.'
您的输入名为 star1, star2... star10
,但您的循环将 i
设置为 0、0.5、1...5,因此您将寻找 ID 为 star0
的控件, star0.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
我有这些单选按钮:
<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'.'
您的输入名为 star1, star2... star10
,但您的循环将 i
设置为 0、0.5、1...5,因此您将寻找 ID 为 star0
的控件, star0.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