我想创建控件并以程序的方式将它们定位在我想要的位置

本文关键字:定位 位置 我想要 方式 控件 创建 程序 | 更新日期: 2023-09-27 18:26:13

我有下面的FormReport.aspx。我想创建div class="employeebox>内部内容的副本,但在单击addrow_clic时以编程方式创建?如有任何帮助,将不胜感激

FormReport.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true"  CodeBehind="FormReport.aspx.cs" Inherits="Tardy_Absentee_Report.FormReport" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
<style type="text/css">
    #headerform
    {
        height: 56px;
    }
    #Div1
    {
        height: 70px;
    }
    .colum-left
    {
        width: 122px;
    }
    .colum-cenleft2
    {
        width: 344px;
    }
    .employeebox
    {
        height: 37px;
    }
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<fieldset>
<legend>Supervisor information </legend>
<div class="row">
    <div class="column-left">Supervisor Name:<span></span> </div>
    <div class="column-cenleft">
        <asp:TextBox ID="txtsupervisorname" runat="server" Width="103px"></asp:TextBox></div>
    <div class="column-cenright"><span>Department:</span></div>
    <div class="column-right">
        <asp:DropDownList ID="DDownDepartment" runat="server" Height="24px" Width="154px">
        </asp:DropDownList>
    </div>
</div>
 <div class="row">
    <div class="column-left">Supervisor Number:<span></span> </div>
    <div class="column-cenleft">
        <asp:TextBox ID="txtSupervisorNumber" runat="server" Width="103px"></asp:TextBox></div>
    <div class="column-cenright"><span>Date of Incident:</span></div>
    <div class="column-right">
        <asp:TextBox ID="txtDate" runat="server"></asp:TextBox></div>
</div>
</fieldset>
<fieldset>
<legend> Employee Information</legend>     
<div id="EmployeeInfo">
    <div class="column-left2"><span>Employee Number</span></div>
    <div class="column-left2"><span>Employee Name</span></div>
    <div class="colum-left2"><span>Reason Code</span></div>
</div>
<div class="employeebox">
<div class="column-left2"><asp:TextBox ID="TextBox4" runat="server"></asp:TextBox></div>
<div class="column-left2">
    <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox></div>
<div class="column-left2">
    <asp:DropDownList ID="DDownResoncode1" runat="server" Width="153px">
    </asp:DropDownList>
</div>    
</div>
<div class="employeebox">
<div class="column-left2"><asp:TextBox ID="TextBox7" runat="server"></asp:TextBox></div>
<div class="column-left2">
    <asp:TextBox ID="TextBox8" runat="server"></asp:TextBox></div>
<div class="column-left2">
    <asp:DropDownList ID="DDownResoncode2" runat="server" Height="20px" Width="153px">    
</asp:DropDownList></div>    
</div>
<div class="employeebox">
<div class="column-left2"><asp:TextBox ID="TextBox10" runat="server"></asp:TextBox></div>
<div class="column-left2">
    <asp:TextBox ID="TextBox11" runat="server"></asp:TextBox></div>
<div class="column-left2">
    <asp:DropDownList ID="DDownResoncode3" runat="server" Width="153px"></asp:DropDownList>
</div>    
</div>
<div class="employeebox">
    <div class="column-left2"><asp:TextBox ID="TextBox13" runat="server"></asp:TextBox></div>
    <div class="column-left2">
    <asp:TextBox ID="TextBox14" runat="server"></asp:TextBox></div>
    <div class="column-left2">
        <asp:DropDownList ID="DDownResoncode4" runat="server" Height="16px" Width="153px">       
</asp:DropDownList>
    </div>    
</div>
<div style="height: 33px">
    <asp:Button ID="addrow" runat="server" Text="AddRow" onclick="addrow_Click" />    
</div>
<div>
    <asp:Button ID="Button1" runat="server" Text="Submit" onclick="Button1_Click" />
</div>
</fieldset>

</asp:Content> 

FormReport.aspx。cs

protected void addrow_Click(object sender, EventArgs e)
    {
        TextBox newtextbox = new TextBox();
        Panel panel1 = new Panel();
        panel1.Controls.Add(panel1);
    }       

我想创建控件并以程序的方式将它们定位在我想要的位置

您可以使用HtmlGenericControl在面板内部创建div或在父面板内部添加面板

以及添加可以使用控件的样式。样式。添加("样式"、"值")

ASPX:或者您可以使用占位符

C#

protected void addrow_Click(object sender, EventArgs e)
{
    Panel pnlEmployeeBox = new Panel();
    pnlEmployeeBox.CssClass = "pnlEmployeeBox";
    HtmlGenericControl columnLeft2= new HtmlGenericControl("div");
columnLeft2.CssClass = "column-left2";
        TextBox txt1 = new TextBox();
columnLeft2.Controls.Add(txt1);
pnlEmployeeBox.Controls.Add(columnLeft2);
 columnLeft2 = new HtmlGenericControl("div");
        txt1 = new TextBox();
    pnlEmployeeBox.Controls.Add(columnLeft2);
// and same goes for the dropdown list

}