DropDownList1.Attributes["onchange"]=string.Format(@"document.all['{0}'].value=this.Items[0].Text;",TextBox1.ClientID);我写的不起作用,请问该怎么写?
还想请教一个问题,就是document.all['{0}']后如何能知道有什么属性(相当于智能感知)?

解决方案 »

  1.   

    你应该从这几个问题抽象一下,自己找出答案了!我假设你的this.Items是写的对的,那么应该:DropDownList1.Attributes["onchange"]=string.Format(@"document.all['{0}'].value=document.all['{1}'].value;",TextBox1.ClientID,this.Items[0].ClientID);由于不知道你的this是什么类型,也许,this.Items[0]前边应该写上类型转换,例如:(TextBox)this.Items[0]。你自己试试。这是因为TextBox1和this.Items[0]是服务端控件,它在客户端时不存在的,它被转换为很多html。就像高级语言的对象在机器执行时被转换为数据段中的数据块,这是不同的概念,不能在脚本直接处理服务器端控件。
      

  2.   

    另外,如果this是datagrid的行(DataGridItem),那么你应该首先为它强制写上ID。因为默认情况下是对TD不输出ID的,虽然asp.net对几乎所有控件生成客户端ID,但是有个别的就不生成。大概是:(TableCell)this.Items[0].Attribute["ID"]=随机生成的ID号;
    DropDownList1.Attributes["onchange"]=string.Format(@"document.all['{0}'].value=document.all['{1}'].value;",TextBox1.ClientID,随机生成的ID号);
      

  3.   

    哦,sorry,没有审题。如果你想获得<select中第一个值,大概应该写:DropDownList1.Attributes["onchange"]=string.Format(@"document.all['{0}'].value=this.options[0].value;",TextBox1.ClientID);对于dhtml对象的属性,你不要自己去想象,去买一本《DHTML大全》之类的工具书常备案头,一查Select对象的属性就知道了。
      

  4.   


    参考
    <!-- HTML 代码-->
    <asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
    <asp:DropDownList id="DropDownList1" runat="server">
    <asp:ListItem Value="aa">aa</asp:ListItem>
    <asp:ListItem Value="bb">bb</asp:ListItem>
    <asp:ListItem Value="cc">cc</asp:ListItem>
    </asp:DropDownList>/*c# 代码*/protected System.Web.UI.WebControls.TextBox TextBox1;
    protected System.Web.UI.WebControls.DropDownList DropDownList1;

    private void Page_Load(object sender, System.EventArgs e)
    {
    DropDownList1.Attributes["onchange"]=string.Format(@"document.all['{0}'].value=this.value;",TextBox1.ClientID);
    }