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

标签 c# asp.net

这是我的要求:

  • 我有一个下拉列表和文本框(appName 和配置文件)。
  • 我想从下拉列表和文本框中获取值并将它们添加到表格(或呈现为 表)
  • 有时我希望能够遍历表并将值提交到数据库。

我的问题:

  • 由 onClick 引起的回发甚至将表格封装为仅显示最后输入的值,并且不保留任何以前的值 值(value)观。

注意事项:

  • 我尝试使用绑定(bind)到数据网格的数据列表来解决这个问题,但没有成功。

代码:

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

解决我问题的代码:

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

最佳答案

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

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

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

关于c# - 通过按钮 onclick 事件在 ASP.NET 中创建动态表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8318279/

相关文章:

c# - 使用 C# 将 S3 对象移动到同一存储桶中但具有相同权限的另一个文件夹

javascript - 使用站点身份验证 cookie 的摘要进行 CSRF

c# - 如何在 C# 中的 GridView 中隐藏页脚行

c# - asp.net 中的第一个数据 api 证书创建错误

ASP.NET MVC。扩展 HtmlHelper 与自定义类

c# - 为什么下面这个简单的 Rx.NET 不打印任何数字?

c# - 为什么无法检查模型兼容性?

javascript - 在 html select 上设置选定值而不使用 runat=server

javascript - 通过传递参数在 JavaScript 函数中捕获控件 ID

javascript - 使用 Handler.ashx 获取图像