formview ItemTemplate formating

I have a formView on one of my pages as described in the code Snippet.
The <ItemTemplate> section has a table with background (looks different then the <InsertItemTemplate> and <EditItemTemplate> ).
To apply same look to <InsertItemTemplate> and <EditItemTemplate> I need to cut and past those lines to these sections as well.
*** Question / Problem :
Is there a way to define a single template look that will be applied to all The <ItemTemplate>, <InsertItemTemplate> and <EditItemTemplate> ?

<asp:FormView ID="FormView1" runat="server" DataSourceID="SqlDataSource1" 
        OnItemDeleted="FormView1_ItemDeleted" OnItemInserted="FormView1_ItemInserted"
        OnItemUpdated="FormView1_ItemUpdated">
        <EditItemTemplate>
            <asp:Label ID="GroupIdLabel1" Visible="false" runat="server" Text='<%# Eval("GroupId") %>'></asp:Label><br />
            GROUP_NAME:
            <asp:TextBox ID="GROUP_NAMETextBox" Enabled="false" runat="server" Text='<%# Bind("GROUP_NAME") %>'>
            </asp:TextBox><br />
            GROUP_DESCRIPTION:
            <asp:TextBox ID="GROUP_DESCRIPTIONTextBox" runat="server" Text='<%# Bind("GROUP_DESCRIPTION") %>'>
            </asp:TextBox><br />
            <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
                Text="Update">
            </asp:LinkButton>
            <asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
                Text="Cancel" PostBackUrl="~/AdminScripts/GroupList.aspx">
            </asp:LinkButton>
        </EditItemTemplate>
        <InsertItemTemplate>
            GROUP_NAME:
            <asp:TextBox ID="GROUP_NAMETextBox" runat="server" Text='<%# Bind("GROUP_NAME") %>'>
            </asp:TextBox><br />
            GROUP_DESCRIPTION:
            <asp:TextBox ID="GROUP_DESCRIPTIONTextBox" runat="server" Text='<%# Bind("GROUP_DESCRIPTION") %>'>
            </asp:TextBox><br />
            <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"
                Text="Insert">
            </asp:LinkButton>
            <asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
                Text="Cancel" PostBackUrl="~/AdminScripts/GroupList.aspx">
            </asp:LinkButton>
        </InsertItemTemplate>
        <ItemTemplate>
            <table bgcolor="gray" width="100%" border="2">
                <tr>
                    <td width="75%">
                        GROUP_NAME:
                        <asp:Label ID="GROUP_NAMELabel" runat="server" Text='<%# Bind("GROUP_NAME") %>'>
                        </asp:Label><br />
                        GROUP_DESCRIPTION:
                        <asp:Label ID="GROUP_DESCRIPTIONLabel" runat="server" Text='<%# Bind("GROUP_DESCRIPTION") %>'>
                        </asp:Label><br />
                        <asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete"
                            Text="Delete">
                        </asp:LinkButton>
                        <asp:LinkButton ID="CancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
                            Text="Cancel" PostBackUrl="~/AdminScripts/GroupList.aspx">
                        </asp:LinkButton>
                    </td>
                </tr>
            </table>
        </ItemTemplate>
    </asp:FormView>

Open in new window

LVL 1
vu3lmgAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David RobitailleAnalyst ProgrammerCommented:
you could copy Template  and or template style
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not (Page.IsPostBack) Then
 
                        FormView1.InsertItemTemplate = FormView1.EditItemTemplate
 
                    End If
    End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vu3lmgAuthor Commented:
