Creating dynamic tables in ASP.NET using the onclick event button

Here are my requirements:

  • I have a drop-down list and a text box (application name and profile).
  • I want to take values ​​from a drop-down list and a text field and add them to a table (or a control like gridview that displays the table)
  • At some point, I want to be able to scroll through a table and pass values ​​to db.

My problem:

  • Rollback caused by onClick parity parses the table to display only the last entered value and does not save any of the previous values.

Notes:

  • I tried to work with this using a datalist tied 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; } } 
+4
source share
1 answer

How about saving a list of objects (they can even be simple pairs of key values) in the ViewState. You can use this data as a data source for a GridView. I think this is the easiest way. If you need more information, let me know.

Editing. Your solution above looks good. I could just simplify setting the properties for your ViewState values ​​a bit.

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

Source: https://habr.com/ru/post/1383747/


All Articles