简体   繁体   中英

Creating Dynamic Tables in ASP.NET via button onclick event

Here are my requirements:

  • I have a dropdown list and text box (appName and profile).
  • I want to take the values from the dropdown and text box and add them to a table (or a control like gridview that renders into a table)
  • At some point I want to be able to loop through the table and submit the values to a db.

My problem:

  • The postback caused by the onClick even is casing the table to only show the last value entered, and doesn't retain any of the previous values.

Notes:

  • I tried to work arond this using a datalist bound to a datagrid, but no luck.

Code:

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

Code that solved my problem:

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

How about storing a List of objects (they could even be simple key value pairs) in the ViewState. You can use that data as the DataSource for a GridView. I think that's the simplest way to go. If you need more details, let me know.

Edits-- Your solution above looks good-- I might just make it a little easier by setting up a property for your ViewState values..

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM