Вы находитесь на странице: 1из 7

PLACE HOLDER AND HIDDEN CONTROL

INTRODUCTION

PlaceHolder Web server control in ASP.NET is used to added the controls at run time.
PlaceHolder control is used as a container in which other controls can be added dynamically.
This control has no HTML output, it is used only to load a control at a specific place on the
page (Whereas Panel control has <div> as HTML output.)

Ideally HiddenField is used to create a hidden field in the form. When it is rendered
on the page, it is implemented through <input type=hidden> HTML tag.

PLACEHOLDER SERVER CONTROL

Placeholder is an asp.net web server control which used to store dynamically added
web server controls on the web page. By using a placeholder control we can dynamically add
Label, textbox, Button, radiobutton, Image and many more web server controls in an asp.net
web page. Placeholder server control act as a container control to store server controls that
are dynamically added to the web page. Placeholder control does not provide any visible
output. We only can see the dynamically added server controls inside a placeholder control as
child controls. We can add, insert and remove server controls programmatically in the
placeholder control.

The following asp.net c# example code demonstrates us how can we add server
controls dynamically in a web page using placeholder web server control. In the bellow
example code we put a placeholder server control by declarative syntax. We also create a
Button control with a Click event. When someone click the button, we create a Image control
instance programmatically. After populating the Image server control we add it to the
placeholder control dynamically using controlcollection.Add() method as Controls.Add().
The Add() method adds the specified control object to the collection. Here controls collection
is placeholder child controls collection. Add() method require an argument. This argument
type is System.Web.UI.Control. New control is added to the end of an ordinal index array. To
add new control to specific index position we can use addat() method. Finally the web page
display the image that is dynamically added to the page using placeholder server control.

1
PlaceHolder.aspx

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"


"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

protected void Button1_Click(object sender, EventArgs e)

Image img = new Image();

img.ImageUrl = @"~/Images/sea.jpg";

img.BorderWidth = 3;

img.BorderColor = System.Drawing.Color.SaddleBrown;

PlaceHolder1.Controls.Add(img);

</script>

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

<title>asp.net PlaceHolder example: how to use</title>

</head>

<body>

<form id="form1" runat="server">

<div>

2
<h2 style="color:Navy">PlaceHolder Example</h2>

<asp:PlaceHolder

ID="PlaceHolder1"

runat="server"

>

</asp:PlaceHolder>

<br />

<asp:Button

ID="Button1"

runat="server"

Text="Add Image Control"

OnClick="Button1_Click"

Font-Bold="true"

ForeColor="SaddleBrown"

/>

</div>

</form>

</body>

</html>

3
HIDDEN CONTROLS

The hidden field is a control that stores a value but does not display that value to the
user. It would be similar to using a label that had a value, but was hidden. The hidden field is
rendered as an HTML element. The hidden field can be used to store data that needs to persist
across multiple post backs. This control can come in handy when other methods of storing
data across post backs are not available, such as session states and cookies. Yes, it is possible
to find a good web host. Sometimes it takes a while to find one you are comfortable with.
After trying several, we went with Server Intellect and have been very happy thus far. They

4
are by far the most professional, customer service friendly and technically knowledgeable
host weve found so far.

Using the HiddenField

There are two different ways that you might want to use the hidden field, from a
javascript function or from the code behind of our page. In this example, we will be
demonstrating both ways. To do this, create a new ASP.NET Empty Web Site and:

Right click the project in your solution explorer.

Select add new item

Select a web form.

Name it Default.aspx.

Click add.

Open Default.aspx up to design mode.

Drag and drop a label onto the web form.

Drag and drop a hiddenfield onto the web form.

Set the Value property of the hiddenfield to Hello Hidden Field!.

To access the hidden field in our code behind, we can simply add the following code
to our Page_Load event method that will set our label to the value of the hidden field:

Label1.Text = HiddenField1.Value;

Next, to access the hidden field from a javascript function, we can add in a button to
the Default.aspx page as follows:

<input type="submit" name="btnDisplay" value="Click Me!"


onclick="displayHiddenValue()" />

5
It is possible to simply add in the following JavaScript function to run on the event
that our button is clicked to display the hidden field value:

JavaScript

function displayHiddenValue()

alert(form1.HiddenField1.value);

Testing

To test this out, load up the web site. Notice that your label contains the value of the
hidden field. Next, click the button and notice the alert popup that displays to us the value of
our hidden field. The hidden field can be used for many different purposes, but is not suitable
to hold sensitive information. This is because the user can view the value of the hidden field
in the page source via your web browser.

CONCLUSION

ASP.NET PlaceHolder Control is not like other standard ASP.NET controls. It has not
user interface and doesn't produce any HTML output on client side. PlaceHolder control is
invisible for website's visitors. Still, it is the one of very important controls on the Visual
Studio toolbox, especially useful if you create dynamic user interface. Only purpose of
PlaceHolder control is to hold other controls, or content like HTML, JavaScript, plain text
etc. Although it is possible to add controls directly to Page object, this way is not used much
because it is hard to manipulate where and how added controls will be shown at run time.
PlaceHolder control, just like its name says, holds place for other controls, without producing
its own visible output.

6
REFERENCES

http://www.devmanuals.com/tutorials/ms/aspdotnet/placeholder.html

http://www.dotnetfunda.com/tutorials/controls/placeholder.aspx

http://www.beansoftware.com/ASP.NET-Tutorials/Using-PlaceHolder-
Control.aspx

http://asp-net-example.blogspot.in/2009/02/aspnet-placeholder-example-how-to-
use.html

http://www.dotnetfunda.com/tutorials/controls/hiddenfield.aspx

Вам также может понравиться