如何保持用户控制值? [英] How to persist user control values?

查看:105
本文介绍了如何保持用户控制值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要保持用户的控制值

如果我增加一个行previous控制值是不存在的。

If I add one more rows the previous control values is not there

我通过一些网站走了,但我不能清楚地了解

I gone through some sites but I'm not able to understand clearly

code:

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

在这里,我加入了用户控件

Here I am adding the User controls

   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

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>

任何想法?谢谢advacne

Any ideas? Thanks in advacne

推荐答案

动态控制必须在每次回传被加入,使ViewState中能保持它的价值。有关详细信息,请阅读ASP.NET页面生命周期。

The dynamic controls must be added on every postback, so that ViewState can maintain its values. For more details, please read the ASP.NET page life cycle.

这篇关于如何保持用户控制值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