Home All Groups Group Topic Archive Search About

FormView - FindControl only works for default view

Author
20 Mar 2006 6:27 PM
David Thielen
Hi;

I have the following two lines in my code-behind:
object o = DataSourceItemView.FindControl("DataTitle"); // in <ItemTemplate>
o = DataSourceItemView.FindControl("NewButton"); // in <InsertItemTemplate>

When I run the first returns the control and the second returns null.

If I then add DefaultMode="Insert" to my FormView, then the first returns a
null and the second returns a control.

It looks like FindControl only works for the default template - is this
true? And if so, I assume this means FormView can only be used for one
template?

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Author
21 Mar 2006 7:59 AM
Steven Cheng[MSFT]
Hi Dave,

If you want to access the controls defined in a certain Template, the
FormView¡¯s currentMode should be of that Template¡¯s mode. In other words,
at a certain time, we can only access controls in a single template. Also,
from the code you provided, I¡¯m still not quite sure about your detailed
code logic, would you provide some further complete codebehind period?

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
Author
21 Mar 2006 1:57 PM
David Thielen
the code:
using System;
using System.Web.UI.WebControls;
using net.windward.env.ado;

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

        object o = DataSourceItemView.FindControl("DataTitle");
        o = DataSourceItemView.Row.FindControl("DataTitle");
        o = DataSourceItemView.FindControl("NewButton");
        o = DataSourceItemView.Row.FindControl("NewButton");
        //        if (!IsPostBack)
//        {
            if (DataSourceItemView.CurrentMode == FormViewMode.Insert)
            {
                MultiView dsView = (MultiView)DataSourceItemView.FindControl("dsView");
                RadioButtonList radioPermissions =
(RadioButtonList)DataSourceItemView.Row.FindControl("radioPermissions");
                DropDownList lstVendor =
(DropDownList)DataSourceItemView.Row.FindControl("lstVendor");

                radioPermissions.SelectedIndex = 0;
                dsView.ActiveViewIndex = 0;

                // get the SQL drivers - some fail on getFactory and the exception is
the only way to know - yuck
                lstVendor.Items.Add(new ListItem("--- Select Vendor ---", ""));
                IWrVendor[] vendors = WrFactory.Vendors;
                foreach (IWrVendor vnd in vendors)
                    lstVendor.Items.Add(new ListItem(vnd.Name, vnd.Value));
            }
//        }
    }

    protected void DataProvider_SelectedIndexChanged(object sender, EventArgs e)
    {
        MultiView dsView = (MultiView)DataSourceItemView.FindControl("dsView");
        dsView.ActiveViewIndex = RadioDataProviderValue;
    }

    private int RadioDataProviderValue
    {
        get
        {
            RadioButtonList DataProvider =
(RadioButtonList)DataSourceItemView.FindControl("DataProvider");
            return Convert.ToInt32(DataProvider.SelectedItem.Value);
        }
    }
    protected void DataSourceItemView_ModeChanged(object sender, EventArgs e)
    {
        if (DataSourceItemView.CurrentMode == FormViewMode.Insert)
        {
            object o = DataSourceItemView.FindControl("DataTitle");
            o = DataSourceItemView.Row.FindControl("DataTitle");
            o = DataSourceItemView.FindControl("NewButton");
            o = DataSourceItemView.Row.FindControl("NewButton");
            MultiView dsView = (MultiView)DataSourceItemView.FindControl("dsView");
            RadioButtonList radioPermissions =
(RadioButtonList)DataSourceItemView.FindControl("radioPermissions");
            DropDownList lstVendor =
(DropDownList)DataSourceItemView.FindControl("lstVendor");
        }
    }
}
Author
22 Mar 2006 2:57 AM
Steven Cheng[MSFT]
Thanks for your response and the detailed code snippet and page template,

From the complete code behind period, I think the problem is on the
event(of the FormView) you're using, the event "ModeChanged" you're using
is only a notification event on the changing of the detailsview's
curerntMode, and at that time, the actual underlying template and control
collection are still under construncting and the controls may not have been
created yet.  To access the child controls in certain template, I suggest
you use the "ItemCreated" event, at that time, the certain controls in the
current active template are guaranteed to be created. e.g:

======================
protected void DetailsView1_ItemCreated(object sender, EventArgs e)
    {
        if (DetailsView1.CurrentMode == DetailsViewMode.Insert)
        {
            Response.Write("<br/>" +
DetailsView1.Rows[0].Cells[1].Controls[0]);

        }
    }

==========================

You can try using the FindControl to locate certain named control there.

Hope this helps.

regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
Author
21 Mar 2006 1:57 PM
David Thielen
the page:
<%@ Page Language="C#" MasterPageFile="~/Windward.master"
AutoEventWireup="true" CodeFile="Datasource.aspx.cs" Inherits="Datasource"
Title="View DataSource" %>
<asp:Content ID="Content1" ContentPlaceHolderID="PageContent" Runat="Server">

    <asp:ObjectDataSource ID="DatasourceItemSource" runat="server"
TypeName="net.windward.portal.net.data.DataSourceManager"
        DataObjectTypeName="net.windward.portal.net.data.DataSourceItem"
SelectMethod="FindById"
        DeleteMethod="Delete" UpdateMethod="Update">
        <SelectParameters>
            <asp:QueryStringParameter Name="ID" QueryStringField="ID" />
        </SelectParameters>
    </asp:ObjectDataSource>

    <asp:FormView ID="DataSourceItemView" DefaultMode="Insert" runat="server"
DataKeyNames="ID" DataSourceID="DatasourceItemSource"
OnModeChanged="DataSourceItemView_ModeChanged">
        <EmptyDataTemplate>
        <h3><asp:Literal ID="Literal1" runat="server" Text='<%$
Resources:Windward, NoData %>' /></h3>
        </EmptyDataTemplate>
        <ItemTemplate>
            <table>
                <tr>
                    <td>
                        Title:</td>
                    <td><%# Eval("Title") %>
                    </td>
                </tr>
                <tr>
                    <td >Description:
                    </td>
                    <td><%# Eval("Description") %>
                    </td>
                </tr>
                <tr><td colspan='2'>
                <asp:LinkButton ID="NewButton"
                                        Text="New"
                                        CommandName="New"
                                        RunAt="server"/>

                </td></tr>
            </table>
        </ItemTemplate>

        <InsertItemTemplate>
            <table>
            <tr>
                <td>
                    Title:</td>
                <td>
                    <asp:TextBox ID="DataTitle" runat="server" Columns="60"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="errTitle" runat="server"
ControlToValidate="DataTitle"
                ErrorMessage="Please enter a title for this
datasource"></asp:RequiredFieldValidator></td>
            </tr>
            <tr>
                <td valign="top">
                    Description:</td>
                <td valign="top">
                    <asp:TextBox ID="Description" runat="server" Columns="60" Rows="4"
Height="72px"></asp:TextBox></td>
            </tr>
            <tr>
                <td valign="top">
                    <asp:RadioButtonList ID="DataProvider" runat="server"
AutoPostBack="True"
OnSelectedIndexChanged="DataProvider_SelectedIndexChanged">
                        <asp:ListItem Value="0">Sql Database</asp:ListItem>
                        <asp:ListItem Value="1">XML Datasource</asp:ListItem>
                    </asp:RadioButtonList></td>
                <td valign="top">
                    <asp:MultiView ID="dsView" runat="server">
                        <asp:View ID="View1" runat="server">
                        <table width="100%">
                            <tr>
                                <td>
                                    Vendor:</td>
                                <td >
                                    <asp:DropDownList ID="lstVendor" runat="server" AutoPostBack="True" >
                                    </asp:DropDownList>
                                    <asp:CustomValidator ID="errVendor" runat="server"
ControlToValidate="lstVendor"
                                        ErrorMessage="CustomValidator">Please select a database
vendor</asp:CustomValidator></td>
                            </tr>
                            <tr>
                                <td>
                                    Server:</td>
                                <td>
                                    <asp:DropDownList ID="lstServer" runat="server" AutoPostBack="True" >
                                    </asp:DropDownList>
                                    <asp:Label ID="statusServer" runat="server" Text="{not supported}"
Visible="False"></asp:Label>
                                    <asp:CustomValidator ID="errServer" runat="server"
ErrorMessage="CustomValidator">Please select a server (or enter the
connection string)</asp:CustomValidator></td>
                            </tr>
                            <tr>
                                <td>
                                    Database:</td>
                                <td>
                                    <asp:DropDownList ID="lstDatabase" runat="server"
AutoPostBack="True">
                                    </asp:DropDownList>
                                    <asp:Label ID="statusDatabase" runat="server" Text="{not
