将FormView绑定到基础DataSource [英] Binding a FormView to an underlying DataSource
问题描述
我创建了一个< formview>我的代码中的对象,根据这篇文章中提供的示例。
但是,即使我已经创建了从FormView中删除,插入和更新的事件,用户似乎无法更新数据。
I have created a little <formview> object within my code, according to the examples provided in this article.
However, even though I've created events for deleting, inserting, and updating from the FormView, the user, has seemingly no way to update the data.
<%@ 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:FormView ID="Envelope" runat="server" DataSourceID="SqlDataSource1"
AllowPaging="True"
OnItemDeleted = "ArticleEnvelope_Deleted"
OnItemInserted = "ArticleEnvelope_Inserted"
OnItemUpdated = "ArticleEnvelope_Updated">
<HeaderTemplate>
</HeaderTemplate>
<EditItemTemplate>
Timestamp:
<asp:TextBox ID="TimestampTextBox" runat="server"
Text='<%# Bind("Timestamp") %>' />
<br />
Subject:
<asp:TextBox ID="SubjectTextBox" runat="server" Text='<%# Bind("Subject") %>' />
<br />
EntryText:
<asp:TextBox ID="EntryTextTextBox" runat="server"
Text='<%# Bind("EntryText") %>' />
<br />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True"
CommandName="Update" Text="Update" />
<asp:LinkButton ID="UpdateCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
<InsertItemTemplate>
Timestamp:
<asp:TextBox ID="TimestampTextBox" runat="server"
Text='<%# Bind("Timestamp") %>' />
<br />
Subject:
<asp:TextBox ID="SubjectTextBox" runat="server" Text='<%# Bind("Subject") %>' />
<br />
EntryText:
<asp:TextBox ID="EntryTextTextBox" runat="server"
Text='<%# Bind("EntryText") %>' />
<br />
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True"
CommandName="Insert" Text="Insert" />
<asp:LinkButton ID="InsertCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</InsertItemTemplate>
<ItemTemplate>
Timestamp:
<asp:Label ID="TimestampLabel" runat="server" Text='<%# Bind("Timestamp") %>' />
<br />
Subject:
<asp:Label ID="SubjectLabel" runat="server" Text='<%# Bind("Subject") %>' />
<br />
EntryText:
<asp:Label ID="EntryTextLabel" runat="server" Text='<%# Bind("EntryText") %>' />
<br />
</ItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:couch_dbConnectionString %>"
SelectCommand="SELECT [Timestamp], [Subject], [EntryText] FROM [Article]"></asp:SqlDataSource>
</div>
</form>
</body>
</html>
Code-behind:
Code-behind:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
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;
using System.Web.Configuration;
public partial class Articles : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.Title = "Jon's Couch";
}
protected void ArticleEnvelope_Deleted(object sender, EventArgs e)
{
//TODO: Update code...
Envelope.DataBind();
}
protected void ArticleEnvelope_Inserted(object sender, EventArgs e)
{
//TODO: Update code...
Envelope.DataBind();
}
protected void ArticleEnvelope_Updated(object sender, EventArgs e)
{
//TODO: update code...
Envelope.DataBind();
}
}
How do I create controls, that, would allow me to update, insert, and delete information using the FormView?
Also it seems that I have found no solution to include this solution within a simple user control (ascx), am I missing something out?
Source code here.
:confused:
How do I create controls, that, would allow me to update, insert, and delete information using the FormView?
Also it seems that I have found no solution to include this solution within a simple user control (ascx), am I missing something out?
Source code here.
:confused:
推荐答案
ConnectionStrings:couch_dbConnectionString %>\"
SelectCommand=\"SELECT [Timestamp], [Subject], [EntryText] FROM [Article]\"></asp:SqlDataSource>
</div>
</form>
</body>
</html>
ConnectionStrings:couch_dbConnectionString %>" SelectCommand="SELECT [Timestamp], [Subject], [EntryText] FROM [Article]"></asp:SqlDataSource> </div> </form> </body> </html>
Code-behind:
Code-behind:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
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;
using System.Web.Configuration;
public partial class Articles : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.Title = "Jon's Couch";
}
protected void ArticleEnvelope_Deleted(object sender, EventArgs e)
{
//TODO: Update code...
Envelope.DataBind();
}
protected void ArticleEnvelope_Inserted(object sender, EventArgs e)
{
//TODO: Update code...
Envelope.DataBind();
}
protected void ArticleEnvelope_Updated(object sender, EventArgs e)
{
//TODO: update code...
Envelope.DataBind();
}
}
How do I create controls, that, would allow me to update, insert, and delete information using the FormView?
Also it seems that I have found no solution to include this solution within a simple user control (ascx), am I missing something out?
Source code here.
:confused:
How do I create controls, that, would allow me to update, insert, and delete information using the FormView?
Also it seems that I have found no solution to include this solution within a simple user control (ascx), am I missing something out?
Source code here.
:confused:
you need to do some minor changes in your FormView Defination
as
put two buttons in ItemTemplate of FormView as
you need to do some minor changes in your FormView Defination
as
put two buttons in ItemTemplate of FormView as
Edit:
<asp:Button ID="Edit" runat="server" Text="Edit" CommandName="edit" />
<br />
New:
<asp:Button ID="new" runat="server" Text="New" CommandName="new" />
<br />
;
and put a code in ItemCommand Event Handler of your FormView..
as
and put a code in ItemCommand Event Handler of your FormView..
as
protected void Envelope_ItemCommand(object sender, FormViewCommandEventArgs e)
{
if (e.CommandName.ToLower() == "new")
Envelope.DefaultMode = FormViewMode.Insert;
else if (e.CommandName.ToLower() == "cancel")
Envelope.DefaultMode = FormViewMode.ReadOnly;
}
Now if you want to Insert or Update from CodeBehind, means you want manually write the Command and Connection objects code then do that in
FormView_Updated Event Handler and FormView_Inserted Event Handler
or you can also do that from you SqlDataSource by setting the Insert,Update and Delete Command of SqlDataSource
Hope you will get some help from this..
Now if you want to Insert or Update from CodeBehind, means you want manually write the Command and Connection objects code then do that in
FormView_Updated Event Handler and FormView_Inserted Event Handler
or you can also do that from you SqlDataSource by setting the Insert,Update and Delete Command of SqlDataSource
Hope you will get some help from this..
这篇关于将FormView绑定到基础DataSource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!