2

So I'm using ASP.NET with C#. And I have an item that is queried from the database and displays items correctly.

I made a comment table, where user enters a piece of text, and presses an AJAX-enhanced button, to insert the comment into the database, which the table listview below displays.

However, when the user enters a comment, the table stays the same, it doesn't refresh. If I press Post a comment again, then it will display the previous comment, but not the current one.

I tried on the "post a comment" button to add the following:

    CommentsView.DataBind();

No luck, it won't refresh. And some smart programmer at microsoft forgot to add a simple "ListView.Refresh()" function to ListView class.

I don't understand why AJAX doesn't automatically update the table to update the information. How do you get it to display the latest data?

6
  • Did you update CommentsView.DataSource after the user enters a comment? Commented Nov 29, 2010 at 18:28
  • 1
    Which AJAX implementation are you using? If you're using Microsoft's ASP.NET AJAX, you probably should wrap an UpdatePanel around the ListView so that you can trigger an update of the ListView by refreshing the UpdatePanel. Commented Nov 29, 2010 at 18:30
  • "..some smart programmer at microsoft.." Sarcasm on SO often does not go over well -- unless you are on meta.stackoverflow.com Commented Nov 29, 2010 at 18:40
  • KBoek, yes I am wrapping it with UpdatePanel, but it's updating the previous comment after each button-click, not the latest one added to database. Commented Nov 29, 2010 at 18:51
  • Don't take my sarcasm so seriously Hogan, I'm just angry that everything in ASP.NET is slow, cumbersome, and not straightforward compared to interpreted languages. Commented Nov 29, 2010 at 19:02

2 Answers 2

3

Those smart programmers at microsoft understand how databind works on apsx pages.

This involves understanding the page lifecycycle. If you read this documentation and understand it, the problem you are having should become clear.

But the quick answer is this: Right now you bind to the data in the page init. If you want that data to change you must rebind to the datasource. .NET automagically caches it for you in the viewstate otherwise.

Add some code to your "Post a comment" to rebind the datasource (and thus change the viewstate) and you should be fine.

Sign up to request clarification or add additional context in comments.

4 Comments

Of course they would understand, they created it. I get that it is in cache. However, I thought pageLoad would load again and requery the data. I made a function to requery the data and put in a new databind. Thanks.
@Dexter : not just in a cache -- in the viewstate. Viewstate is very different than cache and is key to understanding how ASP.NET works. Read this for good times : msdn.microsoft.com/en-us/library/ms972976.aspx#viewstate_topic3
Not sure I understand completely, isn't viewstate same as cache? Why would they be different?
hmmm... viewstate is more like a variable that is passed back and forth between the server and the browser to preserve state. A cache is a stored version of something that is retrieved instead of the original.
0

Simply do

listView.DataBind()

to update the listview.

1 Comment

I had to also wrap the listview in an UpdatePanel, probably because the DataBind() was triggered via an event from a control inside an UpdatePanel in a User Control.

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.