Radio Button 未被选中 vb.net

huangapple go评论62阅读模式
英文:

Radio Button not being checked vb.net

问题

I'm trying to check/select the radio button on code behind (vb.net) but it's not working. It's not putting a dot on the selected radio button. I don't know why. I know it's getting the right row/selection. But there's no dot the selected radio button.

这是我在代码后台(vb.net)尝试选择/检查单选按钮,但它不起作用。它没有在选定的单选按钮上显示点。我不知道为什么。我知道它获取了正确的行/选择,但选定的单选按钮上没有点。

This is how I call the function on radio button checked changed():

这是我在单选按钮选中更改时调用该函数的方式:

If Not IsPostBack Then
If Not SessionVars.CARD_NUM Is Nothing Then
txtCardNum.Text = SessionVars.CARD_NUM
txtBirthDate.Text = SessionVars.DOB
rObject = GetRadioButton()
rbtn_CheckedChanged(rObject, e)
End If
End If

This is the function of radio button when check changed:

这是在单选按钮选中更改时的函数:

Protected Sub rbtn_CheckedChanged(sender As Object, e As EventArgs)

For Each i As ListViewDataItem In lsvAvailable.Items
    rbtnTypes = CType(i.FindControl("rbtn"), RadioButton)
    rbtnTypes.Checked = False
Next
rbtnTypes = CType(sender, RadioButton)
rbtnTypes.Checked = True

End Sub

But no MARK of DOT is on the screen, would anyone enlighten me about this? Is it because it's not triggered physically by the user?

但屏幕上没有点的标记,有谁能为我解释一下吗?是因为它没有被用户物理触发吗?

Thank you!

谢谢!

英文:

I'm trying to check/select the radio button on code behind (vb.net) but it's not working. It's not putting a dot on the selected radio button. I don't know why. I know it's getting the right row/selection. But there's no dot the selected radio button.

   Private Function GetRadioButton() As Object
    Dim rObject As Object = Nothing
    Dim rbtnPTypes As RadioButton
    Try
        For Each i As ListViewDataItem In lsvAvailable.Items
            If SessionVars.TYPE_ID = lsvAvailable.DataKeys(i.DataItemIndex).Value.ToString() Then
                rbtnTypes = CType(i.FindControl("rbtnType"), RadioButton)
               rObject = rbtnTypes
            End If
        Next
    Catch ex As Exception
    End Try

    Return rObject
  End Function

This is how I call the function on radio button checked changed():

    If Not IsPostBack Then
            If Not SessionVars.CARD_NUM Is Nothing Then
                txtCardNum.Text = SessionVars.CARD_NUM
                txtBirthDate.Text = SessionVars.DOB                    
                rObject = GetRadioButton()
                rbtn_CheckedChanged(rObject, e)                    
            End If
        End If

This is the function of radio button when check changed:

 Protected Sub rbtn_CheckedChanged(sender As Object, e As EventArgs)

      For Each i As ListViewDataItem In lsvAvailable.Items
            rbtnTypes = CType(i.FindControl("rbtn"), RadioButton)
            rbtnTypes.Checked = False
      Next
        rbtnTypes = CType(sender, RadioButton)
        rbtnTypes.Checked = True
 End Sub

But no MARK of DOT is on the screen, would anyone enlighten me about this? Is it because it's not triggered physically by the user?

Thank you!

答案1

得分: 0

Feedback

好的,不清楚 SessionVars 是在哪里加载的,在你发布的代码之前是否进行了设置,使用了那个虚构的值/变量。

此外,不清楚为什么你在这里使用了 find control。可能你正在使用某种类型的重复数据控件(如 repeater、listview、gridview 等)。

你还忘记了有关相关 RadioButtonList 的标记?这使得很难猜测和了解你在尝试做什么。

Answer

不过,让我们插入一个 RadioButtonList,然后使用代码后端来设置该 RB 列表。

因此,RadioButtonList(就像列表框或甚至组合框(下拉列表框)一样)可以有两个值。

"显示" 值 - 用户看到的值。

"隐藏" 值 - 控件根据用户选择返回的值。

通常,显示值(文本)和隐藏值是相同的,但通常它们可能不同。

所以,我们可能有一个下拉列表,显示一些文本,但值可能是数据库的 PK 行 ID。

