?
Solved

Controls added to Tablecell loses data on postback.

Posted on 2007-10-18
10
Medium Priority
?
481 Views
Last Modified: 2013-11-07
Hello.

I have a Table on my webpage.

On first load of the page i fill this table with new rows .

Each row has 2 cells.

Cell1 and Cell2.

For each cell i fill some stuff in.
for Cell1 i set .Text = "xxx".
for Cell2 i add a CheckBox as a new control to the cell.
The .Checked property of this CheckBox i set to a value from my SQL Statement.

All of this is working fine.

I add the properties to the cells after i add the cell to the row. This way i should save the text values ect. on postback.

And this works fine.

But, heres the problem.

For each Checkbox i've added to a cell, the user should be able to check this checkbox, and when i click a "Update SQL Table Button" on the webpage, i want to update my SQL Table and for each row in the table set the .Checked value in the SQL DB to match what the user choose on the website.


I do this by first calling a method called "CreateTableStructure()". This Method recreates all rows and cells and add's new checkboxes, but does not set the properties. This works fine...

After that i call a method called "UpdateSQLTaskList()". Both on page_Load event if the page is a postback.

but When i try to loop through my table all my checkbox values are = false, even though the are checked the the CreateTableStructure() has finished. I can see on the webpage that they are checked, but still the have a value of false, when trying to update my SQL DB.

What am i doing wrong?


Thank you in advance.

/Lasse
0
Comment
Question by:SkareMeatPackers
  • 6
  • 3
10 Comments
 
LVL 18

Expert Comment

by:Ajay Sharma
ID: 20099755
r u generating the check boxes using the html component ?
Are they having server side tag ?
0
 

Author Comment

by:SkareMeatPackers
ID: 20100106
I have uploaded my projekt. It's not that big. Only 1 aspx side.

http://www.skare.com/employsystem.zip

Maybe it is more easy to find the problem if you can see the code for yourself??
An SQL DB is attached and a DLL component that i created myself, to make SQL calls and other stuff more easy.
thnx for your help.


/Lasse
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 20102370
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

Author Comment

by:SkareMeatPackers
ID: 20103433
Yes emoreau, but that is not the problem.

Let me try to simplify it :

1. load of a page. (Therefore not a postback).

a. ) I connect to my sqlserver, do a query, and read the result.
b.) For each record found i create a new row in my table with 2 cells.
c.) For cell nr. 2 in each row i also add a control. A Checkbox. The value of this Checkbox i set to true/false depending on a value from the SQL query.

Not the user sees a table and can check all or some checkboxes if they are not checked already.
The user then presses a button to update the sql db, and save checked status for each row in the db.

2.) Load of page again as the button is pressed (Now it is a postback.)

a.) again i do the same query in the sqldb but this time i only add the rows, cells and checkboxes to the table. I dont set any properties. The viewstate should still have them stored. And it does...

b.) Next i do a update sql query, to update the db as described above. I run through all rows in the table, and try to get the checked/uncheked property for each checkbox. However they all have a .Checked property value that i set to FALSE, therefore my update statement will allways set every record in the db to be unchecked.

c. ) After this happens, i then see the reloaded page. And all the checkboxes seems to look fine. those checkboxes i checked before i clicked my button, are still checked.

So what the f... do i keep getting a value of false, when i run through the table trying to update the sql db?

If only the checkboxes did not stay checked when the page reloaded, then i could understand it. But when they visually seems to be cheked, why don't i then get a TRUE value in the .Checked property when looping through the table???

And sorry about the f...

/Lasse
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 20104042
that is exactly what this control is about. keep the controls and their values between postback.
0
 

Author Comment

by:SkareMeatPackers
ID: 20105042
Okay, maybe i just don't understand how to use it then.

Could you give an example based on my code?

I have tried to add that control to the cells instead, and then add the checkbox as a child control to that new placeholder control. But i don't think that i understand the control good enough to know if i am on the right path or not.

When i do so, i get an null reference exception on the postback.  On first load it add's the controls correctly.

I guess an example based on my code, posted above as a zip, would be a great idea!

/Lasse
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 1000 total points
ID: 20105168
there are samples you can download from the web site
0
 

Author Comment

by:SkareMeatPackers
ID: 20107217
I still don't understand that control, after having read the demo source for c#.

I have made a very simple example, of what my problem is. Maybe you could show me how to implement that control into this example :

Heres my problem :

public partial class _Default : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            AddToTable(true);
        }
        else
        {
            AddToTable(false);
        }
    }

    void AddToTable (Boolean DoDataFill)
    {
        TableCell C = new TableCell();
        TableRow R = new TableRow();
        R.Cells.Add(C);
        Table1.Rows.Add(R);

        CheckBox cb = new CheckBox();
        Table1.Rows[0].Cells[1].Controls.Add(cb);
       
        if (!DoDataFill)
        {
            CheckBox checkbox = Table1.Rows[0].Cells[1].Controls[0] as CheckBox;
            String StringthatalwaysreturnsFALSE= checkbox.Checked.ToString();
        }
    }
}

The String called : StringthatalwaysreturnsFALSE
will always return value FALSE. No matter if the checkbox is checked or not. That is what i don't understand.

/Lasse
0
 

Author Comment

by:SkareMeatPackers
ID: 20107408
AHA! I found out myself.

I had to rebuild the table in the pagE_Load event, but check the value in the Button1_Click Event.
0
 

Author Comment

by:SkareMeatPackers
ID: 20122819
But thnx for the suggestions anyway!

Points for that!

/Lasse
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month16 days, 3 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question