davrob60
I am trying the way you've suggested.  
I have hidden all the action buttons and make them visible depending on the Action.
It fails at "InsertButton.Visible = true;" (please refer the Attached code snippet)
Error : " System.NullReferenceException: Object reference not set to an instance of an object."
        protected void Page_Load(object sender, EventArgs e)
        {
            TextBox GROUP_NAMETextBox = ((TextBox)FormView1.FindControl("GROUP_NAMETextBox"));
            Button InsertButton = ((Button)FormView1.FindControl("InsertButton"));
            Button UpdateButton = ((Button)FormView1.FindControl("UpdateButton"));
            Button DeleteButton = ((Button)FormView1.FindControl("DeleteButton"));
            TextBox GROUP_DESCRIPTIONTextBox = ((TextBox)FormView1.FindControl("GROUP_DESCRIPTIONTextBox"));
            DropDownList LimitQueryIdDropDownList = ((DropDownList)FormView1.FindControl("LimitQueryIdDropDownList"));
 
            if (!Page.IsPostBack)
            {
                switch (Request.QueryString["Action"].ToString())
                {
                    case "Add":
                        FormView1.InsertItemTemplate = FormView1.ItemTemplate;
                        FormView1.ChangeMode(FormViewMode.Insert);
                        InsertButton.Visible = true;
                        break;
 
                    case "Edit":
                        FormView1.EditItemTemplate = FormView1.ItemTemplate;
                        //FormView1.ChangeMode(FormViewMode.Edit);
                        UpdateButton.Visible = true;
                        GROUP_NAMETextBox.Enabled = false;
                        break;
 
                    case "Delete":
                        DeleteButton.Visible = true;
                        GROUP_NAMETextBox.Enabled = false;
                        GROUP_DESCRIPTIONTextBox.Enabled = false;
                        LimitQueryIdDropDownList.Enabled = false;
                        FormView1.ChangeMode(FormViewMode.ReadOnly);
                        break;
                }
            }
        }

Open in new window

0
David RobitailleAnalyst ProgrammerCommented:
try to add this to get the button
it s VB.net, but just ask if you dont understand and need it in C#
dim InsertButton as button = ctype(FormView1.findcontrol("InsertButton"), button )
0
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

David RobitailleAnalyst ProgrammerCommented:
note :  i usually do me adjustment in the formview_DataBound event, i dont know if that work in the page load.
(i copy template in the page load, but toggle visibility on the DataBound)
 
0
David RobitailleAnalyst ProgrammerCommented:
here an exemple
'since we use the same template for insert and update, 
            If FormViewProduct.CurrentMode = FormViewMode.Edit Or _
               FormViewProduct.CurrentMode = FormViewMode.Insert Then
 
                'set the template as insert or edit
                Dim InsertButton1 As LinkButton = CType(FormViewProduct.FindControl("InsertButton1"), LinkButton)
                Dim InsertButton2 As LinkButton = CType(FormViewProduct.FindControl("InsertButton2"), LinkButton)
                Dim InsertCancelButton1 As HyperLink = CType(FormViewProduct.FindControl("InsertCancelButton1"), HyperLink)
                Dim InsertCancelButton2 As HyperLink = CType(FormViewProduct.FindControl("InsertCancelButton2"), HyperLink)
 
                Dim UpdateButton1 As LinkButton = CType(FormViewProduct.FindControl("UpdateButton1"), LinkButton)
                Dim UpdateButton2 As LinkButton = CType(FormViewProduct.FindControl("UpdateButton2"), LinkButton)
                Dim UpdateCancelButton1 As LinkButton = CType(FormViewProduct.FindControl("UpdateCancelButton1"), LinkButton)
                Dim UpdateCancelButton2 As LinkButton = CType(FormViewProduct.FindControl("UpdateCancelButton2"), LinkButton)
 
 
                UpdateButton1.Visible = (FormViewProduct.CurrentMode = FormViewMode.Edit)
                UpdateButton2.Visible = UpdateButton1.Visible
                UpdateCancelButton1.Visible = UpdateButton1.Visible
                UpdateCancelButton2.Visible = UpdateButton1.Visible
 
                InsertButton1.Visible = Not UpdateButton1.Visible
                InsertButton2.Visible = InsertButton1.Visible
                InsertCancelButton1.Visible = InsertButton1.Visible
                InsertCancelButton2.Visible = InsertButton1.Visible
end if

Open in new window