还有第三个选项 - 选择的索引!因此,如果你有 10 个值,你可以从该控件中获取索引 0-9。

所以,让我们插入 3 个文本框,3 个按钮。每个选择都将根据输入的值设置/更改单选按钮列表。

所以,我们有这个:

<div style="float:left">
    <h4>按值设置 RB</h4>
    输入值:2,4,6,8,10
    <br />
    <asp:TextBox ID="txtByValue" runat="server"></asp:TextBox>
    <br />
    <asp:Button ID="cmdValueSet" runat="server" Text="按值设置"
        OnClick="cmdValueSet_Click"/>
</div>

<div style="float:left;margin-left:30px">
    <h4>按文本设置 RB</h4>
    输入文本:Poor, Fair, Good, Excellent, 5 stars
    <br />
    <asp:TextBox ID="txtByText" runat="server"></asp:TextBox>
    <br />
    <asp:Button ID="cmdTextSet" runat="server" Text="按文本设置"
        OnClick="cmdTextSet_Click" />
</div>

<div style="float:left;margin-left:30px">
    <h4>按索引设置 RB</h4>
    输入索引:0,1,2,3,4
    <br />
    <asp:TextBox ID="txtByIndex" runat="server"></asp:TextBox>
    <br />
    <asp:Button ID="cmdIndexSet" runat="server" Text="按索引设置"
        OnClick="cmdIndexSet_Click" />
</div>

<div style="clear:both"></div>
<br />

<asp:RadioButtonList ID="RadioButtonList1" 
    runat="server">
    <asp:ListItem Value="2">Poor</asp:ListItem>
    <asp:ListItem Value="4">Fair</asp:ListItem>
    <asp:ListItem Value="6">Good</asp:ListItem>
    <asp:ListItem Value="8">Excellent</asp:ListItem>
    <asp:ListItem Value="10">5 Stars</asp:ListItem>
</asp:RadioButtonList>

因此,我们得到/看到了这个:

Radio Button 未被选中 vb.net

所以,正如提到的,没有必要查找控件。

不过,你是想按值、按文本还是按索引设置单选按钮列表?

你必须决定你希望这个工作的方式。

由于你没有分享你的标记,那么我们都必须在这里猜测,并发布一个更复杂的“通用”解决方案,而不知道你想采取哪种方法。

注意,某些 RadioButtonList 使用文本标签,因此这样:

<asp:RadioButtonList ID="RadioButtonList1" 
    runat="server">
    <asp:ListItem Value="2" Text="Poor"></asp:ListItem>
    <asp:ListItem Value="4" Text="Fair"></asp:ListItem>
    <asp:ListItem Value="6" Text="Good"></asp:ListItem>
    <asp:ListItem Value="8" Text="Excellent"></asp:ListItem>
    <asp:ListItem Value="10" Text="5 Starts"></asp:ListItem>
</asp:RadioButtonList>

然而,上面发布的代码将以相同的方式工作。

正如提到的,通常 RB 列表不会同时具有值和文本值(再次,就像列表框或下拉(组合)框一样,它们也具有这种能力来具有两个值,但通常我们只使用一个值来控制这些控件,RB 列表也是一样的)。

英文:

Feedback

Well, it's not clear where SessionVars is being loaded, setup before your posted code uses that made up value/var.

Also, it is not clear why you using find control here. It is possible that you're using some type of repeating data control (repeater, listview, gridview etc.).

And you left out the markup for the RadioButton list in question? This makes it VERY difficult to guess and know what you are trying to do here.

Answer

However, let's drop in a RadioButton list, and then use codebehind to set that RB list.