supported}" Visible="False"></asp:Label>
                                    <asp:CustomValidator ID="errDatabase" runat="server"
ErrorMessage="CustomValidator">Please select a database (or enter a
connection string)</asp:CustomValidator></td>
                            </tr>
                            <tr>
                                <td>Username:
                                </td>
                                <td>
                                    <asp:TextBox ID="sqlUsername" runat="server"
Columns="25"></asp:TextBox>
                                    (leave empty to use SSPI)</td>
                            </tr>
                            <tr>
                                <td>Password:
                                </td>
                                <td>
                                    <asp:TextBox ID="sqlPassword" runat="server" Columns="25"
TextMode="Password"></asp:TextBox> <asp:CheckBox
                                        ID="sqlStoreUserPw" runat="server" Text="Store Username &
Password" />&nbsp;
                                    <asp:CustomValidator ID="errSqlStoreUserPw" runat="server"
ErrorMessage="CustomValidator">Must enter username & password to save
them</asp:CustomValidator></td>
                            </tr>
                            <tr>
                                <td>
                                    Connection String:</td>
                                <td>
                                    <asp:TextBox ID="connectionString" runat="server"
Columns="80"></asp:TextBox>&nbsp;
                                    <asp:CustomValidator ID="errConnStr" runat="server"
ErrorMessage="CustomValidator">Please enter a connection
string</asp:CustomValidator></td>
                            </tr>
                        </table>
                            <asp:CustomValidator ID="errSql" runat="server"
ErrorMessage="CustomValidator">Please select a server and database or enter a
connection string</asp:CustomValidator></asp:View>
                        <asp:View ID="View2" runat="server">
                        <table width="100%">
                            <tr>
                                <td>file/url:
                                </td>
                                <td>
                                    <asp:TextBox ID="XmlFilename" runat="server"
Columns="80"></asp:TextBox> 
                                    <asp:RequiredFieldValidator ID="errXmlFile" runat="server"
ErrorMessage="RequiredFieldValidator" ControlToValidate="XmlFilename">Please
enter a filename</asp:RequiredFieldValidator></td>
                            </tr>
                            <tr>
                                <td>Username:
                                </td>
                                <td>
                                    <asp:TextBox ID="xmlUsername" runat="server"
Columns="25"></asp:TextBox>
                                    (leave empty to use your username & password)</td>
                            </tr>
                            <tr>
                                <td>Password:
                                </td>
                                <td>
                                    <asp:TextBox ID="xmlPassword" runat="server" Columns="25"
TextMode="Password"></asp:TextBox> <asp:CheckBox
                                        ID="xmlStoreUserPw" runat="server" Text="Store Username &
Password" />&nbsp;
                                    <asp:CustomValidator ID="errXmlStoreUserPw" runat="server"
ErrorMessage="CustomValidator">Must enter username & password to save
them</asp:CustomValidator></td>
                            </tr>
                        </table>
                        </asp:View>
                    </asp:MultiView></td>
            </tr>
            <tr>
                <td valign="top">
                    Permissions:</td>
                <td valign="top">
                    <asp:RadioButtonList ID="radioPermissions" runat="server" >
                        <asp:ListItem Value="PRIVATE">Private (only me)</asp:ListItem>
                        <asp:ListItem Value="GLOBAL">Global (anyone can run)</asp:ListItem>
                        <asp:ListItem Value="SPECIAL">Special...</asp:ListItem>
                    </asp:RadioButtonList></td>
            </tr>
        </table>
        </InsertItemTemplate>

    </asp:FormView>
</asp:Content>
Author
21 Mar 2006 1:57 PM
David Thielen
Hi;

Ok, posted both in full. The code behind can always get the controls for the
default template (regardless of mode) and never for the non-default template
(regardless of mode).

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com



Show quoteHide quote
"Steven Cheng[MSFT]" wrote:

> Hi Dave,
>
> If you want to access the controls defined in a certain Template, the
> FormView¡¯s currentMode should be of that Template¡¯s mode. In other words,
> at a certain time, we can only access controls in a single template. Also,
> from the code you provided, I¡¯m still not quite sure about your detailed
> code logic, would you provide some further complete codebehind period?
>
> Regards,
>
> Steven Cheng
> Microsoft Online Support
>
> Get Secure! www.microsoft.com/security
> (This posting is provided "AS IS", with no warranties, and confers no
> rights.)
>
>
>