Solved

asp.net placeholder

Posted on 2010-11-24
10
1,078 Views
Last Modified: 2012-05-10
I have attached codes and it is working 100% as I want.
Now if I want to put all of the codes in a function call like

Function PlaceHolderName1()
Return Controls(can be anything input box, checkbox, or...)
End Function

Frontend (Will have PlaceHolder control ready name id="PlaceHolderFrontEnd"
Call  PlaceHolderName1()

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data;

using System.Data.SqlClient;

using System.Web.Configuration;



public partial class Default2 : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        SqlConnection DBSuretyCon = new SqlConnection(WebConfigurationManager.ConnectionStrings["SuretyDb"].ToString());

        SqlCommand Command = new SqlCommand("Select_ClassCodeAdditionalUI", DBSuretyCon);

        Command.CommandType = CommandType.StoredProcedure;

        Command.Parameters.Add("@ClassCodeId", SqlDbType.Int).Value = 22;

        SqlDataAdapter da = new SqlDataAdapter(Command);

        DataTable dtPlaceHolders = new DataTable();

        da.Fill(dtPlaceHolders);

        //Find the master placeholder in the page

        //PlaceHolder phMain = (PlaceHolder)Page.FindControl("PlaceHolder1");

        //Iterate over the database results to instantiate and load the user controls into the main placeholder

        foreach (DataRow row in dtPlaceHolders.Rows)

        {

            Control dbPlaceHolder = Page.LoadControl("~/UI/" + row[0].ToString());

            PlaceHolder1.Controls.Add(dbPlaceHolder);

        }

    }   

}

Open in new window

0
Comment
Question by:Webboy2008
  • 5
  • 4
10 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34208514
Once again I seem to be missing what the actual question is?
0
 

Author Comment

by:Webboy2008
ID: 34208562
carl tawn: I don't get it.
I just need the code in function so i can have more page to call this function.

SqlConnection DBSuretyCon = new SqlConnection(WebConfigurationManager.ConnectionStrings["SuretyDb"].ToString());
        SqlCommand Command = new SqlCommand("Select_ClassCodeAdditionalUI", DBSuretyCon);
        Command.CommandType = CommandType.StoredProcedure;
        Command.Parameters.Add("@ClassCodeId", SqlDbType.Int).Value = 22;
        SqlDataAdapter da = new SqlDataAdapter(Command);
        DataTable dtPlaceHolders = new DataTable();
        da.Fill(dtPlaceHolders);
        //Find the master placeholder in the page
        //PlaceHolder phMain = (PlaceHolder)Page.FindControl("PlaceHolder1");
        //Iterate over the database results to instantiate and load the user controls into the main placeholder
        foreach (DataRow row in dtPlaceHolders.Rows)
        {
            Control dbPlaceHolder = Page.LoadControl("~/UI/" + row[0].ToString());
            PlaceHolder1.Controls.Add(dbPlaceHolder);
        }
0
 
LVL 22

Expert Comment

by:Snarf0001
ID: 34208684
I'm assuming the part you're struggling with is getting the proper placeholder reference?

The easiest and most foolproof way of doing it would be to simply pass a reference to the target placeholder in the function call.
From code below, you'd call from your referenced page above:

RenderControl(PlaceHolder1);
public void RenderControl(PlaceHolder placeHolder)

        {

            SqlConnection DBSuretyCon = new SqlConnection(WebConfigurationManager.ConnectionStrings["SuretyDb"].ToString());

            SqlCommand Command = new SqlCommand("Select_ClassCodeAdditionalUI", DBSuretyCon);

            Command.CommandType = CommandType.StoredProcedure;

            Command.Parameters.Add("@ClassCodeId", SqlDbType.Int).Value = 22;

            SqlDataAdapter da = new SqlDataAdapter(Command);

            DataTable dtPlaceHolders = new DataTable();

            da.Fill(dtPlaceHolders);



            foreach (DataRow row in dtPlaceHolders.Rows)

            {

                Control dbPlaceHolder = placeHolder.Page.LoadControl("~/UI/" + row[0].ToString());

                placeHolder.Controls.Add(dbPlaceHolder);

            }

        }

Open in new window

0
 

Author Comment

by:Webboy2008
ID: 34208726
Compiler Error Message: CS1502: The best overloaded method match for 'System.Web.UI.Control.RenderControl(System.Web.UI.HtmlTextWriter)' has some invalid arguments

