【问题标题】:Display the selected item of a RadioButtonList in TextBox在 TextBox 中显示 RadioButtonList 的选定项
【发布时间】:2017-04-03 21:01:27
【问题描述】:

我是 ASP.NET 的新手。我试图在选择单选按钮时在文本框中显示单选按钮的文本。我尝试了两种不同的方法但没有成功:

尝试 1:

我的 aspx 文件中的代码:

<asp:RadioButtonList ID="radioList" runat="server">
<asp:ListItem Value="selection1" Text ="One"></asp:ListItem>
<asp:ListItem Value="selection2" Text="Two"></asp:ListItem>
</asp:RadioButtonList>

然后是我的 .cs 代码:

public void displayText(object sender, EventArgs e)
{
    var result = radioList.SelectedValue;
    output.Text = result.Text; /* Have also tried result.ToString() */
}

尝试 2:

与上面相同的aspx

.cs代码:

public void displayText(object sender, EventArgs e)
{
    if (selection1.Checked)
    {
        output.Text = "One";
    }
}

第一次尝试没有给我任何错误,但没有显示文本。第二次尝试给我错误 CS0103 The name 'selection1' does not exist in the current context 1_ASPTEST.aspx

我敢肯定,我只是忽略了一些简单的事情,但我被难住了。谢谢!

【问题讨论】:

    标签: c# asp.net textbox radiobuttonlist


    【解决方案1】:

    你也可以用这个。

     if (radioList.SelectedItem != null)
            {
                 output.Text = radioList.SelectedItem.Text;
            } 
    

    希望!它会帮助你

    【讨论】:

      猜你喜欢
      • 2018-03-13
      • 1970-01-01
      • 1970-01-01
      • 2012-11-15
      • 2022-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多