当前位置导航:炫浪网>>网络学院>>网页制作>>ASP.NET教程

利用ASP.NET AJAX实现表格更新程序

欢迎进入.NET社区论坛,与200万技术人员互动交流 >>进入 今天看了一个网站http://aspadvice.com/blogs/azamsharp/archive/2007/06/28/Edit-GridView-Using-CheckBoxes.aspx,自己做了一个小程序,表格更新程序,主要功能为使用CheckBoxe允许编辑GridView行,并且能够保存修改的信息
具体程序如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MultipleUpdateDemo.aspx.cs" Inherits="MultipleUpdateDemo" %>

<!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/C#" runat="server">
        [System.Web.Services.WebMethod]
        public static bool UpdateText(string categoryName, string description, string Id)
        {
            string conectionString = "Data Source=(local);Initial Catalog=Northwind;User ID=sa;Password=sa";
            System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(conectionString);
            conn.Open();
            System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("UPDATE Categories SET CategoryName = '" + categoryName + "', Description = '" + description + "' WHERE CategoryID = " + Id, conn);
            cmd.ExecuteNonQuery();
            conn.Close();
            return true;
            
        }
    </script>
    <script type="text/javascript">

    function disableGridViewTextBoxes() 
    {
        var gvControl = document.getElementById("gvCategories"); 
        var inputElements = gvControl.getElementsByTagName("INPUT"); 
        for( i = 0;i < inputElements.length; i++) 
        {
            if(isTextBox(inputElements[i])) 
            {
                inputElements[i].disabled = true;
            }
        }
    }

    function isTextBox(obj) 
    {
        return obj.type == 'text';
    }

    function editMode(obj) 
    {
        var rowObject = obj.parentElement.parentElement; 
        var inputElements = getElementsByTagName(rowObject,"INPUT"); 

        if(obj.checked) 
        
            showElements(inputElements,"INPUT","text"); 
        }
        else 
        
            hideElements(inputElements,"text"); 
        } 
    }
    
    function updateMode(obj)
    {
        var rowObject = obj.parentElement.parentElement; 
        var inputElements = getElementsByTagName(rowObject,"INPUT"); 
        
        var msg = new Array();

        for(var i=0;i<inputElements.length;i++) 
        {
            if(inputElements[i].type == "checkbox")
            {
                inputElements[i].checked = false;
            }
            if(inputElements[i].type == "text")
            {
                msg.push(inputElements[i].value);
            }
            if(inputElements[i].type == "hidden")
            {
                msg.push(inputElements[i].value);
            }
        }
        PageMethods.UpdateText(msg[0], msg[1], msg[2], onSuccess);
        hideElements(inputElements,"text"); 
        
    }
    
    function onSuccess(response)
    {
    }

    function showElements(list, tagName,type) 
    {
        for(i = 0;i < list.length; i++) 
        {
            if(isTypeOf(list[i],"text")) 
            {
                list[i].disabled = false;
                list[i].focus(); 
                //list[i].select(); 
            }
        }
    }

    function isTypeOf(obj,type) 
    {
        return obj.type == type;
    }

    function hideElements(list, type) 
    {
        for(i = 0; i < list.length;i++) 
        
            if(isTypeOf(list[i],type)) 
            {
                list[i].disabled = true;
            }
        }
    }
    function getElementsByTagName(obj,tagName) 
    {
        return obj.getElementsByTagName(tagName);
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
    </asp:ScriptManager>
    <div>
    <asp:GridView ID="gvCategories" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
    <Columns>
    <asp:TemplateField>
    <ItemTemplate>
    <asp:CheckBox ID="chkSelect" runat="server" onclick="editMode(this)" />
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Category Name">
    <ItemTemplate>
    <asp:TextBox ID="txtCategoryName" BorderWidth="0px" ForeColor="red" runat="server"
    Text='<%# Eval("CategoryName") %>' />
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Description">
    <ItemTemplate>
    <asp:TextBox ID="txtDescription" BorderWidth="0px" ForeColor="red" runat="server"
    Text='<%# Eval("Description") %>' />
    </ItemTemplate>
    </asp:TemplateField>
     <asp:TemplateField>
    <ItemTemplate>
    <asp:HiddenField Value='<%# Eval("CategoryID") %>' ID="hiddenId" runat="server" />
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField>
    <ItemTemplate>
    <input type="button" runat="server" id="btnUpdate" value="更新" onclick="updateMode(this)" />
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
            SelectCommand="SELECT * FROM [Categories]"></asp:SqlDataSource>
    </div>
    </form>
</body>
</html>
<script>
disableGridViewTextBoxes(); 
</script>

相关内容
赞助商链接