簡體   English   中英

如何在C#中創建動態控件(文本框)以及如何設置初始值

[英]How do I create a dynamic control (text box) in C# and how to set the initial the value

單擊按鈕后如何在C#中創建動態控件(文本框)?
回發后如何保留動態控件?
如何保持文本框的值?

我嘗試了以下代碼,buttonclick:

List<string> lstDynId;
int iUnique = 0;
string strDynId = "tbx";

if (ViewState["strVS_DynamicIds"] != null)
{
    DisplayRTControls();
    lstDynId = (List<string>)ViewState["strVS_DynamicIds"];
    //get the Id of last textbox
    string strLastControlId = lstDynId[lstDynId.Count - 1];
    // get the substring starting after "tbx"
    string strIdNumber = strLastControlId.Substring(3);
    iUnique = int.Parse(strIdNumber) + 1;
}
else
{
    lstDynId = new List<string>();
}
strDynId += iUnique;
TextBox tbx = new TextBox();
tbx.ID = strDynId;
tbx.EnableViewState = true;
HtmlTableRow new1 = new HtmlTableRow();
new1.ID = "row" + strDynId;
HtmlTableCell cell1 = new HtmlTableCell();
cell1.ID = "cell" + strDynId;
TableCell cell2 = new TableCell();
cell1.Controls.Add(tbx);
new1.Cells.Add(cell1);
try
{
    tbl.Rows.Add(new1);
}
catch
{
}
lstDynId.Add(strDynId);
ViewState["strVS_DynamicIds"] = lstDynId;

private void DisplayRTControls()
{
    if (ViewState["strVS_DynamicIds"] != null)
    {
        List<string> lstDynId = (List<string>)ViewState["strVS_DynamicIds"];
        foreach (string strId in lstDynId)
        {
            TextBox tbx = new TextBox();
            tbx.ID = strId;
            tbx.Text = tbx.Text;
            tbx.EnableViewState = true;
            HtmlTableRow new1 = new HtmlTableRow();
            HtmlTableCell cell1 = new HtmlTableCell();
            TableCell cell2 = new TableCell();
            cell1.Controls.Add(tbx);
            new1.Cells.Add(cell1);
            tbl.Rows.Add(new1);
        }
    }      
}

問題是我不能保留文本框的值? 誰能幫我?

您可以執行以下操作:

.aspx頁:

<div>
   <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Add Textbox" />
   <asp:Panel ID="Panel1" runat="server">
   </asp:Panel>
</div>

.aspx.cs頁:

私人列表文本框;

protected void Page_Load (object sender, EventArgs e)
{
    PreRender += new EventHandler(_Default_PreRender);

    textboxes = new List<TextBox>();

    if (IsPostBack)
    {
        //recreate Textboxes
        int count = Int32.Parse(ViewState["tbCount"].ToString());

        for (int i = 0; i < count; i++)
        {
            TextBox tb = new TextBox();
            tb.ID = "tb" + i;

            Panel1.Controls.Add(tb);

            textboxes.Add(tb);

            tb.Text = Request.Form[tb.ClientID];
        }
    }
}

protected void Button1_Click(object sender, EventArgs e)
{
    //create new textbox
    TextBox tb = new TextBox();
    tb.ID = "tb" + textboxes.Count;

    Panel1.Controls.Add(tb);

    textboxes.Add(tb);        
}

void _Default_PreRender(object sender, EventArgs e)
{
    //remember how many textboxes we had
    ViewState["tbCount"] = textboxes.Count;
}

有關詳細的源代碼,請參見此處的原始博客文章。

謝謝。

為案例使用服務器端控件將有些技巧,因為您必須在Page Init事件中重新創建它們,以便它們在Text屬性中獲取發布的值。

通過Request.Form恢復服務器端控件值也很麻煩,因為我們不知道PostBack之后每個文本框的HTML name屬性,而這取決於您的控件樹。

您可以使用HtmlInputText而不是TextBox

HtmlInputText tbx = new HtmlInputText();
tbx.ID = strDynId;
tbx.Name = strDynId;

要在PostBack上恢復值,請執行以下操作:

Request.Form[strDynId]

如果要保留TextBox ,則必須在將它們的UniqueId添加到Controls集合或對Request.Form調用中的名稱進行硬編碼之后,找到一種存儲其UniqueId的方法。

為了保留文本框值,請考慮以下示例:

<asp:TextBox ID="txtTotalAmt" runat="server" class="txtfld-popup2" MaxLength="5"></asp:TextBox>

在頁面加載背后的代碼上,您只需添加以下行代碼:-

 protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        TextBox txtValue = new TextBox();
        txtValue.Text = Request.Form[txtTotalAmt.UniqueID];
    }
}

讓我知道是否有效

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM