3

这看起来很简单,但我就是无法理解。我有一个 asp.net 页面,具有以下凭据:

  1. 1 个按钮
  2. 1 个文本框
  3. 1个中继器

*现在,当我在文本框中键入文本时,我将文本传递给使用 jquery ajax 的 Web 方法,该方法将文本插入到数据库中。现在我的代码中有一个公共方法,它从数据库中选择数据并将其绑定到转发器。如何在没有回发的情况下在每个数据插入上显示此转发器?*

 success: function (data) {
            for (var i = 0; i < data.d.length; i++) {
                $("#tbDetails").append("<tr><td>" + data.d[i].ID + "</td><td>" + data.d[i].ChargeName + "</td><td>" + data.d[i].Description + "</td></tr>");
            }
        },
        error: function (result) {
            alert("Error");
4

1 回答 1

0

这是一个小样本,我想你可以忽略插入部分,因为你说你已经有了它。这使用 jQuery,您也在使用它。

HTML:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="false"
        CodeFile="Default.aspx.cs" Inherits="_Default" %>

    <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
        <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
        <script language="javascript" type="text/javascript">
            function insertData() {
                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: "Default.aspx/insertData",
                    data: "{}",
                    dataType: "json",
                    success: function (data) {
                        if (data.d != null)
                            alert(data.d);
                    }
                });
            }

            function retrieveData() {
                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: "Default.aspx/retrieveData",
                    data: "{}",
                    dataType: "json",
                    success: function (data) {
                        if (data.d != null) {
                            for (i = 0; i < data.d.length; i++) {
                                $("#myTable").append("<tr><td><input type='button' onclick='deleteRow(" + data.d[i].ID + ")' value='Delete'></input></td><td>" + data.d[i].Name + "</td><td>" + data.d[i].LastName + "</td><td>" + data.d[i].Age + "</td></tr>");
                            }
                        }
                    }
                });
            }
            function deleteRow(ID) {
                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: "Default.aspx/deleteRow",
                    data: "{'row':'" + ID + "'}",
                    dataType: "json",
                    success: function (data) {
                        if (data.d != null) {
                            alert(data.d);
                        }
                    }
                });
            }
            retrieveData();
        </script>
    </asp:Content>
    <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
        <table id="myTable">
        </table>
    </asp:Content>

ASP.NET:

using System;
using System.Web.Services;
using System.Collections.Generic;

public partial class _Default : System.Web.UI.Page
{

    [WebMethod]
    public static List<SampleClass> retrieveData()
    {
        //Retrieve data from the server and return.
        List<SampleClass> tmp = new List<SampleClass>();

        tmp.Add(new SampleClass() { Name = "Test", LastName = "Number 1", Age = 44, ID = 1 });
        tmp.Add(new SampleClass() { Name = "Test", LastName = "Number 2", Age = 23, ID = 2 });
        tmp.Add(new SampleClass() { Name = "Test", LastName = "Number 3", Age = 20, ID = 3 });
        tmp.Add(new SampleClass() { Name = "Test", LastName = "Number 4", Age = 45, ID = 4 });
        tmp.Add(new SampleClass() { Name = "Test", LastName = "Number 5", Age = 21, ID = 5 });

        return tmp;
    }

    [WebMethod]
    public static bool deleteRow(int row)
     {
        if (row > 0)
            return true;

        return false;
    }

    [WebMethod]
    public static bool insertData()
    {
        //Do all of the insert in the server blah blah blah
        //and return true or false if it was inserted.
        return true;

    }
}

public class SampleClass
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
}

祝你好运!编辑:现在您还可以看到一个删除示例,其中包括将参数传递给 web 方法。祝你好运。

于 2012-07-12T22:04:00.850 回答