public partial class _Default : System.Web.UI.Page
{
    
    protected void Page_Load(object sender, EventArgs e)
    {
        LblErrorMsg.Visible = false;
        if (Page.IsPostBack == false)
        {
            Login();
            SetupLoginTextBoxes();
        }
    }    void SetupLoginTextBoxes()
    {        TxtSearch.Attributes.Add("OnFocus", @"
            if(this.value == 'Enter City Name')
            {
                this.value = '';
            }
            ");        TxtSearch.Attributes.Add("OnBlur", @"
            if(this.value == '')
            {
                this.value = 'Enter City Name';
            }
    }
     
 }
textbox控件上面有两个radiobotton控件,radiobotton1.value=city name,radiobotton2.value=people name。
我想要的结果是:当点击radiobotton1时,TxtSearch.value=‘Enter City Name’,点击radiobotton2时,TxtSearch.value=‘Enter people Name’。请问有什么方法吗?

解决方案 »

  1.   

    radiobotton 的 OnClick事件里直接写
    记得将 radiobotton 的属性  AutoIsPostBack="true"
      

  2.   

    这是你需要的功能:    <script>
        function setTxt()
        {
            var childs=document.getElementById('RadioButtonList1').getElementsByTagName('input');
            for(var i=0;i<childs.length;i++)
                if(childs[i].checked)
                    document.getElementById('TextBox1').value=childs[i].value
        }
        function setFocus()
        {
            var obj=document.getElementById('TextBox1');
            if(obj.value == 'Enter City Name'||obj.value == 'Enter people Name')
            { 
                obj.value = '';
            }
        }
        </script>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:RadioButtonList ID="RadioButtonList1" runat="server">
            <asp:ListItem Value="Enter City Name">city</asp:ListItem>
            <asp:ListItem Value="Enter people Name">people</asp:ListItem>
        </asp:RadioButtonList>    protected void Page_Load(object sender, EventArgs e)
        {
            Init();
        }
        void Init()
        {
            RadioButtonList1.SelectedIndex = 0;
            RadioButtonList1.Attributes.Add("onclick", "setTxt()");
            TextBox1.Text = "Enter City Name";
            TextBox1.Attributes.Add("onfocus", "setFocus()");
            TextBox1.Attributes.Add("onblur", " if(this.value == '')setTxt();");
        }