ASP.Net/Page/Load Control

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Add asp:TextBox to page dynamically (VB.net)

<%@ Page Language=VB Debug=true %>
<script runat=server>
Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
End Sub
Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
    Dim I as Integer
    For I = 1 to txtNumber.Text
        Dim MyTextBox = New TextBox
        MyTextBox.ID = "txtDynamic" & I
        MyTextBox.Text = "Control Number: " & I
        frmMyPage.Controls.Add(MyTextBox)
        Dim MyLiteral = New LiteralControl
        MyLiteral.Text = "<BR><BR>"
        frmMyPage.Controls.Add(MyLiteral)
    Next
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Creating a Basic TextBox Control</TITLE>
</HEAD>
<form 
    id="frmMyPage"
    runat="server"
>
Enter the number of TextBox controls you want:<BR>
<asp:TextBox 
    id="txtNumber" 
    Columns="25"
    MaxLength="3"
    runat=server 
/>
<BR><BR>
<asp:button 
    id="butOK"
    text="OK"
    Type="Submit"
    OnClick="SubmitBtn_Click" 
    runat="server"
/>
<BR><BR>
</Form>
</BODY>
</HTML>



Find control in a page (C#)

<%@ Page Language="C#" %>
<script runat="server">
    void Page_PreRender()
    {
        foreach (BaseValidator valControl in Page.Validators)
        {
            WebControl assControl = (WebControl)Page.FindControl(valControl.ControlToValidate);
            if (!valControl.IsValid)
                assControl.BackColor = System.Drawing.Color.Yellow;
            else
                assControl.BackColor = System.Drawing.Color.White;
        }
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Show Validators</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        
    <asp:Label
        id="lblFirstName"
        Text="First Name"
        AssociatedControlID="txtFirstName"
        Runat="server" />
    <br />    
    <asp:TextBox
        id="txtFirstName"
        Runat="server" />
    <asp:RequiredFieldValidator
        id="reqFirstName"
        ControlToValidate="txtFirstName"
        Text="(Required)"
        EnableClientScript="false"
        Runat="server" />   
     
    <br /><br />
        
    <asp:Label
        id="lblLastName"
        Text="Last Name"
        AssociatedControlID="txtLastName"
        Runat="server" />
    <br />    
    <asp:TextBox
        id="txtLastname"
        Runat="server" />
    <asp:RequiredFieldValidator
        id="reqLastName"
        ControlToValidate="txtLastName"
        Text="(Required)" 
        EnableClientScript="false"
        Runat="server" />   
     
     <br /><br />
     
     <asp:Button
        id="btnSubmit"
        Text="Submit"
        Runat="server" />   
    </div>
    </form>
</body>
</html>



LoadControl method example (VB.net)

<%@ Page Language="vb" %>
<html>
   <head>
      <title>LoadControl method example</title>
      <script runat="server">
         Sub Page_Load()
            Dim Hello As UserControl = LoadControl("BulletList.ascx")
            Page.Controls.Add(Hello)
         End Sub
      </script>
   </head>
<body>
   <asp:label id="Message" runat="server"/>
</body>
</html>



Pass parameter to page control (C#)

/*
ASP.NET 2.0 Unleashed (Unleashed) (Hardcover)
by Stephen Walther 
# Publisher: Sams; Bk&CD-Rom edition (June 6, 2006)
# Language: English
# ISBN: 0672328232
*/      


<%@ Page Language="C#" %>
<%@ Register TagPrefix="user" TagName="PropertyRandomImage" Src="PropertyRandomImage.ascx" %>
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        PropertyRandomImage1.ImageFolderPath = ".";
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Show Programmatic</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    <user:PropertyRandomImage
        ID="PropertyRandomImage1"
        Runat="server" />
    
    </div>
    </form>
</body>
</html>

<%--
<%@ Control Language="C#" ClassName="PropertyRandomImage" %>
<%@ Import Namespace="System.IO" %>
<script runat="server">
    private string _imageFolderPath = "~/Images"; 
    public string ImageFolderPath
    {
        get { return _imageFolderPath; }
        set { _imageFolderPath = value; }
    }
    void Page_Load()
    {
        string imageToDisplay = GetRandomImage();
        imgRandom.ImageUrl = Path.rubine(_imageFolderPath, imageToDisplay);
        lblRandom.Text = imageToDisplay;
    }
    private string GetRandomImage()
    {
        Random rnd = new Random();
        string[] images = Directory.GetFiles(MapPath("."), "*.jpg");
        string imageToDisplay = images[rnd.Next(images.Length)];
        return Path.GetFileName(imageToDisplay);
    }
</script>
<asp:Image
    id="imgRandom"
    Width="300px"
    Runat="server" />
<br />    
<asp:Label  
    id="lblRandom"
    Runat="server" />
--%>



Use propery defined in asp.net control (C#)

/*
ASP.NET 2.0 Unleashed (Unleashed) (Hardcover)
by Stephen Walther 
# Publisher: Sams; Bk&CD-Rom edition (June 6, 2006)
# Language: English
# ISBN: 0672328232
*/      

<%@ Page Language="C#" %>
<%@ Register TagPrefix="user" TagName="AddressForm" Src="AddressForm.ascx" %>
<script runat="server">
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        // Show Billing Address Form Results
        ltlResults.Text = "<br />Billing Street: " + AddressForm1.Street;
        ltlResults.Text += "<br />Billing City: " + AddressForm1.City;
        ltlResults.Text += "<br />Billing State: " + AddressForm1.State;
        ltlResults.Text += "<br />Billing Postal Code: " + AddressForm1.PostalCode;
        ltlResults.Text += "<br /><br />";
        
        // Show Shipping Address Form Results
        ltlResults.Text += "<br />Shipping Street: " + AddressForm2.Street;
        ltlResults.Text += "<br />Shipping City: " + AddressForm2.City;
        ltlResults.Text += "<br />Shipping State: " + AddressForm2.State;
        ltlResults.Text += "<br />Shipping Postal Code: " + AddressForm2.PostalCode;
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <style type="text/css">
        html
        {
            background-color:silver;
            font:14px Georgia,Serif;
        }
        .content
        {
            background-color:white;
            width:600px;
            margin:auto;
            padding:20px;
        }
        .addressLabel
        {
            float:left;
            width:100px;
            padding:5px;
            text-align:right;
        }
        .addressField
        {
            float:left;
            padding:5px;
        }
        .clear
        {
            clear:both;
        }
    </style>
    <title>Checkout</title>
</head>
<body>
    <form id="form1" runat="server">
    <div class="content">
    
    <user:AddressForm
        id="AddressForm1"
        Title="Billing Address"
        Runat="server" />
    <br />
    <user:AddressForm
        id="AddressForm2"
        Title="Shipping Address"
        Runat="server" />
    <br />
    
    <asp:Button
        ID="btnSubmit"
        Text="Submit Form"
        OnClick="btnSubmit_Click" 
        Runat="server" />
    <hr />
    <asp:Literal
        id="ltlResults"
        Runat="server" />
    
    </div>
    </form>
</body>
</html>

<%--
<%@ Control Language="C#" ClassName="AddressForm" %>
<script runat="server">
    public string Title
    {
        get { return ltlTitle.Text; }
        set { ltlTitle.Text = value; }
    }
    public string Street
    {
        get { return txtStreet.Text; }
        set { txtStreet.Text = value; }
    }
    public string City
    {
        get { return txtCity.Text; }
        set { txtCity.Text = value; }
    }
    public string State
    {
        get { return txtState.Text; }
        set { txtState.Text = value; }
    }
    public string PostalCode
    {
        get { return txtPostalCode.Text; }
        set { txtPostalCode.Text = value; }
    }
    
</script>
<fieldset>
<legend>
    <asp:Literal 
        ID="ltlTitle"
        Text="Address Form" 
        runat="server" />
</legend>
<div class="addressLabel">
<asp:Label  
    ID="lblStreet"
    Text="Street:"
    AssociatedControlID="txtStreet"
    Runat="server" />
</div>
<div class="addressField">
<asp:TextBox
    ID="txtStreet"
    Runat="server" />
<asp:RequiredFieldValidator
    ID="reqStreet"
    Text="(required)"
    ControlToValidate="txtStreet"
    Runat="server" />    
</div>
<br class="clear" />
<div class="addressLabel">
<asp:Label  
    ID="lblCity"
    Text="City:"
    AssociatedControlID="txtCity"
    Runat="server" />
</div>
<div class="addressField">
<asp:TextBox
    ID="txtCity"
    Runat="server" />
<asp:RequiredFieldValidator
    ID="reqCity"
    Text="(required)"
    ControlToValidate="txtCity"
    Runat="server" />    
</div>
<br class="clear" />
<div class="addressLabel">
<asp:Label  
    ID="lblState"
    Text="State:"
    AssociatedControlID="txtState"
    Runat="server" />
</div>
<div class="addressField">
<asp:TextBox
    ID="txtState"
    Runat="server" />
<asp:RequiredFieldValidator
    ID="reqState"
    Text="(required)"
    ControlToValidate="txtState"
    Runat="server" />    
</div>
<br class="clear" />
<div class="addressLabel">
<asp:Label  
    ID="lblPostalCode"
    Text="Postal Code:"
    AssociatedControlID="txtPostalCode"
    Runat="server" />
</div>
<div class="addressField">
<asp:TextBox
    ID="txtPostalCode"
    Runat="server" />
<asp:RequiredFieldValidator
    ID="RequiredFieldValidator1"
    Text="(required)"
    ControlToValidate="txtPostalCode"
    Runat="server" />    
</div>
<br class="clear" />
</fieldset>        
--%>