Beans
Beans

Reputation: 329

Creating Dynamic Tables in ASP.NET via button onclick event

Here are my requirements:

My problem:

Notes:

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

Upvotes: 4

Views: 1674

Answers (1)

ek_ny
ek_ny

Reputation: 10243

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

Upvotes: 5

Related Questions