如何解决问题:启用单选按钮上的文本框检查 [英] How to solve the problem: enable text box on radio button check

查看:67
本文介绍了如何解决问题:启用单选按钮上的文本框检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我单击了单选按钮1禁用文本框.单选按钮2单击

文本框已启用,但首先我单击了单选按钮2,然后我单击了单选按钮1并未禁用文本框.如何解决问题

i have click the radio button1 the disable the text box. the radio button2 click

the text box is enable but first i have click radio button2 and then i have click radio button1 not disable the text box How to solve the problem

public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
               txtrb.Enabled = true;
            }
        }

        protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
        {
            {
                if (RadioButton1.Enabled == true )
                {
                    txtrb.Enabled = false ;
                }
            }
        }
        protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (RadioButton2.Enabled == true)
            {
                txtrb.Enabled = true ;
            }

        }
    }
}

推荐答案

如果单选按钮在同一组中,则选中一个将自动取消选中另一个.这样,您只需要一个单选按钮的处理程序即可.第二个可能是多余的,也可能破坏第一个的结果.删除.

此外,你在做什么?!

谁将Booleantrue进行比较?结果与布尔值完全相同!谁在if下分配另一个Boolean?请在编码之前先想一想,以了解这是多么荒谬.这样做:

If you radio button are in the same group, checking one automatically un-check others. In this way, you need the handler for only one radio button. The second one could be redundant or destroy result of the first one. Remove it.

Besides, what are you doing?!

Who compare a Boolean with true?! The result is exactly same very Boolean! Who assign another Boolean under if? Please, think a bit before coding, just to understand how ridiculous this is. Do it this way:

txtrb.Enabled = !RadioButton1.Enabled;



很简单,不是吗?

—SA



Simple, isn''t it?

—SA


使用单选按钮列表并将autopostback属性设置为true;
use radiobuttonlist and make autopostback property true;


SA已经发布了一个不错的答案.您也可以尝试以下操作:

SA already posted a nice answer. You can also try this:

<asp:RadioButton ID="RadioButton1" runat="server" OnCheckedChanged="RadioButton1_CheckedChanged"

           AutoPostBack="true" GroupName="RadioButton" Text="Enable Textbox" /><br />
       <asp:RadioButton ID="RadioButton2" runat="server" OnCheckedChanged="RadioButton2_CheckedChanged"

           AutoPostBack="true" GroupName="RadioButton" Text="Disable Textbox" /><br />
           <asp:TextBox ID="txtrb" runat="server"></asp:TextBox>







protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
    txtrb.Enabled = false ;

}
protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
{
        txtrb.Enabled = true ;

}





希望以上信息对您有所帮助.如果您还有其他顾虑,请告诉我.





I hope the above information will be helpful. If you have more concerns, please let me know.


这篇关于如何解决问题:启用单选按钮上的文本框检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