0
vu3lmgAuthor Commented:
davrob60
I tried to move the code in _DataBound event as you've suggested (check the code here).
Still same error, this time in _DataBound event.
It shows the Updatebutton in edit mode without any problems.
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                switch (Request.QueryString["Action"].ToString())
                {
                    case "Add":
                        FormView1.InsertItemTemplate = FormView1.ItemTemplate;
                        //FormView1.ChangeMode(FormViewMode.Insert);
                        break;
 
                    case "Edit":
                        FormView1.EditItemTemplate = FormView1.ItemTemplate;
                        //FormView1.ChangeMode(FormViewMode.Edit);
                        break;
 
                    case "Delete":
                        FormView1.ChangeMode(FormViewMode.ReadOnly);
                        break;
                }
            }
        }
 
        protected void FormView1_DataBound(object sender, EventArgs e)
        {
            TextBox GROUP_NAMETextBox = ((TextBox)FormView1.FindControl("GROUP_NAMETextBox"));
            Button InsertButton = ((Button)FormView1.FindControl("InsertButton"));
            Button UpdateButton = ((Button)FormView1.FindControl("UpdateButton"));
            Button DeleteButton = ((Button)FormView1.FindControl("DeleteButton"));
            TextBox GROUP_DESCRIPTIONTextBox = ((TextBox)FormView1.FindControl("GROUP_DESCRIPTIONTextBox"));
            DropDownList LimitQueryIdDropDownList = ((DropDownList)FormView1.FindControl("LimitQueryIdDropDownList"));
 
            switch (Request.QueryString["Action"].ToString())
            {
                case "Add":
                    InsertButton.Visible = true;
                    break;
 
                case "Edit":
                    UpdateButton.Visible = true;
                    GROUP_NAMETextBox.Enabled = false;
                    break;
 
                case "Delete":
                    DeleteButton.Visible = true;
                    GROUP_NAMETextBox.Enabled = false;
                    GROUP_DESCRIPTIONTextBox.Enabled = false;
                    LimitQueryIdDropDownList.Enabled = false;
                    break;
            }
        }

Open in new window

0
David RobitailleAnalyst ProgrammerCommented:
  1. look like you use a asp:LinkButton so you should cast them as linkboton (LinkButton InsertButton = ((LinkButton )FormView1.FindControl("InsertButton"));
    )
  2. don`t do a switch in the Page_Load, just copy all the templates.
  3. That will not change anything, but In the databound event, but you could use FormView1.CurrentMode instead of Request.QueryString["Action"].ToString())

0
David RobitailleAnalyst ProgrammerCommented:
if that still dont work, i need to see the new <asp:FormView code
0
vu3lmgAuthor Commented:
davrob60
Now I do not get the "NullReferenceException" error with the code attached below.
BUT it is not Editing or Inserting records.
Please have a look at code attached below (both Group.aspx and Group.aspx.cs
Why did you say "don't do a switch in the Page_Load" ?
//Group.aspx *************************************************************************************
<%@ Page Language="C#" MasterPageFile="~/Scripts/MasterPage.master" AutoEventWireup="true"
    CodeFile="Group.aspx.cs" Inherits="PARC.AdminScripts.Group" StylesheetTheme="FormView_Skin" %>
 
<asp:Content ID="Content1" ContentPlaceHolderID="cphPage" runat="Server">
    <br />
    <asp:Label ID="Label1" runat="server" Text="Group :" Font-Bold="true" Font-Size="medium"></asp:Label>&nbsp;
    <br />
    <br />
    <asp:FormView ID="FormView1" runat="server" DataSourceID="SqlDataSource1" DataKeyNames="GroupId"
        OnItemDeleted="FormView1_ItemDeleted" OnItemInserted="FormView1_ItemInserted"
        OnItemUpdated="FormView1_ItemUpdated" OnDataBound="FormView1_DataBound">
        <ItemTemplate>
            <table width="100%" border="2">
                <tr>
                    <td width="75%">
                        <asp:Label ID="GroupIdLabel" Visible="false" runat="server" Text='<%# Eval("GroupId")%>'></asp:Label><br />
                        <asp:Label Text="Group Name:" ID="Label2" runat="server"></asp:Label>
                        <asp:TextBox ID="GROUP_NAMETextBox" runat="server" Text='<%# Bind("GROUP_NAME")%>'>
                        </asp:TextBox><br />
                        <asp:Label Text="Group Description :" ID="Label3" runat="server"></asp:Label>
                        <asp:TextBox ID="GROUP_DESCRIPTIONTextBox" runat="server" Text='<%# Bind("GROUP_DESCRIPTION")%>'>
                        </asp:TextBox><br />
                        <asp:Label Text="Limit Query :" ID="Label4" runat="server"></asp:Label>
                        <asp:TextBox ID="LimitQueryIdTextBox" Enabled="false" runat="server" Text='<%# Bind("LimitQueryId")%>'>
                        </asp:TextBox>
                        <asp:DropDownList ID="LimitQueryIdDropDownList" runat="server" DataSourceID="SqlDataSource2"
                            DataTextField="QUERY_NAME" DataValueField="QueryId" Width="152px">
                        </asp:DropDownList><br />
                        <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:PARC %>"
                            SelectCommand="SELECT QueryId, QUERY_NAME, F_LIMIT_QUERY 
                                FROM QUERIES WHERE ([F_LIMIT_QUERY] = @F_LIMIT_QUERY)">
                            <SelectParameters>
                                <asp:Parameter DefaultValue="1" Name="F_LIMIT_QUERY" Type="Int32" />
                            </SelectParameters>
                        </asp:SqlDataSource>
                        <br />
                        <asp:Button ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
                            Text="Update" Visible="False" />
                        <asp:Button ID="DeleteButton" runat="server" CausesValidation="True" CommandName="Delete"
                            Text="Delete" Visible="False" />
                        <asp:Button ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"
                            Text="Insert" Visible="False" />
                        <asp:Button ID="CancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
                            Text="Cancel" PostBackUrl="~/AdminScripts/GroupList.aspx" />
                    </td>
                </tr>
            </table>
        </ItemTemplate>
    </asp:FormView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:PARC %>"
        DeleteCommand="DELETE FROM GROUPS WHERE GroupId = @GroupId" 
        InsertCommand="INSERT INTO GROUPS(GROUP_NAME, GROUP_DESCRIPTION, LimitQueryId) 
            VALUES (@GROUP_NAME, @GROUP_DESCRIPTION, @LimitQueryId)" 
        SelectCommand="sp_PARC_GetGroup" SelectCommandType="StoredProcedure" 
        UpdateCommand="UPDATE GROUPS SET GROUP_DESCRIPTION = @GROUP_DESCRIPTION, LimitQueryId = @LimitQueryId 
            WHERE GroupId = @GroupId">
        <SelectParameters>
            <asp:QueryStringParameter Name="GroupId" QueryStringField="GroupId" Type="Int16" />
        </SelectParameters>
        <UpdateParameters>
            <asp:Parameter Name="GROUP_DESCRIPTION" />
            <asp:Parameter Name="LimitQueryId" />
            <asp:Parameter Name="GroupId" />
        </UpdateParameters>
        <InsertParameters>
            <asp:Parameter Name="GROUP_NAME" />
            <asp:Parameter Name="GROUP_DESCRIPTION" />
            <asp:Parameter Name="LimitQueryId" />
        </InsertParameters>
        <DeleteParameters>
            <asp:Parameter Name="GroupId" />
        </DeleteParameters>
    </asp:SqlDataSource>
</asp:Content>
 
 
_______________________________________________________________________________________________
_______________________________________________________________________________________________
_______________________________________________________________________________________________
 
 
//Group.aspx.cs *************************************************************************************
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
 
namespace PARC.AdminScripts
{
    public partial class Group : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                switch (Request.QueryString["Action"].ToString()) //(FormView1.CurrentMode) 
                {
                    case "Add":  // FormViewMode.Insert:
                        FormView1.InsertItemTemplate = FormView1.ItemTemplate;
                        FormView1.ChangeMode(FormViewMode.Insert);
                        break;
 
                    case "Edit":
                        FormView1.EditItemTemplate = FormView1.ItemTemplate;
                        FormView1.ChangeMode(FormViewMode.Edit);
                        break;
 
                    case "Delete":
                        FormView1.ChangeMode(FormViewMode.ReadOnly);
                        break;
                }
            }
        }
        protected void FormView1_ItemUpdated(object sender, FormViewUpdatedEventArgs e)
        {
            Response.Redirect("~/AdminScripts/GroupList.aspx");
        }
        protected void FormView1_ItemDeleted(object sender, FormViewDeletedEventArgs e)
        {
            Response.Redirect("~/AdminScripts/GroupList.aspx");
        }
        protected void FormView1_ItemInserted(object sender, FormViewInsertedEventArgs e)
        {
            Response.Redirect("~/AdminScripts/GroupList.aspx");
        }
        protected void FormView1_DataBound(object sender, EventArgs e)
        {
            TextBox GROUP_NAMETextBox = ((TextBox)FormView1.FindControl("GROUP_NAMETextBox"));
            Button InsertButton = ((Button)FormView1.FindControl("InsertButton"));
            Button UpdateButton = ((Button)FormView1.FindControl("UpdateButton"));
            Button DeleteButton = ((Button)FormView1.FindControl("DeleteButton"));
            TextBox GROUP_DESCRIPTIONTextBox = ((TextBox)FormView1.FindControl("GROUP_DESCRIPTIONTextBox"));
            DropDownList LimitQueryIdDropDownList = ((DropDownList)FormView1.FindControl("LimitQueryIdDropDownList"));
 
            switch (Request.QueryString["Action"].ToString())
            {
                case "Add":
                    InsertButton.Visible = true;
                    break;
 
                case "Edit":
                    UpdateButton.Visible = true;
                    GROUP_NAMETextBox.Enabled = false;
                    break;
 
                case "Delete":
                    DeleteButton.Visible = true;
                    GROUP_NAMETextBox.Enabled = false;
                    GROUP_DESCRIPTIONTextBox.Enabled = false;
                    LimitQueryIdDropDownList.Enabled = false;
                    break;
            }
        }
    }
}

Open in new window

0
David RobitailleAnalyst ProgrammerCommented:
what did you get the NullReferenceException? witch line???
0
vu3lmgAuthor Commented:
davrob60
I am NOT getting that error anymore.
I was getting that error in the previous code (attached in one of my previous comments (Date:11.13.2008 at 04:50PM EST))
0
David RobitailleAnalyst ProgrammerCommented:

Could you place a breakpoint int the FormView1_ItemCommand to see if the Command is lunched? you could also check the e.CommandName to make sure the property is lunched.
there is also the FormView1_ItemInserting evant that you could check if raised.
 
on another subject That what i want to say in "don't do a switch in the Page_Load" :

protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
                    FormView1.InsertItemTemplate = FormView1.ItemTemplate;
                    FormView1.EditItemTemplate = FormView1.ItemTemplate;
 
 
            {
                switch (Request.QueryString["Action"].ToString()) //(FormView1.CurrentMode) 
                {
                    case "Add":  // FormViewMode.Insert:
                        FormView1.ChangeMode(FormViewMode.Insert);
                        break;
 
                    case "Edit":
                        FormView1.ChangeMode(FormViewMode.Edit);
                        break;
 
                    case "Delete":
                        FormView1.ChangeMode(FormViewMode.ReadOnly);
                        break;
                }
            }
        }

Open in new window

0
David RobitailleAnalyst ProgrammerCommented:
oops i missplaced a { , but i<m sure you understant what i meen...
0
vu3lmgAuthor Commented:
davrob60
I tried what you suggested above.  Still same, not inserting the record.
It is not raising the _ItemCommand or _ItemInserting events.
0
David RobitailleAnalyst ProgrammerCommented:
ok, then if those event are not raising it`s beacuse you got a problem with the button, the "click" event dont make its trip to the server. What happen if you set CausesValidation="false"?
<asp:Button ID="InsertButton" runat="server" CausesValidation="false" CommandName="Insert"
                            Text="Insert" Visible="False" />

