簡體   English   中英

如何保持用戶控制值?

[英]How to persist user control values?

我想保留用戶控制值

如果我再添加一行,則以前的控制值不存在

我瀏覽了一些網站,但看不清

碼:

protected void Page_Load(object sender, EventArgs e)
    {    
        if (!IsPostBack)
        {
            ViewState["ControlCount"] = ViewState["ControlCount"] == null ? 2 : ViewState["ControlCount"];
        }
    }

   public int controlCount
    {
        get
        {
            int val = 0;
            try
            {
                val = (int)ViewState["ControlCount"];
            }
            catch (Exception e)
            {
                // handle exception, if required.
            }
            return val;
        }
        set { ViewState["ControlCount"] = value; }
    }

我在這里添加用戶控件

   protected void addnewtext_Click(object sender, EventArgs e)
    {
        int i = controlCount++;

        for (int j = 0; j <= i; j++)
        {
            AddVisaControl ac = (AddVisaControl)Page.LoadControl("AddVisaControl.ascx");
            Label lb = new Label();
            string z = Convert.ToString(j + 1);
            lb.Text = "Visa " + z;
            rpt1.Controls.Add(lb);
            lb.Attributes.Add("class", "style8");
            rpt1.Controls.Add(ac);
            rpt1.Controls.Add(new LiteralControl("<BR>"))    
        }  
    }

AddVisaUserControl.ascx

 <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="AddVisaControl.ascx.cs" EnableViewState="false" Inherits="Pyramid.AddVisaControl" %>
<div id="divreg" runat="server">
<table id="tbl" runat="server">
    <tr>
    <td>
        <asp:Label ID="lbl2" runat="server"></asp:Label>
    </td>
</tr>
<tr>
<td> Visa Number:</td>
<td><asp:TextBox ID="txtUser" Width="160px" runat="server"/></td>
<td> Country Name:</td>
<td><asp:DropDownList ID="dropCountry" Width="165px" runat="server"></asp:DropDownList></td>
</tr>
<tr>
<td> Type of Visa:</td>
<td><asp:DropDownList ID="dropVisa" Width="165px" runat="server"></asp:DropDownList></td>
<td> Type of Entry:</td>
<td><asp:DropDownList ID="dropEntry" Width="165px" runat="server"></asp:DropDownList></td>
</tr>
<tr>
<td> Expiry Date</td>
<td>

</td>
</tr>
</table>
</div>

必須在每個回發中添加動態控件,以便ViewState可以維護其值。 有關更多詳細信息,請閱讀ASP.NET頁面生命周期。

暫無
暫無

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

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