Solved

asp.net placeholder

Posted on 2010-11-24
10
1,119 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 23

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 23

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
 

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 23

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 23

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

695 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