So, a RadioButton list (like a listbox, or even combo box (dropdownlist) can have 2 values.

The "display" value - the value the user sees.

The "hidden" value - the value the control returns based on user choice.

Often, the display value (text), and the hidden value are the same, but often they will not be.

So, we might have a drop down list, displays some text but the value might be say a database PK row id.

And there is a third option - the index chosen! So, if you have 10 values, you can get the "index" 0-9 from that control.

So, let's drop in 3 text boxes, 3 buttons. Each choice will set/change the Radio button list based on the value you enter.

So, we have this:

    &lt;div style=&quot;float:left&quot;&gt;
        &lt;h4&gt;Set RB by Value&lt;/h4&gt;
        Enter Value: 2,4,6,8,10
        &lt;br /&gt;
        &lt;asp:TextBox ID=&quot;txtByValue&quot; runat=&quot;server&quot;&gt;&lt;/asp:TextBox&gt;
        &lt;br /&gt;
        &lt;asp:Button ID=&quot;cmdValueSet&quot; runat=&quot;server&quot; Text=&quot;Set by Value&quot; 
            OnClick=&quot;cmdValueSet_Click&quot;/&gt;
    &lt;/div&gt;

    &lt;div style=&quot;float:left;margin-left:30px&quot;&gt;
        &lt;h4&gt;Set RB by Text&lt;/h4&gt;
        Enter Text: Poor, Fair, Good, Excellent, 5 stars
        &lt;br /&gt;
        &lt;asp:TextBox ID=&quot;txtByText&quot; runat=&quot;server&quot;&gt;&lt;/asp:TextBox&gt;
        &lt;br /&gt;
        &lt;asp:Button ID=&quot;cmdTextSet&quot; runat=&quot;server&quot; Text=&quot;Set by Text&quot; 
            OnClick=&quot;cmdTextSet_Click&quot; /&gt;
    &lt;/div&gt;

    &lt;div style=&quot;float:left;margin-left:30px&quot;&gt;
        &lt;h4&gt;Set RB by index&lt;/h4&gt;
        Enter Index:  0,1,2,3,4
        &lt;br /&gt;
        &lt;asp:TextBox ID=&quot;txtByIndex&quot; runat=&quot;server&quot;&gt;&lt;/asp:TextBox&gt;
        &lt;br /&gt;
        &lt;asp:Button ID=&quot;cmdIndexSet&quot; runat=&quot;server&quot; Text=&quot;Set by Index&quot;
            OnClick=&quot;cmdIndexSet_Click&quot; /&gt;
    &lt;/div&gt;

    &lt;div style=&quot;clear:both&quot;&gt;&lt;/div&gt;
    &lt;br /&gt;

    &lt;asp:RadioButtonList ID=&quot;RadioButtonList1&quot; 
        runat=&quot;server&quot;&gt;
        &lt;asp:ListItem Value=&quot;2&quot;&gt;Poor&lt;/asp:ListItem&gt;
        &lt;asp:ListItem Value=&quot;4&quot;&gt;Fair&lt;/asp:ListItem&gt;
        &lt;asp:ListItem Value=&quot;6&quot;&gt;Good&lt;/asp:ListItem&gt;
        &lt;asp:ListItem Value=&quot;8&quot;&gt;Excellent&lt;/asp:ListItem&gt;
        &lt;asp:ListItem Value=&quot;10&quot;&gt;5 Stars&lt;/asp:ListItem&gt;

    &lt;/asp:RadioButtonList&gt;

And thus we get/see this:

Radio Button 未被选中 vb.net

So, as noted, there is no need to do a find control.

However, are you trying to set the radio button list by Value? by text? or by index?

You have to decide how you want this to work.

Since you did not share your markup, then we all have to guess here, and post a much more complex solution that is "general" without knowing which road you want to take here.

Note that some RadioButton list use the text tag, and thus this:

&lt;asp:RadioButtonList ID=&quot;RadioButtonList1&quot; 
    runat=&quot;server&quot;&gt;
    &lt;asp:ListItem Value=&quot;2&quot; Text=&quot;Poor&quot;&gt;&lt;/asp:ListItem&gt;
    &lt;asp:ListItem Value=&quot;4&quot; Text=&quot;Fair&quot;&gt;&lt;/asp:ListItem&gt;
    &lt;asp:ListItem Value=&quot;6&quot; Text=&quot;Good&quot;&gt;&lt;/asp:ListItem&gt;
    &lt;asp:ListItem Value=&quot;8&quot; Text=&quot;Excellent&quot;&gt;&lt;/asp:ListItem&gt;
    &lt;asp:ListItem Value=&quot;10&quot; Text=&quot;5 Starts&quot;&gt;&lt;/asp:ListItem&gt;
&lt;/asp:RadioButtonList&gt;

However, the above posted code will work thus the same.

And as noted, often the RB list will not have BOTH a value and text value (again, just like a listbox, or dropdown (combo) which also has this ability to have 2 values, but often we only use 1 value for such controls, and a RB list works the same).

huangapple
  • 本文由 发表于 2023年5月29日 20:46:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76357512.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定