Skip to main content

How to use PlaceHolder in asp.net

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 to us how can we add server controls dynamically in a web page using PlaceHolder web server control.

In the below example code, we put a PlaceHolder server control by declarative syntax. We also create a Button control with a Click event. When someone clicks the button, we create an Image control instance programmatically. After populating the Image server control we add it to the PlaceHolder control dynamically using ControlCollection class’s Add() method as the Controls Add(). The Add() method adds the specified control object to the collection.

Here controls collection is PlaceHolder child controls collection. The Add() method requires an argument. This argument type is System.Web.UI.Control. The new control is added to the end of an ordinal index array. To add a new control to a specific index position we can use AddAt() method. Finally, the web page displays the image that is dynamically added to the page using PlaceHolder server control.
PlaceHolder.aspx

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

<!DOCTYPE html>

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

Popular posts from this blog

Restricting Jetpack Compose TextField to Numeric Input Only

Jetpack Compose has revolutionized Android development with its declarative approach, enabling developers to build modern, responsive UIs more efficiently. Among the many components provided by Compose, TextField is a critical building block for user input. However, ensuring that a TextField accepts only numeric input can pose challenges, especially when considering edge cases like empty fields, invalid characters, or localization nuances. In this blog post, we'll explore how to restrict a Jetpack Compose TextField to numeric input only, discussing both basic and advanced implementations. Why Restricting Input Matters Restricting user input to numeric values is a common requirement in apps dealing with forms, payment entries, age verifications, or any data where only numbers are valid. Properly validating input at the UI level enhances user experience, reduces backend validation overhead, and minimizes errors during data processing. Compose provides the flexibility to implement ...

jetpack compose - TextField remove underline

Compose TextField Remove Underline The TextField is the text input widget of android jetpack compose library. TextField is an equivalent widget of the android view system’s EditText widget. TextField is used to enter and modify text. The following jetpack compose tutorial will demonstrate to us how we can remove (actually hide) the underline from a TextField widget in an android application. We have to apply a simple trick to remove (hide) the underline from the TextField. The TextField constructor’s ‘colors’ argument allows us to set or change colors for TextField’s various components such as text color, cursor color, label color, error color, background color, focused and unfocused indicator color, etc. Jetpack developers can pass a TextFieldDefaults.textFieldColors() function with arguments value for the TextField ‘colors’ argument. There are many arguments for this ‘TextFieldDefaults.textFieldColors()’function such as textColor, disabledTextColor, backgroundColor, cursorC...

jetpack compose - Image clickable

Compose Image Clickable The Image widget allows android developers to display an image object to the app user interface using the jetpack compose library. Android app developers can show image objects to the Image widget from various sources such as painter resources, vector resources, bitmap, etc. Image is a very essential component of the jetpack compose library. Android app developers can change many properties of an Image widget by its modifiers such as size, shape, etc. We also can specify the Image object scaling algorithm, content description, etc. But how can we set a click event to an Image widget in a jetpack compose application? There is no built-in property/parameter/argument to set up an onClick event directly to the Image widget. This android application development tutorial will demonstrate to us how we can add a click event to the Image widget and make it clickable. Click event of a widget allow app users to execute a task such as showing a toast message by cli...