0
vu3lmgAuthor Commented:
Same Problem.
0
David RobitailleAnalyst ProgrammerCommented:
i really dont know, your thigs should work.
the only difference with my code is that my buttons are visible by default and i set them invisible in the databound. you could try this, but i my hopes are tin...
you could also try to add a onclick event for those buton? then you could call fromview.update() but it<s patching work and i dont like it...
0
vu3lmgAuthor Commented:
When you say "my buttons are visible ...",  Did you create an actual project and try running my code ?
If yes, Is it working ?
0
David RobitailleAnalyst ProgrammerCommented:
no, i was speaking about one of my own project. i may try to run you code later, but i<m not able do do it right now.
0
vu3lmgAuthor Commented:
Ok let me know the result when you try out.
0
David RobitailleAnalyst ProgrammerCommented:
Thats weird but i moved the butons into the footer template and it a now magicly work!
 First time i saw this...
 
Anyway, try that and give me news

 </ItemTemplate>
        <FooterTemplate>
             <br />
                        <asp:Button ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
                            Text="Update" Visible="False" />
                        <asp:Button ID="DeleteButton" runat="server" CausesValidation="True" CommandName="Delete"
                            Text="Delete" Visible="False" />
                        <asp:Button ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"
                            Text="Insert" Visible="False" />
                        <asp:Button ID="CancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
                            Text="Cancel" PostBackUrl="~/AdminScripts/GroupList.aspx" />
        </FooterTemplate>
    </asp:FormView>

