Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 29th, 2005, 05:26 AM
Registered User
 
Join Date: Nov 2005
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default FindControl

Hi,

I created an class PlaceHTML in a ClassLibrary. I call this class in my webform. I want to fill PlaceHolders with HTML with this class, therefore i need to identify the control. I used 'FindControl'. The class is looking for the right control, but doesn't find it. Does anyone know what i am doing wrong?

Quote:
quote:
public void PlaceHTMLContent(string[] strArray, int PlaceHolderID)
{
 string strID = "PlaceHolder" + PlaceHolderID.ToString();
 System.Diagnostics.Trace.WriteLine(strID);

 PlaceHolder objPlaceHolder = new PlaceHolder();
 objPlaceHolder = (PlaceHolder) objPlaceHolder.FindControl(strID);

 if(objPlaceHolder!=null)
 {
  System.Diagnostics.Trace.WriteLine(strID);
 }
 else
 {
  System.Diagnostics.Trace.WriteLine("Control not found");
 }
}
Thx.

 
Old November 29th, 2005, 07:02 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Ankur_Verma Send a message via MSN to Ankur_Verma
Default

PlaceHTML class that you have created- is it derived from Control class and using Control.FindControl or have u written something of your own to do find controls.
Because if you are using Control's FindControl then the way you are creating the control's ID is a bit of more work you are doing than you need to. U don’t need to pass an integer and then synthesize the text ID based on it, you can pass the text ID of the control to look for straightaway. I mean if you are not doing it to achieve some other goal this function could be made simpler.

Make sure that the value returned by FindControl is being cast properly.



Regards
Ankur Verma
 
Old November 29th, 2005, 10:38 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Can you paste your markup so we can see how you are declaring the controls?

-Peter
 
Old November 30th, 2005, 05:09 AM
Registered User
 
Join Date: Nov 2005
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

There are several PlaceHolders in the webform.

In my webapplication i have an jagged array. Each array contains information which i want to place into a PlaceHolder. The Array and a ID are passed to the class. I want to use FindControl to assign properties. Therefore i first need to identify the right PlaceHolder. Is this possible with FindControl?

I am trying another approach. I have created a custom control (PlaceHolder). I would like the custom to retrieve the information from the dataprovider on initialization. Therefore i again need to know the ID of the Placeholder. For example (PlaceHolder1, PlaceHolder2). I tried to use this.ID in the custom control, which doesnt work. Is there a way to retrieve the ID inside the custom control on initialization.

Or does anyone has another solution for my need.



 
Old November 30th, 2005, 05:11 AM
Registered User
 
Join Date: Nov 2005
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The Markup:

Code:
<asp:PlaceHolder id="PlaceHolder1" runat="server"></asp:PlaceHolder>
 
Old November 30th, 2005, 08:06 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Ankur_Verma Send a message via MSN to Ankur_Verma
Default

Again I would say that if your are basing the Placeholder on Control class, or on some class derived from Control class, then both the things i.e. FindControl and ID should be no issues. FindControl should work fine and ID (inherited from Control or any class derived from it) would abe available as a public property that you can both set or get as usuall.
But it think a lot has been said and heard without yeilding much result, so post the relevent parts of placeholder and i may be be able say as to where the things are going wrong.

Regards
Ankur Verma
 
Old November 30th, 2005, 09:03 AM
Registered User
 
Join Date: Nov 2005
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am using a Literal instead of a PlaceHolder now. The Problem stays.

Code:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Data.Odbc;
using System.Configuration;

namespace System.Web.UI.WebControls
{
    /// <summary>
    /// Summary description for WervisPlaceHOlder.
    /// </summary>
    /// 

    public class WervisLiteral : System.Web.UI.WebControls.Literal
    {

        /// <summary>
        /// Required designer variable.
        /// </summary>
        /// 

