Why do texboxes in the main body of a form act differently to postback than textboxes in a repeater

M

Mike Lerch

I am wondering this: why do texboxes in the main body of a form act
differently to postback than textboxes in a repeater on the form? I
posted earlier about not being able to capture a user's input from a
textbox, and I finally figured out what was happening: the textbox
was bound, and it was getting stuffed with the database value both on
the first load of the form AND after the user had changed stuff and
clicked the button that fired the postback.

So, if I basically added this:

if (!Page.IsPostBack)
{
Page.DataBind();
}

and now those textboxes are working fine. But I really don't
understand this: the textboxes within the repeater did and still do
work as I expected. If I change something in them, I can step through
the code and watch as they are re-databound, and THEN the OnChange
event fires. But where the OnChange event of the main form's
textboxes reveals that the textboxes' Text is whatever was originally
in the database, the textboxes within the repeater are showing the
user's text. HOW IS THIS POSSIBLE?

Any help in understanding this is greatly appreciated, as I think I
see how to make everything work the way I want, but I really want to
understand WHY this is.

Also, if there are any great resources describing the interrelations
of databinding, Page.IsPostBack and viewstate, I'd really like to know
them!

TIA,
Lerch
 
A

Abhijeet Dev

http://www.wilsondotnet.com/Demos/ViewState.aspx

In general conditions, all the elements of page should be loaded only when
its not a PostBack request.
To find out more about PostBack , you can just have a look at the html
source of rendered web page. In ASP.NET, web/html component when rendered,
it checks for the event hadlers attached to the events that it has published
and generates a common method __doPostBack, which is attached to equivalent
events for those objects in html document object model.

You can see this code automatically generated:
<input type="hidden" name="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" value="" />
function __doPostBack(eventTarget, eventArgument) {
var theform;
if (window.navigator.appName.toLowerCase().indexOf("netscape") > -1) {
theform = document.forms["Form1"];
}
else {
theform = document.Form1;
}
theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
theform.__EVENTARGUMENT.value = eventArgument;
theform.submit();
}

and then for an HtmlButton in which an eventhandler is attached in aspx.cs
file to ServerClick event in aspx file.
<input language="javascript" onclick="javascript:finalize();
__doPostBack('Submit1','')" name="Submit1" id="Submit1" type="button"
value="Test Button" />

When you databind a control, it retrieves all the data from datasource
according to ur specifications and store it in its own fields and when
render method is called, it renders html alongwith that data.

Hope it will make it easier for u to dig more into it.

Abhijeet Dev
 
M

Mike Lerch

http://www.wilsondotnet.com/Demos/ViewState.aspx

In general conditions, all the elements of page should be loaded only when
its not a PostBack request.
...
Hope it will make it easier for u to dig more into it.

Thanks, I'm going to look at that site. I guess what's confusing me
is that my app doesn't meet the general conditions: it needs to load
data the very first time the user sees the page (what data is loaded
is determined through the querystring, which are passed as parameters
to a stored procedure). On postback, the user's changes need to go
back to the database. If I don't bind on the initial page view, the
user isn't going to see the information they need to edit! (In fact,
they'll barely see anything at all since my page is mostly a repeater,
which won't even display if it doesn't have any rows!)

Is there an example out there of a simple user account type thing?
You know, where your website maintains a membership list and you give
the opportunity to let members edit their phone number in case it
changes? That would be similar to what I'm trying to do: load the
data into the controls based on what user it is, let them edit the
data in those controls, then send their edits back to the database.

This has to be a common thing!

Lerch
 
A

Abhijeet Dev

I had worked on the same thing once.. but then i was working in a company,
so they own the code. I had designed and implemented usercontrols to achieve
all this. Based on the user rights, it renders 'edit' or not. When clicked
on edit, caption of edit button changes to update and all the text boxes
from readonly mode to a normal textbox, where user could modify data and
post it back to the server. The whole process is quite easy to understand
and it works smoothly.
You can mail me back on my personal email account, we can take it on from
there.

Abhijeet Dev
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top