SimpleSearch.ascx中:
--------------------------------------------------------------------------
<SCRIPT language="JavaScript">
function checkall(){
list=document.Form1
if ((list.DropDownList1.options[list.DropDownList1.selectedIndex].value=="0000" ||......)
 {window.alert("地点、职能必须选择!")
    return false
    }
.......省略
}
</SCRIPT><asp:imagebutton id="ImageButton1" runat="server" ImageUrl="../images/searchimg.gif"></asp:imagebutton>-----------------------------------------------------------------------------
SimpleSearch.ascx.Cs中:然后我在SimpleSearch.ascx.Cs中为ImageButton1加了ImageButton1.Attributes["onclick"]="javascript:return checkall();";然后我把这个SimpleSearch.ascx用户控件拖到index.aspx页面内,可是实际一运行index.aspx,发现SimpleSearch.ascx控件中的javascritpt 却根本不起作用,不知道为什么,
可是同样的做法,我如果不做成控件,直接做成aspx,aspx.cs 时却没问题。

解决方案 »

  1.   

    你看看你的页面解析后的html上的下拉筐的id或者name是不是“DropDownList1”
      

  2.   

    将javascript 代码用Response.write 语句在ascx页面中打印出来即可!!!!
    注意双引号改单引号。
    如:Response.write ("<Script language='javascript'>")
    .....
    ....Response.write ("</script>")
      

  3.   

    用法不一样...必须在ASPX里面有这个JavaScript代码才行....你看ASPX页面里面的原文件 生成了JavaScript代码没有
      

  4.   

    TO:DanielTangram(高高.Net) 那如果我的javascript 代码一共有很多行,都要这样在ascs.Cs文件中这样打出来吗?
      

  5.   

    本人曾在做用户控件的时候,遇到跟楼主一样的事情,所以我可以确保使用Response.write打印出来的javascript能够成功执行。
      

  6.   

    TO:parsely(林琳) 对的,没错。试一下,相信没有问题!!
      

  7.   

    TO:xiahouwen(活靶子.NET) 老大你说对了
      

  8.   

    如:
       ' //打印javascript程序,此段程序响应客户端的事件
            Dim strScript As String = ""
            strScript += "<script language='javascript'> " & ControlChars.CrLf
            strScript += "function do_gopage(strgopage)" & ControlChars.CrLf & "{" & ControlChars.CrLf
        ........(此处省略若干行。。)    
            Response.Write(strScript)
      

  9.   

    Dim strScript As String = ""
            strScript += "<script language='javascript'> " & ControlChars.CrLf
            strScript += "function do_gopage(strgopage)" & ControlChars.CrLf & "{" & ControlChars.CrLf
            strScript += "document.all('" + ID + "_hid_GoPage').value = strgopage;" & ControlChars.CrLf(上面的语句组合控件新生成的ID,ID是根据规律自动生成。)        strScript += "document.all('" + ID + "_hid_CustomClicked').value = true;" & ControlChars.CrLf
            strScript += "document.all('" + ID + "_hid_GoPage').form.submit();" & ControlChars.CrLf & "}" & ControlChars.CrLf
            strScript += "function f_Submit()" & ControlChars.CrLf & "{" & ControlChars.CrLf
            strScript += "var int_GoPage = document.all('txt_GoPage').value;" & ControlChars.CrLf
      

  10.   

    原来拖到index.aspx 后,vs.net自动把ascx中的控件给改名了,控件名前头加了SimpleSearch1_DropDownList1如果原来名为DropDownList1,则改名后为SimpleSearch1_DropDownList1,真是奇怪
      

  11.   

    TO: DanielTangram(高高.Net)谢谢你,你的方法也是对的
      

  12.   

    To:DanielTangram(高高.Net) 
    你的方法看不懂啊
      

  13.   

    “如果原来名为DropDownList1,则改名后为SimpleSearch1_DropDownList1,真是奇怪”所以才会有这一句:strScript += "document.all('" + ID + "_hid_CustomClicked').value = true;" & ControlChars.CrLfdocument.all()中,ID就是Me.ID,在你的例子中M.ID = "SimpleSearch1",与后面的“_DropDownList1”一组合,不就变成了编译过后的控件ID????很难理解吗??
      

  14.   

    <SCRIPT language="JavaScript">
    function checkall(){
    list=document.Form1
    if ((list.SimpleSearch1_DropDownList1.options[list.SimpleSearch1_DropDownList1.selectedIndex].value=="0000" || list.SimpleSearch1_Function.options[list.SimpleSearch1_Function.selectedIndex].value=="0000") && (list.SimpleSearch1_keyword.value=="" || list.SimpleSearch1_keyword.value=="输入公司/职位关键字"))
        {window.alert("地点、职能必须选择!")
        return false
        }
    if ((list.SimpleSearch1_DropDownList1.options[list.SimpleSearch1_DropDownList1.selectedIndex].value=="0000")&&(list.SimpleSearch1_SubFunction.options[list.SimpleSearch1_SubFunction.selectedIndex].value=="0000")&&(list.SimpleSearch1_keyword.value==""))
        {window.alert("关键字不能为空!")
         return false
         }
    else return true
    }
    function clearfield(){
    if (document.Form1.SimpleSearch1_keyword.value == "输入公司/职位关键字")
    document.Form1.SimpleSearch1_keyword.value = "";
    }
    function GetObjID(ObjName)
    { for (var ObjID=0; ObjID < document.Form1.elements.length; ObjID++)
        if ( document.Form1.elements[ObjID].name == ObjName )
        {  return(ObjID);
           break;
        }
      return(-1);
    }function ChangeOption(ObjName, DesName)
    {
      //GET OBJECT VALUE, ID OF DESTINATION OBJECT
      ObjID    = GetObjID(ObjName);
      DesObjID = GetObjID(DesName);
      if ( ObjID != -1 && DesObjID != -1 )
      {
        CatValue = document.Form1.elements[ObjID].value.substring(0, 2);
        if ( CatValue == "0" )
          document.Form1.elements[DesObjID].length=0;
        else
        { //PARSING
          document.Form1.elements[DesObjID].length=0;
          var tt = document.Form1.FuncCode.value;
          var SubCategory = tt.split(";");
          //GENERATE OPTIONS
          document.Form1.elements[DesObjID].options[0]= new Option("---所有---", "0000");
          j=1;
          for (var i=0; i< SubCategory.length; i++ )
          {
            pp = SubCategory[i].split(",");
            if ( pp[0].substring(0,2) == CatValue && pp[0].substring(2,4) != "00" )
            {
              document.Form1.elements[DesObjID].options[j]= new Option( pp[1], pp[0]);
              j++;
            }//if
          }//for
        }//if
      }//if
    }
    </SCRIPT>这样一大堆,要如何改你那样的啊?!
      

  15.   

    使用YourContorl.ClientID可以获得控件在被解析成html后的的id
    可以配合这个写javascript
    既在javascript内用<%xxx%>输出该id可以保证后来得到的javascript正确
    或者
    在后台cs中拼接出你的javascript
    然后
    RegisterStartupScript
    或者
    RegisterClientScriptBlock
    你的javascript