简体   繁体   English

用linq在asp.net中更新不起作用

[英]Update in asp.net with linq not working

I have a strange problem. 我有一个奇怪的问题。 I am trying to update some fields using linq and it is not working. 我正在尝试使用linq更新某些字段,但它不起作用。 I tried to build the site and debug it which works and shows that the function is called. 我试图构建该站点并对其进行调试,并显示该函数已被调用。 I also tried manually adding the values in the code behind file and that works too but somehow on runtime the form doesnt pass the values back from the text fields. 我还尝试了在文件后面的代码中手动添加值,该方法也可以工作,但是在运行时,窗体无法将值从文本字段传递回去。

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AddEditBook1.aspx.cs" MasterPageFile="../AdminMaster.master" Inherits="AdminPanel_AddEditBook1" Title="Add or Edit Book Page"%>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPagePlaceHolder1" Runat="Server">
<div class="content"> 
    <div class="titleMain">Library -> Add or Edit Book Page
    </div>
    <h1><span></span>Add or Edit Book Page</h1>

    <div class="contentpage" >
        <p>On this page you can Add or Edit Book Page</p>

        <asp:Label ID="AddEditBookMsg" runat="server" Visible="false" />

            <table width="650"  class="table" cellspacing="15">
                <tr>
                    <td>Book ID</td>
                    <td><asp:Label ID="BookId" runat="server" /></td>
                </tr>
                <tr>
                    <td>Book name</td>
                    <td><asp:TextBox ID="BookName" runat="server" /></td>
                </tr>
                <tr>
                    <td>Author</td>
                    <td><asp:TextBox ID="BookAuthor" runat="server" Text="" /></td>
                </tr>

Code: 码:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class AdminPanel_AddEditBook1 : System.Web.UI.Page
{
    // book represents 3 tables
    protected void Page_Load(object sender, EventArgs e)
    {
        AddEditBookMsg.Visible = false;
        if (Request["book_id"] != null)
        {
            GetExistingBook();
        }
    }

    public void GetExistingBook()
    {
        BooksDataContext book = new BooksDataContext();
        int book_id = Int32.Parse(Request["book_id"].ToString());
        var bookexist = book.books.Single(b => b.book_id == book_id);
        var bookDDL = from c in book.categories
                      select c;
        var bookexistdesc = book.bookdescs.Single(bds => bds.book_id == book_id);
        if (bookexist.book_id == book_id)
        {
            BookName.Text = bookexist.book_name;
            BookAuthor.Text = bookexist.book_author;
            BookShortDesc.Text = bookexist.book_short_desc;
            BookLongDesc.Text = bookexistdesc.book_long_desc;
            BookImg.Text = bookexist.book_img;
            BookSqu.Text = bookexist.book_squ;
            BookArrived.Text = bookexist.book_arrived.ToString();
            BookQty.Text = bookexist.book_qty.ToString();

            if (!Page.IsPostBack)
            {
                CaID.DataSource = bookDDL;
                CaID.DataTextField = "ca_name";
                CaID.DataValueField = "ca_id";
                CaID.DataBind();
            }
        }
    }

    protected void update_Click(object sender, EventArgs e)
    {
        BooksDataContext book = new BooksDataContext();

        int book_id = Int32.Parse(Request["book_id"].ToString());
        book bookexist2 = book.books.Single(b1 => b1.book_id == book_id);
        bookdesc bookexistdesc2 = book.bookdescs.Single(bds1 => bds1.book_id == book_id);


        bookexist2.book_name = BookName.Text;
        bookexist2.book_author = BookAuthor.Text.ToString();
        bookexist2.book_short_desc = BookShortDesc.Text;

        bookexist2.book_img = BookImg.Text;
        bookexist2.book_squ = BookSqu.Text;
        bookexist2.book_arrived = DateTime.Parse(BookArrived.Text);
        bookexist2.book_qty = Int32.Parse(BookQty.Text);

        bookexistdesc2.book_long_desc = BookLongDesc.Text;
        book.SubmitChanges();
    }
}

You forgot to check if it is a postback in Page_Load. 您忘记了在Page_Load中检查它是否是回发。 You'll reload from the db on postback too... 您也将在回发时从数据库重新加载...

if (!IsPostback())
{
  if (Request["book_id"] != null)
  {
    GetExistingBook();
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM