FormView(ASP.NET)...正在更新对SQL数据库的更改? [英] FormView (ASP.NET) ...Updating changes to SQL Database?

查看:78
本文介绍了FormView(ASP.NET)...正在更新对SQL数据库的更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注本文,以及

I''m following this article, and, this MSDN article, to try to get an <asp:formview> that updates changes to a database, including: browsing through data, editing, inserting new items, and, deleting.

I''m stuck at the problem where I would like to save changes updated within two textboxes (txtSubject and txtBody - see Articles.aspx). However, when I update the text, nothing happens.

I''m using Visual Studio 2008 / .NET 3.5 SP1 / WinXP SP3 / IIS 5.1 on my development pc.

Any ideas please?

:confused:

Articles.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Articles.aspx.cs" Inherits="Articles" %>
<!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 id="Head1" runat="server">
    <link href="StyleSheet.css" rel="stylesheet" type="text/css" />
    <title>@Jon's</title>
</head>
<body>
    <form id="form1" runat="server">
    <h1>@Jon's <img src="images/sofa.jpg" alt="logo"/> </h1 >
    <div style="float:left;width:122px">
        <p style="width: 122px; height: 663px; margin-top: 0px; margin-right: 0px;">
        <a href="About.aspx">About me</a> <br />
        <a href="Diary.aspx">My diary</a> <br />
        <a href="Articles.aspx">Articles</a> <br />
        <a href="Bookmarks.aspx">My bookmarks</a> <br />
        <a href="Promotions.aspx">Promotions</a> <br />
        <a href="Resume.aspx">My resume</a> <br />
        <a href="Warez.aspx">Filesharing</a> <br />
        </p>
      </div>
   <div>
   <asp:Calendar ID="calArticles" runat="server" BackColor="White"

    BorderColor="#999999" CellPadding="4" DayNameFormat="Shortest"

    Font-Names="Verdana" Font-Size="8pt" ForeColor="Black" Height="180px"

    Width="200px" onselectionchanged="calArticles_SelectionChanged">
    <SelectedDayStyle BackColor="#666666" Font-Bold="True" ForeColor="White" />
    <SelectorStyle BackColor="#CCCCCC" />
    <WeekendDayStyle BackColor="#4AA02C" />
    <TodayDayStyle BackColor="#CCCCCC" ForeColor="Black" />
    <OtherMonthDayStyle ForeColor="#808080" />
    <NextPrevStyle VerticalAlign="Bottom" />
    <DayHeaderStyle BackColor="#CCCCCC" Font-Bold="True" Font-Size="7pt" />
    <TitleStyle BackColor="#999999" BorderColor="Black" Font-Bold="True" />
</asp:Calendar>
   <asp:FormView ID="Envelope" runat="server" DataSourceID="SqlDataSource1"

            AllowPaging="True"

            OnItemDeleted = "ArticleEnvelope_Deleted"

            OnItemInserted = "ArticleEnvelope_Inserted"

            OnItemUpdated = "ArticleEnvelope_Updated">
        <HeaderTemplate>
        </HeaderTemplate>
        <EditItemTemplate>
        <p> <asp:TextBox runat="server" Width="1000" ReadOnly="false" ID="txtSubject" Text='<%# Bind("Subject")%>'></asp:TextBox> </p>
        <p> <asp:TextBox runat="server" Height="300" Width="1000" ReadOnly="false" ID="txtBody" TextMode="MultiLine" Text='<%# Bind("EntryText") %>'></asp:TextBox>
        <p>
            &nbsp;<asp:Button ID="btnNew" enabled="false" runat="server" Text="New" CommandName="New" />
            &nbsp;<asp:Button ID="btnEdit" enabled="false" runat="server" Text="Edit" CommandName="Edit" />
            &nbsp;<asp:Button ID="btnSave" enabled="true" runat="server" Text="Save" OnClick="btnSave_Click" CommandName="Update" />
            &nbsp;<asp:Button ID="btnCancel" enabled="true" runat="server" Text="Cancel" CommandName="Cancel" />
            &nbsp;<asp:Button ID="btnRefresh" enabled="false" runat="server" Text="Refresh" />
            &nbsp;<asp:Button ID="btnDelete" enabled="false" runat="server" Text="Delete"/>
        </p>
        </p>
        </EditItemTemplate>
         <InsertItemTemplate>
        <p> <asp:TextBox runat="server" Width="1000" ReadOnly="false" ID="txtSubject" Text='<%# Bind("Subject")%>'></asp:TextBox> </p>
        <p> <asp:TextBox runat="server" Height="300" Width="1000" ReadOnly="false" ID="txtBody" TextMode="MultiLine" Text='<%# Bind("EntryText") %>'></asp:TextBox>
        <p>
            &nbsp;<asp:Button enabled="false" ID="btnNew" runat="server" Text="New" CommandName="New" />
            &nbsp;<asp:Button enabled="false" ID="btnEdit" runat="server" Text="Edit" CommandName="Edit" />
            &nbsp;<asp:Button enabled="true" ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" CommandName="Update" />
            &nbsp;<asp:Button enabled="true" ID="btnCancel" runat="server" Text="Cancel" CommandName="Cancel" />
            &nbsp;<asp:Button enabled="false" ID="btnRefresh" runat="server" Text="Refresh" />
            &nbsp;<asp:Button enabled="false" ID="btnDelete" runat="server" Text="Delete"/>
        </p>
        </p>
        </InsertItemTemplate>
        <ItemTemplate>
        <p> <asp:TextBox runat="server" Width="1000" ReadOnly="false" ID="txtSubject" Text='<%# Bind("Subject")%>'></asp:TextBox> </p>
        <p> <asp:TextBox runat="server" Height="300" Width="1000" ReadOnly="false" ID="txtBody" TextMode="MultiLine" Text='<%# Bind("EntryText") %>'></asp:TextBox>
        <p>
            &nbsp;<asp:Button enabled="true" ID="btnNew" runat="server" Text="New" CommandName="New" />
            &nbsp;<asp:Button enabled="true" ID="btnEdit" runat="server" Text="Edit" CommandName="Edit" />
            &nbsp;<asp:Button enabled="true" ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" CommandName="Update" />
            &nbsp;<asp:Button enabled="true" ID="btnCancel" runat="server" Text="Cancel" CommandName="Cancel" />
            &nbsp;<asp:Button enabled="true" ID="btnRefresh" runat="server" Text="Refresh" />
            &nbsp;<asp:Button enabled="true" ID="btnDelete" runat="server" Text="Delete"/>
        </p>
        </p>
        </ItemTemplate>
        </asp:FormView>
<p>
    <asp:TextBox ID="txtSearch" runat="server" Width="211px"></asp:TextBox>
    <asp:Button ID="btnSearch" runat="server" Text="Search" />

</p>
<p>
    <asp:Label ID="lblFooter" runat="server"></asp:Label>

</p>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"

        ConnectionString="<%$ ConnectionStrings:couch_dbConnectionString %>"

        SelectCommand="SELECT [Timestamp], [Subject], [EntryText] FROM [Article]"

        UpdateCommand="UPDATE [Article] SET [Timestamp] = @Timestamp, [Subject] = @Subject, [EntryText] = @EntryText WHERE [EntryID] = @EntryID;"

        DeleteCommand="DELETE FROM [Article] WHERE [EntryID]=@EntryID"

        InsertCommand="INSERT INTO [Article] VALUES [Timestamp]=@Timestamp, [Subject] = @Subject, [EntryText] = @EntryText;"> </asp:SqlDataSource>
</div>
</form>
</body>
</html>


Articles.aspx.cs


Articles.aspx.cs

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 Articles : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.Title = "Jon's Couch";
        Envelope.DataBind();
    }
    protected void calArticles_SelectionChanged(object sender, EventArgs e)
    {
            SqlDataSource1.SelectCommand =
            System.String.Concat("SELECT [Timestamp], [Subject], [EntryText] FROM [Article] WHERE [Timestamp] >= '" +
            calArticles.SelectedDate.ToString() + "' AND [Timestamp] < (DATEADD(day, 1, [Timestamp]));");
            Envelope.DataBind();
    }
    protected void ArticleEnvelope_Inserted(object sender, EventArgs e)
    {
        Envelope.DataBind();
    }
    protected void ArticleEnvelope_Deleted(object sender, EventArgs e)
    {
    }
    protected void ArticleEnvelope_Updated(object sender, EventArgs e)
    {
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        Envelope.DataBind();
    }
}



Source code
here, and, I also tried a separate project as proof of concept...doesn''t work either.

:confused:



Source code here, and, I also tried a separate project as proof of concept...doesn''t work either.

:confused:

推荐答案