        string strConnectie = ConfigurationSettings.AppSettings["strConnectie"];
        string strDatabaseType = ConfigurationSettings.AppSettings["strDatabaseType"];

        private System.ComponentModel.Container components = null;

        public WervisLiteral()
        {
            InitializeComponent();
            System.Diagnostics.Trace.WriteLine("--> WervisPlaceHolder");

            System.Diagnostics.Trace.WriteLine("--> " + strConnectie);
            System.Diagnostics.Trace.WriteLine("--> " + strDatabaseType);

            GetContent();
        }

        public void GetContent(){


            string strSQL = "SELECT * FROM Content " +
                "WHERE ContentSpark = '" + this.ID + "' " +
                "ORDER BY ContentPlaceHolder ASC";

            //Database verbinding voor specifieke website
            //Connectionstring uit web.config

            if (strDatabaseType == "1") 
            {            
                System.Diagnostics.Trace.WriteLine("--> MSSQL");
                //MSSQL
                //Maak database verbinding
                SqlConnection conMSSQL  = new SqlConnection(strConnectie);
                conMSSQL.Open();

                //Database commando
                SqlCommand comSQL = new SqlCommand();
                comSQL.Connection = conMSSQL;
                comSQL.CommandText = strSQL;

                //DataReader
                SqlDataReader dataRead;
                dataRead = comSQL.ExecuteReader();

                while (dataRead.Read()) 
                {
                    //dataRead.GetString(i);
                    System.Diagnostics.Trace.WriteLine("--> DataRead");
                }

                dataRead.Close();
                conMSSQL.Close();
            } 
            else 
            {
                System.Diagnostics.Trace.WriteLine("--> MySQL");
                //MySQL
                //Maak database verbinding
                OdbcConnection conMySQL = new OdbcConnection (strConnectie);
                conMySQL.Open();

                //Database commando
                OdbcCommand comMySQL = new OdbcCommand();
                comMySQL.Connection = conMySQL;
                comMySQL.CommandText = strSQL;

                //DataReader
                OdbcDataReader dataRead;
                dataRead = comMySQL.ExecuteReader();

            while (dataRead.Read()) 
                {
                    //dataRead.GetString(i);
                    System.Diagnostics.Trace.WriteLine("-->DataRead: " + dataRead.GetString(3));
                this.Text = dataRead.GetString(3);

                }

                dataRead.Close();    
                conMySQL.Close();
            }


        }


        #region Component Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            components = new System.ComponentModel.Container();
            System.Diagnostics.Trace.WriteLine("--> (this)" + ID);


        }
        #endregion


    }
}
 
Old November 30th, 2005, 09:51 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Ankur_Verma Send a message via MSN to Ankur_Verma
Default

If you are expecting statments in the InitializeComponent to show you the value of ID property then let me tell you that you wont get anything there. U've called InitializeComponent in WervisLiteral's contructor. That means that InitializeComponent will be called as soon as your WervisLiteral' object is created, at which point the properties like ID, etc have not been set. If you see the InitializeComponent of you form you would see that the properties of your controls get set later on.

Try this - try to display the ID property of WervisLiteral objects at a button click once the form has loaded completely, at this point you should be able to see the value of ID property.

Regards
Ankur Verma





Similar Threads
Thread Thread Starter Forum Replies Last Post
FindControl in DataListItem SKhna ASP.NET 2.0 Basics 1 February 29th, 2008 05:15 PM
using FindControl after ChangeMode in a FormView pbb ASP.NET 2.0 Basics 1 March 6th, 2007 10:11 AM
PreviousPage.FindControl returns nothing dschips BOOK: Professional ASP.NET 2.0 and Special Edition; ISBN: 978-0-7645-7610-2; ISBN: 978-0-470-04178-9 3 March 6th, 2007 08:35 AM
FindControl dkr72 Excel VBA 0 January 19th, 2005 09:55 AM
Problem with FindControl RussC Excel VBA 1 June 16th, 2004 11:58 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.