views:

1994

answers:

3

Hi guys,

I have a TabContainer inside a data bound FormView (to present the information by category ex: Client Bio data, health history, financial details...). The Update and Insert of the formView doesn't work (posting NULL values to the database) - I guess the FormView cannot find the TextBoxes inside the tab container's tab panels.

Some of the forums say that it's because of the TabContainer's implementation (by design) of "INamingContainer", and a hack is to take control of the TabContainer's source code (ajax ctl toolkit's source code) and remove the "INamingContainer" interface from it... Too complicated to my taste .. I'm kinda lost.

Well is there a straight forward and better way to fix this? I'm dazzled to see that the toolkit has failed to implement this basic functionnality as for most developper ordering info (tab control) with formview is a common need.

Thanks in advance, Jeewai

+1  A: 

Answering my own thread... I got some great inside from the asp.net forum and decided to post the solution here: Reproducing the explanation that helped me out:

Hope that will clear out some questions to other users who may encounter the same issue.

Best, JY

Blockquote Hi JY,

The short answer is that when a Bind statement is compiled, there are some limitations on extracting values for an insert/update. If the controls within the FormView are then within another Naming Container (TabContainer and TabPanel are both naming containers), then the compiler can't resolve how to extract the value from the TextBox. I have a more detailed discussion of this on my blog at http://www.aarongoldenthal.com/post/2009/03/15/ASPNET-Databinding-Bind()-Method-Dissected.aspx.

To get around this, you'll need to extract the values manually, something like:

protected void FormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e) { // Get references to the controls TextBox LastNameTextBox= FormView1.FindControl("TabContainer1").FindControl("TabPanel1").FindControl("LastNameTextBox") as TextBox;

// Set update parameters in datasource
ObjectDataSource1.UpdateParameters["LastName"].DefaultValue = LastNameTextBox.Text;

}

Since FindControl only searches the current naming container, you'll need to dig through each naming container (FormView, TabContainer, and TabPanel) to get to the TextBox.

Hope that helps.

Aaron

Blockquote

A: 

Hi, I had the same Problem and found your solution, but it doesen't seem to work. Can you see what I'm doing wrong?

<%@ Page Title="" Language="C#" MasterPageFile="~/layout/MasterPageEmpty.master" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="DataLab_Default2" %>

<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="cc1" %>