frontend call aspx/c#:

<asp:placeholder id="PhdAdditionalInformation" />

 ReturnAscxControl(RenderControl("PhdAdditionalInformation"));
 ReturnAscxControl(RenderControl(PhdAdditionalInformation));

Both calls in the frontpage are failed.

           
0
 
LVL 22

Expert Comment

by:Snarf0001
ID: 34208761
1) You do have runat="server" on <asp:placeholder id="PhdAdditionalInformation" />?
2) Are you calling from the pages code-behind page?
3) What is ReturnAscxControl?  The function i provided doesn't return anything, so wondering what you're expecting.

Pls post more complete code.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:Webboy2008
ID: 34208775
asp frontend called:

ReturnAscxControl(SessionClassCodeId, RenderControl("PhdAdditionalInformation"));
           
public void ReturnAscxControl(int ClassCodeId, PlaceHolder PlaceHolderName)

    {

        SqlConnection DBSuretyCon = new SqlConnection(WebConfigurationManager.ConnectionStrings["SuretyDb"].ToString());

        SqlCommand Command = new SqlCommand("Select_ClassCodeAdditionalUI", DBSuretyCon);

        Command.CommandType = CommandType.StoredProcedure;

        Command.Parameters.Add("@ClassCodeId", SqlDbType.Int).Value = ClassCodeId;

        SqlDataAdapter da = new SqlDataAdapter(Command);

        DataTable dtPlaceHolders = new DataTable();

        da.Fill(dtPlaceHolders);

        //Find the master placeholder in the page

        //PlaceHolder phMain = (PlaceHolder)Page.FindControl("PlaceHolder1");

        //Iterate over the database results to instantiate and load the user controls into the main placeholder

        foreach (DataRow row in dtPlaceHolders.Rows)

        {

            Control dbPlaceHolder = Page.LoadControl("~/UI/" + row[0].ToString());

            PlaceHolderName.Controls.Add(dbPlaceHolder);

        }

    }

Open in new window

0
 
LVL 22

Expert Comment

by:Snarf0001
ID: 34208809
Okay, so the attached code for ReturnAscxControl appears to have exactly the same code as what i posted in RenderControl.
So what then do you have in RenderControl?

Also, stupid thing on my part, RenderControl is already a method on the page.
If ReturnAscxControl is supposed to replace the function I put in, then just call it with:

ReturnAscxControl(SessionClassCodeId, PhdAdditionalInformation);


Also, if you are going to put this code outside in a different class, it's important to change:
  Page.LoadControl("~/UI/" + row[0].ToString());
to
  PlaceHolderName.Page.LoadControl("~/UI/" + row[0].ToString());

as I had outlined, in a separate class file you'll have no visibility of the Page object.
0
 

Author Comment

by:Webboy2008
ID: 34208872
I want to put the function into App_Code folder.
What shoud I change?
0
 
LVL 22

Accepted Solution

by:
Snarf0001 earned 500 total points
ID: 34208891
Just the part where you reference the Page.
using System;

using System.Web.UI.WebControls;

using System.Data.SqlClient;

using System.Web.Configuration;

using System.Data;

using System.Web.UI;



namespace SampleNameSpace

{

    public class SampleClass

    {

        public static void ReturnAscxControl(int ClassCodeId, PlaceHolder PlaceHolderName)

        {

            SqlConnection DBSuretyCon = new SqlConnection(WebConfigurationManager.ConnectionStrings["SuretyDb"].ToString());

            SqlCommand Command = new SqlCommand("Select_ClassCodeAdditionalUI", DBSuretyCon);

            Command.CommandType = CommandType.StoredProcedure;

            Command.Parameters.Add("@ClassCodeId", SqlDbType.Int).Value = ClassCodeId;

            SqlDataAdapter da = new SqlDataAdapter(Command);

            DataTable dtPlaceHolders = new DataTable();

            da.Fill(dtPlaceHolders);



            foreach (DataRow row in dtPlaceHolders.Rows)

            {

                Control dbPlaceHolder = PlaceHolderName.Page.LoadControl("~/UI/" + row[0].ToString());

                PlaceHolderName.Controls.Add(dbPlaceHolder);

            }

        }

    }

}

Open in new window

0
 

Author Comment

by:Webboy2008
ID: 34208918
Thanks, 500pts on the way. I have another question posted if you interest to help.

http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_26637527.html
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now