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

时间:2014-01-31 11:05:52

标签: 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></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>

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());
                        }
                    }
                }
            }
    }
}

我想要做的是,在PostBack之后,在我添加了数字2后,它将再次显示2个DropDownlist,2个Textboxes,2个Labels,2个Textbox和2个其他标签。 我在Post back后尝试做,在TextBox cmimi = new TextBox();输入一个数字后,它将自动添加到下面的标签中。我想做但没有运气。 我希望你理解我的解释。 谢谢。 :)

0 个答案:

没有答案