<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AddCategory.aspx.cs" Inherits="Manager_AddCategory" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>添加论坛类型</title>
    <script type="text/javascript">
var xmlHttp;
function CreateHttp()
{
    if(window.ActiveXObject)
    {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if(window.XMLHttpRequest)
    {
        xmlHttp=new XMLHttpRequest();
    }
}
function AddCategory()
{
    debugger
    document.getElementById("divList").innerText="请稍候.......正在保存数据!";
    var myName=document.getElementById("txtCategoryName").value;                  //报错的地方
    var myDes=document.getElementById("txtCategoryDes").value;
    CreateHttp();
    xmlHttp.onreadystatechange=StateChange;
    xmlHttp.open("POST","AddCategoryServer.aspx?name="+escape(myName)+"&des="+escape(myDes),true);
    xmlHttp.send(null);
}
function StateChange()
{
    if(xmlHttp.readystate==4&&xmlHttp.status==200)
    {
        document.getElementById("divList").innerText=xmlHttp.responseText;
    }
}
</script></head>
<body>   <form id="form1" runat="server">
    <div id="divList">
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
        添加论坛类别界面<br />
        <table style="width: 541px">
            <tr>
                <td style="height: 28px">
                    论坛名称:</td>
                <td style="height: 28px">
                    <asp:TextBox ID="txtCategoryName" runat="server" Width="319px"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtCategoryName"
                        ErrorMessage="请输入名称"></asp:RequiredFieldValidator></td>
            </tr>
            <tr>
                <td>
                    论坛描述信息:</td>
                <td>
                    <asp:TextBox ID="txtCategoryDes" runat="server" Width="319px"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtCategoryDes"
                        ErrorMessage="请输入描述"></asp:RequiredFieldValidator></td>
            </tr>
            <tr>
                <td>
                </td>
                <td>
                    <input id="btnAddCategory" style="width: 164px" type="button" value="添加类别" onclick="AddCategory()" /></td>
            </tr>
        </table>
    
    </div>
    </form>
</body>
</html>
报错为:
Microsoft JScript 运行时错误: 缺少对象
我把script放到</html>下面也报错
请问为什么

解决方案 »

  1.   

    var myName=document.getElementById('<%=txtCategoryName.ClientID%>').value;   
      

  2.   


    <asp:TextBox ID="txtCategoryName" runat="server" Width="319px"> 
    txtCategoryName这个是服务器控件ID 并不一定和生成Html中ID一样
      

  3.   

    我访问一个放到表格外面的服务器TextBox控件就没有问题 为什么呢?
      

  4.   

    服务器控件生成页面html时ID会变,楼上已经解答,还有一种方法就运行后右击源码,找到对应控件,看看对应ID是多少,然后复制替换txtCategoryName这个就可以了
      

  5.   

    xmlHttp.open("POST"
    ==>
    xmlHttp.open("GET"
      

  6.   

    <div id="divList">
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
            添加论坛类别界面 <br /> 
    ==>
    <div id="divList"><span id="sp_note"></span>
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
            添加论坛类别界面 <br /> 
    document.getElementById("divList").innerText="请稍候.......正在保存数据!"; 
    ==>
    document.getElementById("sp_note").innerHTML="请稍候.......正在保存数据!";