4

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;
        }
    }
1
  • This could use some error handling, but it demonstrates what I am trying to do. Commented Nov 30, 2011 at 16:18

1 Answer 1

5

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;
 }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I actually attempted this, but I think where I went wrong was storing the list locally rather than in the view state. I'm going to work on this now, but any examples would be great.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.