<asp:Content ID="Content6" ContentPlaceHolderID="MainContent" Runat="Server">
    <br />
    <br />
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <br />
            <br />
            <cc1:TabContainer ID="TabContainer1" runat="server" ActiveTabIndex="1" 
                Height="393px" Width="640px">
                <cc1:TabPanel ID="TabPanel1" runat="server" HeaderText="TabPanel1">
                    <ContentTemplate>
                        <br />
                        <br />
                        Tab 1<br />
                        <br />
                        <br />
                    </ContentTemplate>
                </cc1:TabPanel>
                <cc1:TabPanel ID="TabPanel2" runat="server" HeaderText="TabPanel2">
                    <ContentTemplate>
                        <br />
                        <br />
                        Tab 2<br />
                        <br />
                        <asp:FormView ID="FormView1" runat="server" DataKeyNames="PK_OrderID" 
                            DataSourceID="ObjectDataSource1" Height="153px" Width="358px" 
                            onitemupdating="FormView1_ItemUpdating">
                            <EditItemTemplate>
                                PK_OrderID:
                                <asp:Label ID="PK_OrderIDLabel1" runat="server" 
                                    Text='<%# Eval("PK_OrderID") %>' />
                                <br />
                                OrderName:
                                <asp:TextBox ID="OrderNameTextBox" runat="server" 
                                    Text='<%# Bind("OrderName") %>' />
                                <br />
                                OrderPriority:
                                <asp:TextBox ID="OrderPriorityTextBox" runat="server" 
                                    Text='<%# Bind("OrderPriority") %>' />
                                <br />
                                OrderDate:
                                <asp:TextBox ID="OrderDateTextBox" runat="server" 
                                    Text='<%# Bind("OrderDate") %>' />
                                <br />
                                FK_RequesterID:
                                <asp:TextBox ID="FK_RequesterIDTextBox" runat="server" 
                                    Text='<%# Bind("FK_RequesterID") %>' />
                                <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>
                                PK_OrderID:
                                <asp:TextBox ID="PK_OrderIDTextBox" runat="server" 
                                    Text='<%# Bind("PK_OrderID") %>' />
                                <br />
                                OrderName:
                                <asp:TextBox ID="OrderNameTextBox" runat="server" 
                                    Text='<%# Bind("OrderName") %>' />
                                <br />
                                OrderPriority:
                                <asp:TextBox ID="OrderPriorityTextBox" runat="server" 
                                    Text='<%# Bind("OrderPriority") %>' />
                                <br />
                                OrderDate:
                                <asp:TextBox ID="OrderDateTextBox" runat="server" 
                                    Text='<%# Bind("OrderDate") %>' />
                                <br />
                                FK_RequesterID:
                                <asp:TextBox ID="FK_RequesterIDTextBox" runat="server" 
                                    Text='<%# Bind("FK_RequesterID") %>' />
                                <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>
                                PK_OrderID:
                                <asp:Label ID="PK_OrderIDLabel" runat="server" 
                                    Text='<%# Eval("PK_OrderID") %>' />
                                <br />
                                OrderName:
                                <asp:Label ID="OrderNameLabel" runat="server" Text='<%# Bind("OrderName") %>' />
                                <br />
                                OrderPriority:
                                <asp:Label ID="OrderPriorityLabel" runat="server" 
                                    Text='<%# Bind("OrderPriority") %>' />
                                <br />
                                OrderDate:
                                <asp:Label ID="OrderDateLabel" runat="server" Text='<%# Bind("OrderDate") %>' />
                                <br />
                                FK_RequesterID:
                                <asp:Label ID="FK_RequesterIDLabel" runat="server" 
                                    Text='<%# Bind("FK_RequesterID") %>' />
                                <br />
                                <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" 
                                    CommandName="Edit" Text="Edit" />
                                &nbsp;<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" 
                                    CommandName="Delete" Text="Delete" />
                                &nbsp;<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" 
                                    CommandName="New" Text="New" />
                            </ItemTemplate>
                        </asp:FormView>
                        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
                            DeleteMethod="DeleteOrder" InsertMethod="AddOrder" 
                            OldValuesParameterFormatString="original_{0}" SelectMethod="GetOrders" 
                            TypeName="OrdersBLL" UpdateMethod="UpdateProduct">
                            <DeleteParameters>
                                <asp:Parameter Name="orderID" Type="Int32" />
                            </DeleteParameters>
                            <InsertParameters>
                                <asp:Parameter Name="orderName" Type="String" />
                                <asp:Parameter Name="orderPriority" Type="String" />
                                <asp:Parameter Name="orderDate" Type="DateTime" />
                                <asp:Parameter Name="fk_RequesterID" Type="Int32" />
                            </InsertParameters>
                            <UpdateParameters>
                                <asp:Parameter Name="orderID" Type="Int32" />
                                <asp:Parameter Name="orderName" Type="String" />
                                <asp:Parameter Name="orderPriority" Type="String" />
                                <asp:Parameter Name="orderDate" Type="DateTime" />
                                <asp:Parameter Name="fk_RequesterID" Type="Int32" />
                            </UpdateParameters>
                        </asp:ObjectDataSource>
                    </ContentTemplate>
                </cc1:TabPanel>
                <cc1:TabPanel ID="TabPanel3" runat="server" HeaderText="TabPanel3">
                    <ContentTemplate>
                        <br />
                        Tab 3
                    </ContentTemplate>
                </cc1:TabPanel>
            </cc1:TabContainer>
            <br />
            <br />
            <br />
            <asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>
            <br />
            <br />
        </ContentTemplate>
    </asp:UpdatePanel>
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
</asp:Content>

C# Code Behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class DataLab_Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void FormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e)
    {

        // Get references to the controls TextBoxes

        Label   PK_OrderIDLabel1 = FormView1.FindControl("TabContainer1").FindControl("TabPanel2").FindControl("TextBoxTab1") as Label;
        TextBox OrderNameTextBox = FormView1.FindControl("TabContainer1").FindControl("TabPanel2").FindControl("OrderNameTextBox") as TextBox;
        TextBox OrderPriorityTextBox = FormView1.FindControl("TabContainer1").FindControl("TabPanel2").FindControl("OrderNameTextBox") as TextBox;
        TextBox OrderDateTextBox = FormView1.FindControl("TabContainer1").FindControl("TabPanel2").FindControl("OrderNameTextBox") as TextBox;
        TextBox FK_RequesterIDTextBox = FormView1.FindControl("TabContainer1").FindControl("TabPanel2").FindControl("OrderNameTextBox") as TextBox;

        // Set update parameters in datasource
        ObjectDataSource1.UpdateParameters["orderID"].DefaultValue = PK_OrderIDLabel1.Text;
        ObjectDataSource1.UpdateParameters["orderName"].DefaultValue = OrderNameTextBox.Text;
        ObjectDataSource1.UpdateParameters["orderPriority"].DefaultValue = OrderPriorityTextBox.Text;
        ObjectDataSource1.UpdateParameters["orderDate"].DefaultValue = OrderDateTextBox.Text;
        ObjectDataSource1.UpdateParameters["fk_RequesterID"].DefaultValue = FK_RequesterIDTextBox.Text;

    }

}
A: 

Wow, I thought I was going crazy. I couldn't get this to work for days. Finally tonight, I tore everything down and found that the problem was the tabs.

After figuring it out, I did some research and lots of people have this problem.

One of the goals of .NET was to eliminate some coding. The deeper I get into .NET, the more quirks I am finding, making it ncessary to create workaroundss with odd pieces of code. The proposed solution here sounds like it would work but really shouldn't be necessary.

I thought 2 way binding would take care of this on its own.

Very disappointed.

Joe