Solved

asp.net placeholder

Posted on 2010-11-24
10
1,099 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
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 

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
 

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need help with building query string 2 19
jquery datatables, asp.net MVC 9 41
Call Controller Action Method from ASPX 2 14
C# LINQ ForEach() question 6 28
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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

770 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