0

如何在不刷新页面的情况下从文本框中保存数据。客户端脚本将我添加到带有文本值的页表中,我希望将该值保存在后面的代码中。

$("#<%=this.AddBookOnPage.ClientID%>").click(function () {
                if (!Page_IsValid)
                    return;
                $("#<%=this.BookAdd.ClientID%>").dialog("close");
                $("#MyTableVis").css("display", "block");
                myFunc();
            });
            function myFunc() {
                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: 'AddAuthor.aspx/InsertMethod',
                    data: "{'Book':'" + $("#<%=this.BookTextBox.ClientID%>").val() + "', 'Genre':'" + $("#<%=this.GenreTextBox.ClientID%>").val() + "','Pages':'" + $("#<%=this.PagesTextBox.ClientID%>").val() + "}",


                });
                mytable.items.push({ Book: $("#<%=this.BookTextBox.ClientID%>").val(), Genre: $("#<%=this.GenreTextBox.ClientID%>").val(), Pages: $("#<%=this.PagesTextBox.ClientID%>").val() });
                var bookdata = $("#<%=this.BookTextBox.ClientID%>").val();
                var genredata = $("#<%=this.GenreTextBox.ClientID%>").val();
                var pagesdata = $("#<%=this.PagesTextBox.ClientID%>").val();
                ASPprac2.myService.savedata(bookdata, genredata, pagesdata);
                for (var i = 0; i < mytable.items.length; i++) {
                    $('#MyTableVis > tbody').append("<tr class='point'><td style='color:gray'>" + mytable.items[i].Book + "</td><td style='color:gray'>" + mytable.items[i].Genre + "</td><td style='color:gray'>" + mytable.items[i].Pages + "</td></tr>");
                }
                mytable.items.shift();
            }

在代码后面我有方法

 [WebMethod]
        public void InsertMethod(string Book, string Genre, string Pages)
        {
            mylist = new List<string>();
            mylist.Add(Book);
            mylist.Add(Genre);
            mylist.Add(Pages);

        }

当我点击按钮保存时,它将我的数据保存在数据库中

  protected void AddAuthorBase_Click(object sender, EventArgs e)
        {
            if (!this.Page.IsValid)
                return;
            try
            {
                var author = new Author
                {
                    LastName = LastNameTextBox.Text,
                    FirstName = FirstNameTextBox.Text,
                    MiddleName = MiddleNameTextBox.Text,
                    BirthDate = DateTime.Parse(BirthDateTextBox.Text)
                };
                _mymodel.Authors.Add(author);
                _mymodel.SaveChanges();

                var book = new Book
                {
                    AuthorID = author.AuthorID,
                    Book1 = mylist[0],
                    Genre = mylist[1],
                    Pages = Int32.Parse(mylist[2])
                };
                _mymodel.Books.Add(book);
                _mymodel.SaveChanges();

            }
            catch (Exception ex)
            {
                ErrorOutput.Text = ex.Message;
            }
        }
4

0 回答 0