通过按钮onclick事件在ASP.NET中创建动态表

Bar*_*nes 4 c# asp.net

这是我的要求:

  • 我有一个下拉列表和文本框(appName和profile).
  • 我想从下拉列表和文本框中获取值,并将它们添加到表(或像gridview一样的控件呈现到表中)
  • 在某些时候,我希望能够遍历表并将值提交给数据库.

我的问题:

  • 由onClick引起的回发甚至将表格仅显示为输入的最后一个值,并且不保留任何先前的值.

笔记:

  • 我试图使用绑定到数据网格的数据列表来工作,但没有运气.

码:

    protected void addAppButton_Click(object sender, EventArgs e)
    {
        DropDownList appList = (DropDownList)newEntryFV.FindControl("appList");
        TextBox profileTextBox = (TextBox)newEntryFV.FindControl("profileTextBox");
        addAppsToTable(appList.SelectedValue.ToString(), profileTextBox.Text.ToString());
    }

    private void addAppsToTable(string appName, string profileName)
    {
        Table appsTable = (Table)newEntryFV.FindControl("appTable");
        TableRow newRow = new TableRow();
        TableCell appNameCell = new TableCell();
        TableCell profileCell = new TableCell();
        appNameCell.Text = appName;
        profileCell.Text = profileName;
        newRow.Cells.Add(appNameCell);
        newRow.Cells.Add(profileCell);
        appsTable.Rows.Add(newRow);
    }
Run Code Online (Sandbox Code Playgroud)

解决我的问题的代码:

    [Serializable]
    public class securityApps
    {
        public string secAppID { get; set; }
        public string secAppName { get; set; }
        public string secProfile { get; set; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        BindApps();
    }

    protected void addAppButton_Click(object sender, EventArgs e)
    {
        DropDownList appList = (DropDownList)newEntryFV.FindControl("appList");
        TextBox profileTextBox = (TextBox)newEntryFV.FindControl("profileTextBox");
        addAppsToListVS(appList.SelectedValue.ToString(), appList.SelectedItem.Text.ToString(), profileTextBox.Text.ToString());
        BindApps();

    }

    private void addAppsToListVS(string appID, string appName, string profile)
    {
        securityApps secApp = new securityApps();
        secApp.secAppID = appID;
        secApp.secAppName = appName;
        secApp.secProfile = profile;
        ((List<securityApps>)ViewState["appsListVS"]).Add(secApp);
    }
    // Binds apps to Grid View
    private void BindApps()
    {
        GridView appsListGV = (GridView)newEntryFV.FindControl("appsListGV");
        if (ViewState["appsListVS"] != null)
        {
            appsListGV.DataSource = (List<securityApps>)ViewState["appsListVS"];
            appsListGV.DataBind();
        }
        else
        {
            List<securityApps> appsListVS = new List<securityApps>();
            ViewState["appsListVS"] = appsListVS;
        }
    }
Run Code Online (Sandbox Code Playgroud)

ek_*_*_ny 5

如何在ViewState中存储对象列表(它们甚至可以是简单的键值对).您可以将该数据用作GridView的DataSource.我认为这是最简单的方法.如果您需要更多详细信息,请与我们联系.

编辑 - 上面的解决方案看起来不错 - 通过为ViewState值设置属性,我可能会让它更容易一些.

List<securityApps> AppsListVS{
 get
 { 
    if(ViewState["AppListVS"] == null
       this.AppListVS = new List(securityApps)();
     return (List<securityApps>)ViewState["AppListVS"];
 }
 set
 {
      ViewState["AppListVS"] = value;
 }
}
Run Code Online (Sandbox Code Playgroud)