如何读取用户控件值?

时间:2013-09-26 06:42:42

标签: c# asp.net user-controls viewstate

我想从用户控件中获取值

我试过但循环出来了

代码:

的.aspx

<asp:Repeater ID="rpt1" runat="server">
<ItemTemplate>
</ItemTemplate>
</asp:Repeater>
<asp:Button ID="btnSaveVisa" Text="Save" runat="server" OnClick="btnSaveVisa_Click" />   

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>

.aspx.cs

public void generateControls()
    {
        for (int i = 0; i < int.Parse(ViewState["ControlCount"].ToString()); i++)
        {
            Label lbl = new Label();
            string count = Convert.ToString(i + 1);
            lbl.Text = "Visa" + count;
            rpt1.Controls.Add(lbl);
            rpt1.Controls.Add(LoadControl("VisaUserControl.ascx"));
            rpt1.Controls.Add(new LiteralControl("<BR>"));
        }
    }
    protected void btnAddVisa_Click(object sender, EventArgs e)
    {
        ViewState["ControlCount"] = int.Parse(ViewState["ControlCount"].ToString()) + 1;
        generateControls();
    }

//当我从控件中读取值时出现问题<循环出现

    private void saveData()
    {
        for (int i = 0; i < this.rpt1.Controls.Count; i++)
        {
            if (this.rpt1.Controls[i] is TextBox)
            {
                TextBox txtserial = (TextBox)this.rpt1.Controls[i];
                string value = txtserial.Text;
            }
        }
    }

    protected void btnSaveVisa_Click(object sender, EventArgs e)
    {
       saveData();
    }

enter image description here

有什么想法吗?提前致谢

1 个答案:

答案 0 :(得分:1)

   public List<string> NoOfControls
    {
        get
        {
            return ViewState["NoOfControls"] == null ? new List<string>() : (List<string>)ViewState["NoOfControls"];
        }
        set
        {
            ViewState["NoOfControls"] = value;
        }

    }


    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);

        GenerateControls();
    }



    private void GenerateControls()
    {
        foreach (string i in NoOfControls)
        {
            var ctrl = (AddVisaUserControl)LoadControl(@"AddVisaUserControl.ascx");
            ctrl.ID = i;
            this.AddVisaPlaceHolder.Controls.Add(ctrl); // Add in placeholder
        }
    }

    //Adding controls to Place Holder
    protected void AddButton_Click(object sender, EventArgs e)
    {

        List<string> temp = null;
        var uc = (AddVisaUserControl)this.LoadControl(@"AddVisaUserControl.ascx");

        string id = Guid.NewGuid().ToString();
        uc.ID = id;

        temp = NoOfControls;
        temp.Add(id);
        NoOfControls = temp;
        AddVisaPlaceHolder.Controls.Add(uc);
    }

    //Save
    protected void Save_Click(object sender, EventArgs e)
    {

        foreach (var control in AddVisaPlaceHolder.Controls)
        {
            var usercontrol = control as AddVisaUserControl;

          //you can access properties from usercontrol

            //Implement save logic here
        }

    }