Click here to Skip to main content
16,004,778 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

XML
<%@ 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" />
            &nbsp;<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" />
             &nbsp;<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:
C#
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:
Posted
Updated 3-Sep-10 5:12am
v3

1 solution

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
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..
 
Share this answer
 
Comments
koool.kabeer 4-Sep-10 5:14am    
vote me how much of my code helped u
jon-80 4-Sep-10 5:30am    
Thanks, so far the only issue is how to manually add the event handler for the button:

Articles.aspx
<asp:linkbutton id="InsertButton" runat="server" causesvalidation="True"
="" commandname="Insert" text="Insert" onclick="InsertButton_Click">

Articles.aspx.cs

public partial class Articles : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.Title = "Jon's Couch";

InsertButton_Click += new System.EventHandler(this.InsertButton_Click);

}

protected void InsertButton_Click(object sender, FormViewCommandEventArgs e)
{
if (e.CommandName == "new")
{
Envelope.DefaultMode = FormViewMode.Insert;
}
}
}
jon-80 4-Sep-10 5:33am    
I'm following MSDN articles at http://msdn.microsoft.com/en-us/library/6w2tb12s(v=VS.90).aspx - How to: Dynamically Bind Event Handlers at Run Time in ASP.NET Web Pages, and, http://msdn.microsoft.com/en-us/library/t3d01ft1(v=VS.90).aspx - How to: Dynamically Bind Event Handlers at Run Time in ASP.NET Web Pages.
jon-80 4-Sep-10 5:42am    
Source code at http://cid-b712073b3513eb8e.office.live.com/self.aspx/.Public/dumps/Jon%5E4s%20Couch.rar
koool.kabeer 4-Sep-10 5:44am    
ok whatever you are following to bind event handlers at runtime or you just giving the event handlers at the design time.....
the part..
if (e.CommandName == "new")
{
Envelope.DefaultMode = FormViewMode.Insert;
}

must be in ItemCommand Event Handler...
not in InsertButton_Click..

you can do this instead of giving the EventHandler for click event...

just set the CommandName of your Button as "new" in the PageLoad...
and then just write the code of ItemCommand as I stated in the answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900