Open in new window

0
vu3lmgAuthor Commented:
Still same problem.
Can you plese post your code here.
0
David RobitailleAnalyst ProgrammerCommented:
here the default2 aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
 
<!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 runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
     <asp:Label ID="Label1" runat="server" Text="Group :" Font-Bold="true" Font-Size="medium"></asp:Label>&nbsp;
    <br />
    <br />
    <asp:FormView ID="FormView1" runat="server" DataSourceID="SqlDataSource1" DataKeyNames="Zone_ID"
        OnItemDeleted="FormView1_ItemDeleted" OnItemInserted="FormView1_ItemInserted"
        OnItemUpdated="FormView1_ItemUpdated" OnDataBound="FormView1_DataBound"  >
        <ItemTemplate>
            <table width="100%" border="2">
                <tr>
                    <td width="75%">
                        <asp:Label ID="GroupIdLabel" Visible="false" runat="server" Text='<%# Eval("Zone_ID")%>'></asp:Label><br />
                        <asp:Label Text="Group Name:" ID="Label2" runat="server"></asp:Label>
                        <asp:TextBox ID="GROUP_NAMETextBox" runat="server" Text='<%# Bind("Code")%>'>
                        </asp:TextBox><br />
                        <asp:Label Text="Group Description :" ID="Label3" runat="server"></asp:Label>
                        <asp:TextBox ID="GROUP_DESCRIPTIONTextBox" runat="server" Text='<%# Bind("Descr")%>'>
                        </asp:TextBox><br />
                        <asp:Label Text="Limit Query :" ID="Label4" runat="server"></asp:Label>
                        <asp:TextBox ID="LimitQueryIdTextBox" Enabled="false" runat="server" Text='<%# Bind("Transport_Unit_Price")%>'>
                        </asp:TextBox>
                        
                       
                    </td>
                </tr>
            </table>
        </ItemTemplate>
        <FooterTemplate>
             <br />
                        <asp:Button ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
                            Text="Update" Visible="False"  />
                        <asp:Button ID="DeleteButton" runat="server" CausesValidation="True" CommandName="Delete"
                            Text="Delete" Visible="False" />
                        <asp:Button ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"
                            Text="Insert" Visible="False" />
                        <asp:Button ID="CancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
                            Text="Cancel" PostBackUrl="~/AdminScripts/GroupList.aspx" />
        </FooterTemplate>
    </asp:FormView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:QSWebConnectionString %>"
        DeleteCommand="DELETE FROM [QSW_Zone] WHERE [Zone_ID] = @Zone_ID" 
        InsertCommand="INSERT INTO ..."
        SelectCommand="SELECT * FROM [QSW_Zone] WHERE ([Zone_ID] = @Zone_ID)" 
        
        UpdateCommand="UPDATE [QSW_Zone] SET [Descr] = @Descr,... WHERE [Zone_ID] = @Zone_ID">
        
          SelectParameters>
            <asp:Parameter DefaultValue="2" Name="Zone_ID" Type="Int32" />
        </SelectParameters>
        
    </asp:SqlDataSource>
 
 
    </form>
</body>
</html>

Open in new window

