Solved

asp.net placeholder

Posted on 2010-11-24
10
1,094 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
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, f…

910 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

23 Experts available now in Live!

Get 1:1 Help Now