Salve a tutti. Scrivo questo post dato che sto cercando di imparare ad usare il viewstate ma ho avuto problemi su un applicazione di prova che avevo trovato e analizzato per esercitarmi.
Questa applicazione funziona così: ci sono 2 textbox dove l'utente inserisce i valori per il numero di colonne e righe di una tabella che viene creata dinamicamente. Con il pulsante "Generate" viene generata la tabella mentre con il pulsante "Cause Postback" vengono chiamati dei postback. Lo scopo del programma è modificare i dati della griglia e ritrovarseli dopo il postback. Il problema di tale programma è che lanciando dei postback (AL PRIMO) i dati modificati nelle textbox vengono resettati, mentre con i successivi postback funziona tutto perfettamente. Qualcuno ha 1 soluzione a questo problema??
Luca

codice:
        Rows: <asp:TextBox ID="txtRows" runat="server" Width="30px"> </asp:TextBox> 

        Cols: <asp:TextBox ID="txtCols" runat="server" Width="30px"></asp:TextBox>
        

        

        <asp:Button ID="btnGenerate" OnClick="btnGenerate_Click" runat="server" Text="Generate" />
        
 

        <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
        

        <asp:Button ID="btnPost" runat="server" OnClick="Button1_Click" Text="Cause Postback" />
        <asp:Label ID="LabelErrore" runat="server" Text=""></asp:Label>

codice cs:

codice:
    protected int Rows
    {
        get
        {
            return ViewState["Rows"] != null ? (int)ViewState["Rows"] : 0;
        }
        set
        {
            ViewState["Rows"] = value;
        }
    }

    // Columns property to hold the Columns in the ViewState
    protected int Columns
    {
        get
        {
            return ViewState["Columns"] != null ? (int)ViewState["Columns"] : 0;
        }
        set
        {
            ViewState["Columns"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        CreateDynamicTable();        
    }

    protected void btnGenerate_Click(object sender, EventArgs e)
    {
        //Set the Rows and Columns property with the value
        //entered by the user in the respective textboxes
        this.Rows = Int32.Parse(txtRows.Text);
        this.Columns = Int32.Parse(txtCols.Text);
        CreateDynamicTable();
    }


    private void CreateDynamicTable()
    {
        PlaceHolder1.Controls.Clear();

        // Fetch the number of Rows and Columns for the table 
        // using the properties
        int tblRows = Rows;
        int tblCols = Columns;
        // Create a Table and set its properties 
        Table tbl = new Table();
        // Add the table to the placeholder control
        PlaceHolder1.Controls.Add(tbl);
        // Now iterate through the table and add your controls 
        for (int i = 0; i < tblRows; i++)
        {
            TableRow tr = new TableRow();
            for (int j = 0; j < tblCols; j++)
            {
                TableCell tc = new TableCell();
                TextBox txtBox = new TextBox();
                txtBox.Text = i.ToString() + j.ToString();
                // Add the control to the TableCell
                tc.Controls.Add(txtBox);
                // Add the TableCell to the TableRow
                tr.Cells.Add(tc);
            }
            // Add the TableRow to the Table
            tbl.Rows.Add(tr);
        }

        // This parameter helps determine in the LoadViewState event,
        // whether to recreate the dynamic controls or not

        ViewState["dynamictable"] = true;
    }

    // Check the ViewState flag to determine whether to
    // rebuild your table again
    protected override void LoadViewState(object earlierState)
    {
        base.LoadViewState(earlierState);
        if (ViewState["dynamictable"] == null)
            CreateDynamicTable();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {

    }