?
Solved

formview ItemTemplate formating

Posted on 2008-11-13
32
Medium Priority
?
1,323 Views
Last Modified: 2012-05-05
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

0
Comment
Question by:vu3lmg
  • 20
  • 12
32 Comments
 
LVL 18

Accepted Solution

by:
David Robitaille earned 1500 total points
ID: 22952212
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
 
LVL 1

Author Comment

by:vu3lmg
ID: 22955061
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
 
LVL 18

Assisted Solution

by:David Robitaille
David Robitaille earned 1500 total points
ID: 22955169
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 18

Expert Comment

by:David Robitaille
ID: 22955252
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
 
LVL 18

Expert Comment

by:David Robitaille
ID: 22955261
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
 
LVL 1

Author Comment

by:vu3lmg
ID: 22955506
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
 
LVL 18

Expert Comment

by:David Robitaille
ID: 22956963
  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
 
LVL 18

Expert Comment

by:David Robitaille
ID: 22959158
if that still dont work, i need to see the new <asp:FormView code
0
 
LVL 1

Author Comment

by:vu3lmg
ID: 22961000
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
 
LVL 18

Expert Comment

by:David Robitaille
ID: 22961087
what did you get the NullReferenceException? witch line???
0
 
LVL 1

Author Comment

by:vu3lmg
ID: 22961357
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
 
LVL 18

Expert Comment

by:David Robitaille
ID: 22961885

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
 
LVL 18

Expert Comment

by:David Robitaille
ID: 22961892
oops i missplaced a { , but i<m sure you understant what i meen...
0
 
LVL 1

Author Comment

by:vu3lmg
ID: 22962260
davrob60
I tried what you suggested above.  Still same, not inserting the record.
It is not raising the _ItemCommand or _ItemInserting events.
0
 
LVL 18

Expert Comment

by:David Robitaille
ID: 22962297
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
 
LVL 1

Author Comment

by:vu3lmg
ID: 22962416
Same Problem.
0
 
LVL 18

Expert Comment

by:David Robitaille
ID: 22962512
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
 
LVL 1

Author Comment

by:vu3lmg
ID: 22962756
When you say "my buttons are visible ...",  Did you create an actual project and try running my code ?
If yes, Is it working ?
0
 
LVL 18

Expert Comment

by:David Robitaille
ID: 22962792
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
 
LVL 1

Author Comment

by:vu3lmg
ID: 22963361
Ok let me know the result when you try out.
0
 
LVL 18

Expert Comment

by:David Robitaille
ID: 22977686
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
 
LVL 1

Author Comment

by:vu3lmg
ID: 22986528
Still same problem.
Can you plese post your code here.
0
 
LVL 18

Expert Comment

by:David Robitaille
ID: 22986619
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
 
LVL 18

Expert Comment

by:David Robitaille
ID: 22986623
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
 
LVL 18

Expert Comment

by:David Robitaille
ID: 22986654
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
 
LVL 18

Expert Comment

by:David Robitaille
ID: 22994541
so, any new on that?
0
 
LVL 1

Author Comment

by:vu3lmg
ID: 22995418
Still not working.
Did you try to Insert or Update any records ?
0
 
LVL 18

Expert Comment

by:David Robitaille
ID: 22995511
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
 
LVL 1

Author Comment

by:vu3lmg
ID: 22995702
Yes it launches ItemUpdated but gives error that GROUP_DESCRIPTION field is NULL, though it has some value.
0
 
LVL 18

Expert Comment

by:David Robitaille
ID: 22996023
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
 
LVL 1

Author Comment

by:vu3lmg
ID: 22998880
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
 
LVL 18

Expert Comment

by:David Robitaille
ID: 22998935
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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month15 days, 22 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question