在aspx中,实现在文本框中敲入回车键就提交表单,只要在这个文本框中加入  onkeydown="javascript:keydown()"  并在页面中加入函数: <script language="javascript">
function keydown()
{
if (event.keyCode == 13)
{
event.returnValue=false;
event.cancel = true;
Form1.btnSubmit.click();
}
}
</script>
即可模拟btnSubmit按钮的click事件,从而提交表单。但我在一个ascx文件中,也要实现这个功能,为何总是报错?说那个按钮找不到?ascx文件源码如下:关键字搜索:
<asp:TextBox id="txtboxKeyword" runat="server" Font-Size="9pt" Height="22px" Width="280px" BorderStyle="Solid" BorderWidth="1px" BorderColor="#FCA31D" style="PADDING-TOP:3pt" onfocus="javascript:this.value=''" onkeydown="javascript:keydown1()">请输入餐厅名 菜名 菜系 城市 街道 氛围等任意关键字。</asp:TextBox>
<asp:ImageButton id="ImageButton1" runat="server" mageUrl="../plugins/images/search.gif" ImageAlign="AbsBottom" Width="25" Height="25" CausesValidation="False"></asp:ImageButton>函数是这么写的: <script language="javascript">
function keydown1()
{
if (event.keyCode == 13)
{
event.returnValue=false;
event.cancel = true;
Form1.ImageButton1.click();
}
}
</script>
运行时,总是说Form1.ImageButton1' 为空或不是对象。我查看了源文件,发现它变成 
<INPUT id=WebPart_Head1_ImageButton1 style="WIDTH: 25px; HEIGHT: 25px" type=image src="../plugins/images/search.gif" align=absBottom border=0 name=WebPart_Head1:ImageButton1>
我把Form1.ImageButton1.click()改成Form1.WebPart_Head1_ImageButton1.click();也不行。请大侠指点一下该如何实现?谢谢。

解决方案 »

  1.   

    <%@ Page debug="true"%>
    <%@ Import NameSpace="System.IO" %>
    <%@ Import NameSpace="System.Data" %>
    >
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="gb2312" lang="gb2312">
    <head>
    <title>  获得一个目录下所有的文件,绑定到DataGrid上 </title>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <meta name="title" content="" />
    <meta name="author" content="活靶子,Huobazi,www.AspxBoy.com" />
    <meta name="subject" content="" />
    <meta name="language" content="gb2312" />
    <meta name="keywords" content="" />
    <meta name="Copyright" content="www.AspxBoy.com" />
    <meta name="robots" content="all" />
    <script language="c#" runat="server">
    void Page_Load(object o , EventArgs e)
    {
    if(!Page.IsPostBack){
    this.dg.DataSource = CreateDataSource();
    this.dg.DataBind();
    }

    }DataTable CreateDataSource() 
       {
          DataTable dt = new DataTable();
          DataRow dr; 
          dt.Columns.Add(new DataColumn("文件名称", typeof(string)));
          dt.Columns.Add(new DataColumn("生成时间", typeof(DateTime)));
      dt.Columns.Add(new DataColumn("文件大小(bytes)", typeof(long))); DirectoryInfo dir = new DirectoryInfo(Server.MapPath(".")+"\\");
        foreach(FileSystemInfo fsi in dir.GetFileSystemInfos())
         {
    if(fsi is FileInfo)
               {
                   FileInfo  fi = (FileInfo)fsi;
                    if((fi.Extension == ".htm")||(fi.Extension == ".aspx"))
    {
                      dr = dt.NewRow();
      dr[0] = fi.Name;
      dr[1] = fi.LastWriteTime;
      dr[2] = fi.Length;
      dt.Rows.Add(dr);
    }
              }
        }      
    return dt;
       }
    </script>
    </head>
    <body>
    <form id="frm" runat="server"><asp:DataGrid id="dg" runat="server" ></asp:dataGrid></form>
    </body>
    </html>
      

  2.   

    晕,剪贴板 不听话了
    呵呵,是这个
    http://www.aspxboy.com/private/showthread.asp?threadid=307
      

  3.   

    你的问题出在
    当Ascx被放置在Aspx内的时候其中的控件客户端html内的哪个id全部变化
    这个你可以看html代码看到
    所以你要用YourWebcontrol.ClientID来获取它真正运行时候的客户端id
      

  4.   

    我确实是知道ascx中的控件的ID会变化 ,所以查看了源文件,发现它变成 
    <INPUT id=WebPart_Head1_ImageButton1 style="WIDTH: 25px; HEIGHT: 25px" type=image src="../plugins/images/search.gif" align=absBottom border=0 name=WebPart_Head1:ImageButton1>
    我把Form1.ImageButton1.click()改成Form1.WebPart_Head1_ImageButton1.click();也不行。