C# 在标签asp net中显示从文本框输入的文本

C# 在标签asp net中显示从文本框输入的文本,c#,asp.net,postback,C#,Asp.net,Postback,所以我在asp.net中有这个代码 Default.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title&

所以我在asp.net中有这个代码

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            width: 248px;
        }
        .auto-style2 {
            width: 253px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
    <div>
        <table style="width:100%;">
            <tr>
                <td class="auto-style2">
                    &nbsp;</td>
                <td>
                    <asp:SqlDataSource ID="Artikujt" runat="server" ConnectionString="<%$ ConnectionStrings:ChipString %>" SelectCommand="SELECT * FROM [Artikujt]"></asp:SqlDataSource>
                </td>
            </tr>
            <tr>
                <td class="auto-style1">&nbsp;</td>
                <td class="auto-style2">
        <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" ></asp:TextBox>
                </td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style1">&nbsp;</td>
                <td class="auto-style2">
        <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
                </td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style1">&nbsp;</td>
                <td class="auto-style2">
        <asp:PlaceHolder ID="PlaceHolder2" runat="server"></asp:PlaceHolder>
                </td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style1">&nbsp;</td>
                <td class="auto-style2">
        <asp:PlaceHolder ID="PlaceHolder3" runat="server"></asp:PlaceHolder>
                </td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style1">&nbsp;</td>
                <td class="auto-style2">
        <asp:PlaceHolder ID="PlaceHolder4" runat="server"></asp:PlaceHolder>
                </td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style1">&nbsp;</td>
                <td class="auto-style2">
        <asp:PlaceHolder ID="PlaceHolder5" runat="server"></asp:PlaceHolder>
                </td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style1">&nbsp;</td>
                <td class="auto-style2">
                    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
                </td>
                <td>&nbsp;</td>
            </tr>
        </table>
        <br />
    </div>
    </form>
</body>
</html>

.auto-style1{
宽度:248px;
}
.auto-style2{
宽度:253px;
}

Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

public partial class Default2 : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            AddItems();
        }
    }

    DropDownList artikulli;
    TextBox cmimi;
    Label tregoCmimi;
    TextBox sasia;
    Label cmimiGjithsej;

    protected void AddItems()
    {
            if (!string.IsNullOrEmpty(TextBox1.Text))
            {
                int a = int.Parse(TextBox1.Text);
                for (int j = 1; j <= a; j++)
                {

                    artikulli = new DropDownList();
                    cmimi = new TextBox();
                    tregoCmimi = new Label();
                    sasia = new TextBox();
                    cmimiGjithsej = new Label();

                    //artikulli.ID = j.ToString(IDUnik.ToString("N").Substring(31));

                    artikulli.ID = "artikulli_" + j.ToString();
                    artikulli.AutoPostBack = true;

                    cmimi.ID = "cmimi_" + j.ToString();
                    cmimi.AutoPostBack = true;

                    tregoCmimi.ID = "tregoCmimi_" + j.ToString();

                    sasia.ID = "sasia_" + j.ToString();
                    sasia.AutoPostBack = true;

                    cmimiGjithsej.ID = "cmimiGjithsej_" + j.ToString();

                    PlaceHolder1.Controls.Add(artikulli);
                    PlaceHolder2.Controls.Add(cmimi);
                    PlaceHolder3.Controls.Add(tregoCmimi);
                    PlaceHolder4.Controls.Add(sasia);
                    PlaceHolder5.Controls.Add(cmimiGjithsej);



                    DataTable listaArtikujt = new DataTable();

                    using (SqlConnection lidhje = new SqlConnection(ConfigurationManager.ConnectionStrings["ChipString"].ConnectionString))
                    {
                        try
                        {
                            SqlDataAdapter adapter = new SqlDataAdapter("SELECT [Artikulli] FROM [Artikujt]", lidhje);
                            adapter.Fill(listaArtikujt);

                            artikulli.DataSource = listaArtikujt;
                            artikulli.DataTextField = "Artikulli";
                            artikulli.DataBind();
                        }
                        catch (Exception ex)
                        {
                            Response.Write("Gabim:" + ex.ToString());
                        }
                    }
                }
            }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用System.Data.SqlClient;
使用系统配置;
使用系统数据;
公共部分类Default2:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(iPostBack)
{
AddItems();
}
}
投递员artikulli;
文本框cmimi;
标记Tregocmi;
文本框sasia;
标签cmimighitsej;
受保护的无效附加项()
{
如果(!string.IsNullOrEmpty(TextBox1.Text))
{
int a=int.Parse(TextBox1.Text);

对于(int j=1;j)当你尝试这样做时,到底发生了什么事,与你想要的相反?每次发回后,最后生成的控件都将被删除?@HamedShams在我添加e number后创建的新文本框上,它将显示在标签中,但我没有运气/