ConnectionStrings:couch_dbConnectionString %> " SelectCommand="SELECT [Timestamp], [Subject], [EntryText] FROM [Article]" UpdateCommand="UPDATE [Article] SET [Timestamp] = @Timestamp, [Subject] = @Subject, [EntryText] = @EntryText WHERE [EntryID] = @EntryID;" DeleteCommand="DELETE FROM [Article] WHERE [EntryID]=@EntryID" InsertCommand="INSERT INTO [Article] VALUES [Timestamp]=@Timestamp, [Subject] = @Subject, [EntryText] = @EntryText;"> </asp:SqlDataSource> < /div > < /form > < /body > < /html >
ConnectionStrings:couch_dbConnectionString %>" SelectCommand="SELECT [Timestamp], [Subject], [EntryText] FROM [Article]" UpdateCommand="UPDATE [Article] SET [Timestamp] = @Timestamp, [Subject] = @Subject, [EntryText] = @EntryText WHERE [EntryID] = @EntryID;" DeleteCommand="DELETE FROM [Article] WHERE [EntryID]=@EntryID" InsertCommand="INSERT INTO [Article] VALUES [Timestamp]=@Timestamp, [Subject] = @Subject, [EntryText] = @EntryText;"> </asp:SqlDataSource> </div> </form> </body> </html>


Articles.aspx.cs


Articles.aspx.cs

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 Articles : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.Title = "Jon's Couch";
        Envelope.DataBind();
    }
    protected void calArticles_SelectionChanged(object sender, EventArgs e)
    {
            SqlDataSource1.SelectCommand =
            System.String.Concat("SELECT [Timestamp], [Subject], [EntryText] FROM [Article] WHERE [Timestamp] >= '" +
            calArticles.SelectedDate.ToString() + "' AND [Timestamp] < (DATEADD(day, 1, [Timestamp]));");
            Envelope.DataBind();
    }
    protected void ArticleEnvelope_Inserted(object sender, EventArgs e)
    {
        Envelope.DataBind();
    }
    protected void ArticleEnvelope_Deleted(object sender, EventArgs e)
    {
    }
    protected void ArticleEnvelope_Updated(object sender, EventArgs e)
    {
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        Envelope.DataBind();
    }
}



Source code here, and, I also tried a separate project as proof of concept...doesn''t work either.

:confused:



Source code here, and, I also tried a separate project as proof of concept...doesn''t work either.

:confused:


You provided source code, but, didn''t provide your database backup. So, couldn''t try to debug the codes in Visual Studio.

However, are you trying to update data using the btnSave_Click()? If that is so, instead of using the following code to update data:

You provided source code, but, didn''t provide your database backup. So, couldn''t try to debug the codes in Visual Studio.

However, are you trying to update data using the btnSave_Click()? If that is so, instead of using the following code to update data:

<asp:Button enabled="true" ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" CommandName="Update" />





protected void btnSave_Click(object sender, EventArgs e)
{
       Envelope.DataBind();
}



You should try to remove the OnClick="btnSave_Click" event handler and see what happens:



You should try to remove the OnClick="btnSave_Click" event handler and see what happens:

<asp:Button enabled="true" ID="btnSave" runat="server" Text="Save" CommandName="Update" />



Because, using the "CommandName" property of the Button, the Asp.net runtime binds the Button''s update command with the Update command of the SqlDataSource and executes the Update Query.

But, if you really want to use the btnSaveClick() method , you should write your own codes to update data (Like the following you''ve already written):



Because, using the "CommandName" property of the Button, the Asp.net runtime binds the Button''s update command with the Update command of the SqlDataSource and executes the Update Query.

But, if you really want to use the btnSaveClick() method , you should write your own codes to update data (Like the following you''ve already written):

protected void calArticles_SelectionChanged(object sender, EventArgs e)
{
           SqlDataSource1.SelectCommand =
           System.String.Concat("SELECT [Timestamp], [Subject], [EntryText] FROM [Article] WHERE [Timestamp] >= '" +
           calArticles.SelectedDate.ToString() + "' AND [Timestamp] < (DATEADD(day, 1, [Timestamp]));");
           Envelope.DataBind();
}




I can suggest you the following link to see how to update data using FormView and SqlDataSource:

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.formview.aspx[^]




I can suggest you the following link to see how to update data using FormView and SqlDataSource:

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.formview.aspx[^]


这篇关于FormView(ASP.NET)...正在更新对SQL数据库的更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