0
David RobitailleAnalyst ProgrammerCommented:
here the default.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 Default2 : System.Web.UI.Page
{
   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            switch (Request.QueryString["Action"].ToString()) //(FormView1.CurrentMode) 
            {
                case "Add":  // FormViewMode.Insert:
                    FormView1.InsertItemTemplate = FormView1.ItemTemplate;
                    FormView1.ChangeMode(FormViewMode.Insert);
                    break;
 
                case "Edit":
                    FormView1.EditItemTemplate = FormView1.ItemTemplate;
                    FormView1.ChangeMode(FormViewMode.Edit);
                    break;
 
                case "Delete":
                    FormView1.ChangeMode(FormViewMode.ReadOnly);
                    break;
            }
        }
    }
 
 
    protected void FormView1_ItemUpdated(object sender, FormViewUpdatedEventArgs e)
    {
        if (e.Exception != null)
        {
            Response.Redirect("~/AdminScripts/GroupList.aspx");
        }
    }
    protected void FormView1_ItemDeleted(object sender, FormViewDeletedEventArgs e)
    {
        Response.Redirect("~/AdminScripts/GroupList.aspx");
    }
    protected void FormView1_ItemInserted(object sender, FormViewInsertedEventArgs e)
    {
        Response.Redirect("~/AdminScripts/GroupList.aspx");
    }
    protected void FormView1_DataBound(object sender, EventArgs e)
    {
        TextBox GROUP_NAMETextBox = ((TextBox)FormView1.FindControl("GROUP_NAMETextBox"));
        Button InsertButton = ((Button)FormView1.FindControl("InsertButton"));
        Button UpdateButton = ((Button)FormView1.FindControl("UpdateButton"));
        Button DeleteButton = ((Button)FormView1.FindControl("DeleteButton"));
        TextBox GROUP_DESCRIPTIONTextBox = ((TextBox)FormView1.FindControl("GROUP_DESCRIPTIONTextBox"));
        DropDownList LimitQueryIdDropDownList = ((DropDownList)FormView1.FindControl("LimitQueryIdDropDownList"));
 
        switch (Request.QueryString["Action"].ToString())
        {
            case "Add":
                InsertButton.Visible = true;
                break;
 
            case "Edit":
                UpdateButton.Visible = true;
                GROUP_NAMETextBox.Enabled = false;
                break;
 
            case "Delete":
                DeleteButton.Visible = true;
                GROUP_NAMETextBox.Enabled = false;
                GROUP_DESCRIPTIONTextBox.Enabled = false;
                LimitQueryIdDropDownList.Enabled = false;
                break;
        }
    }
 
   
}

Open in new window

0
David RobitailleAnalyst ProgrammerCommented:
i put a breakpoint in the ItemUpdated event, and it lunch...
FormView1_ItemUpdated
i had to modify your thing to remove the master page and set the datasource to link to one of my own database, but that worked...
0
David RobitailleAnalyst ProgrammerCommented:
so, any new on that?
0
vu3lmgAuthor Commented:
Still not working.
Did you try to Insert or Update any records ?
0
David RobitailleAnalyst ProgrammerCommented:
yup, but using my how data. i cutted it from the code i sent you but The important thing is to check if the FormView1_ItemUpdated is lunched.
So did it?
0
vu3lmgAuthor Commented:
Yes it launches ItemUpdated but gives error that GROUP_DESCRIPTION field is NULL, though it has some value.
0
David RobitailleAnalyst ProgrammerCommented:
ok, it`s because it s not binding correctly.
you got to mannualy bind it in the FormView1_ItemUpdating event using :
e.NewValues("GROUP_DESCRIPTION") =  the value form the control.
same thing for the insert, i i think it`s "e.Values"
I just thought of that, but it may be because you use the ItemTemplate has the source instead of the EditItemTemplate?
0
vu3lmgAuthor Commented:
I giveup.
I checked on the http://forums.asp.net/t/1350307.aspx and they recommend not to use ItemTemplate for Edit or Insert.
Thanks for your help.
0
David RobitailleAnalyst ProgrammerCommented:
well it`s your choice,
in my case, I dont use a ItemTemplate, but i use the same template for edit and insert and that work well.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.