Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

asp.net placeholder

Posted on 2010-11-24
10
Medium Priority
?
1,128 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 2000 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

618 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