 |
BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6
 | This is the forum to discuss the Wrox book ASP.NET 2.0 Instant Results by Imar Spaanjaars, Paul Wilton, Shawn Livermore; ISBN: 9780471749516 |
|
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|
|

August 11th, 2007, 10:05 AM
|
|
Authorized User
|
|
Join Date: Aug 2007
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
Consider a label: it gets rendered in the browser as a <span> element so it doesn't use a form control.
However, when you set the Label's Text property, it will survive postbacks because it uses ViewState as
the backing store for the text.
|
I am sorry , I am not quite familiar with HTML , does form control is something like
<input:*** type="***" > and wrapped in the outter <form action="***"> ?
------------------------------------------------------------------------
We learn from the history that we do not learn from the history
|
|

August 11th, 2007, 10:35 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Yes. Many of the server side asp:* controls, like TextBox, CheckBox, RadioButton and so on end up as HTML form controls in the browser.
By design, the entire page is always wrapped in an HTML <form> element.
Juts create a simple page in Visual Web Developer, drag a few controls on it and then open the page in the browser. Then right-click the page in the browser, choose View Source to open up the HTML for the page.
Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
|
|

August 11th, 2007, 11:31 AM
|
|
Authorized User
|
|
Join Date: Aug 2007
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks for your quick reply.
I am so happy I have a clear idea of the ViewState and Postback.
Now I want to ask you another question.
This question is about the event of the DataControls like GridView.
I am quite confuse of these three events of GridView:
1.RowCreated
2.RowDataBound
3.DataBound
My question is
1.the order of the event fired.(that is which come first,and which follows)
2.In each event , is the data the GridView bound to accessable?
And how to access?
------------------------------------------------------------------------
We learn from the history that we do not learn from the history
|
|

August 11th, 2007, 11:38 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
The best way to find this out, and the best way to learn .NET is to simply try it out:
1. Create a new web site
2. Add a GridView
3. Add a *DataSource control and bind it to the GridView
4. Write handlers for the events you mentioned.
5. Set breakpoints in each of the handlers.
Run the page by pressing F5 and see what happens when. When the code breaks, look at the e argument in the Watch window.
Hope this helps,
Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
|
|

August 11th, 2007, 11:42 AM
|
|
Authorized User
|
|
Join Date: Aug 2007
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks again for your quick reply.
Now I will try.:)
------------------------------------------------------------------------
We learn from the history that we do not learn from the history
|
|

August 12th, 2007, 04:02 AM
|
|
Authorized User
|
|
Join Date: Aug 2007
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Imar,I realy think you for not only tell me the answer,but also tell me how to get the answer for myself by make good use of the tools and trying out some litter experiment.We Chinese have a goodold saying ,"Teaching him fishing rather than give him fish". This is the best answer I have ever get from the Wrox forum.Thank you very much,you are my best teacher.
My expreriment is as follow:
Code:
in the aspx page:
<asp:GridView ID="gvwTest" runat="server" OnRowDataBound ="gvwTest_RowDataBound" OnRowCreated ="gvwTest_RowCreated"
AutoGenerateColumns="False" DataKeyNames="ArticleID" DataSourceID="SqlDataSource1" >
<Columns >
<asp:BoundField DataField="ArticleID" HeaderText="ArticleID" InsertVisible="False"
ReadOnly="True" SortExpression="ArticleID" />
<asp:BoundField DataField="AddedBy" HeaderText="AddedBy" SortExpression="AddedBy" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [ArticleID], [AddedBy], [Title] FROM [tbh_Articles]">
</asp:SqlDataSource>
and in the aspx.cs code behind file:
public partial class GridView : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e){ }
protected void gvwTest_DataBinding(object sender, EventArgs e) { }
protected void gvwTest_DataBound(object sender, EventArgs e) { }
protected void gvwTest_RowCreated(object sender, GridViewRowEventArgs e)
{
Response.Write("---------------------- in gvwTest_RowCreated -------------------------<br />");
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells != null)
{
e.Row.Attributes.Add("onClick", "alert('Selected value: " + e.Row.Cells[0].Text + "')");//Got "" here
DataRowView dataRowView = (DataRowView) e.Row.DataItem;//the binding data can be access by this way
if (dataRowView != null)
{
for (int i = 0; i < dataRowView.Row.ItemArray.Length; i++)
{
Response.Write("DataRowView.Row.ItemArray[" + i.ToString() + "]:" +
dataRowView.Row.ItemArray[i] + "<br />");
}
}
if (gvwTest.DataKeys != null && gvwTest.DataKeys.Count > 0)
//The DataKeys is also accessable,and I wonder why
{
for (int i = 0; i < gvwTest.DataKeys.Count; i++)
{
Response.Write("gvwTest.DataKeys[" + i.ToString() + "]:" +
gvwTest.DataKeys[i].Value + "<br />");
}
}
}
}
Response.Write("---------------------- in gvwTest_RowCreated -------------------------<br />");
}//When it come out here,the e.Row.Cells[0].Text becomes a real ID. so I wonder some event has been fired
//before it come to the RowDataBound
protected void gvwTest_RowDataBound(object sender, GridViewRowEventArgs e)
{
Response.Write("********************** in gvwTest_RowDataBound ***********************<br />");
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells != null )
{
e.Row.Attributes.Add("onClick", "alert('Selected value: " + e.Row.Cells[0].Text + "')");
//Got a really articleID
DataRowView dataRowView = (DataRowView)e.Row.DataItem;
if(dataRowView !=null)
{
for (int i = 0; i < dataRowView.Row.ItemArray.Length; i++)
{
Response.Write("DataRowView.Row.ItemArray[" + i.ToString() + "]:" +
dataRowView.Row.ItemArray[i]+"<br />");
}
}
if (gvwTest.DataKeys != null && gvwTest.DataKeys.Count > 0)
{
for (int i = 0; i < gvwTest.DataKeys.Count;i++)
{
Response.Write("gvwTest.DataKeys[" + i.ToString() + "]:" +
gvwTest.DataKeys[i].Value + "<br />");
}
}
}
}
Response.Write("********************** in gvwTest_RowDataBound ***********************<br />");
}
}
Code:
The outcome result is :
---------------------- in gvwTest_RowCreated -------------------------
---------------------- in gvwTest_RowCreated -------------------------
********************** in gvwTest_RowDataBound ***********************
********************** in gvwTest_RowDataBound ***********************
---------------------- in gvwTest_RowCreated -------------------------
DataRowView.Row.ItemArray[0]:8
DataRowView.Row.ItemArray[1]:marco
DataRowView.Row.ItemArray[2]:Top 10 of brewing methods
gvwTest.DataKeys[0]:8
---------------------- in gvwTest_RowCreated -------------------------
********************** in gvwTest_RowDataBound ***********************
DataRowView.Row.ItemArray[0]:8
DataRowView.Row.ItemArray[1]:marco
DataRowView.Row.ItemArray[2]:Top 10 of brewing methods
gvwTest.DataKeys[0]:8
********************** in gvwTest_RowDataBound ***********************
---------------------- in gvwTest_RowCreated -------------------------
DataRowView.Row.ItemArray[0]:9
DataRowView.Row.ItemArray[1]:marco
DataRowView.Row.ItemArray[2]:Drink & Food contest
gvwTest.DataKeys[0]:8
gvwTest.DataKeys[1]:9
---------------------- in gvwTest_RowCreated -------------------------
********************** in gvwTest_RowDataBound ***********************
DataRowView.Row.ItemArray[0]:9
DataRowView.Row.ItemArray[1]:marco
DataRowView.Row.ItemArray[2]:Drink & Food contest
gvwTest.DataKeys[0]:8
gvwTest.DataKeys[1]:9
********************** in gvwTest_RowDataBound ***********************
---------------------- in gvwTest_RowCreated -------------------------
DataRowView.Row.ItemArray[0]:10
DataRowView.Row.ItemArray[1]:marco
DataRowView.Row.ItemArray[2]:New skin online
gvwTest.DataKeys[0]:8
gvwTest.DataKeys[1]:9
gvwTest.DataKeys[2]:10
---------------------- in gvwTest_RowCreated -------------------------
********************** in gvwTest_RowDataBound ***********************
DataRowView.Row.ItemArray[0]:10
DataRowView.Row.ItemArray[1]:marco
DataRowView.Row.ItemArray[2]:New skin online
gvwTest.DataKeys[0]:8
gvwTest.DataKeys[1]:9
gvwTest.DataKeys[2]:10
********************** in gvwTest_RowDataBound ***********************
---------------------- in gvwTest_RowCreated -------------------------
---------------------- in gvwTest_RowCreated -------------------------
********************** in gvwTest_RowDataBound ***********************
********************** in gvwTest_RowDataBound ***********************
ArticleID AddedBy Title
8 marco Top 10 of brewing methods
9 marco Drink & Food contest
10 marco New skin online
my conclusion is :
The RowCreated event fires before the RowDataBound event.And in both event ,the binding data is accessable via the GridViewRowEventArgs e,using the e.Row.DataItem to get the data record for the current row(Business object for ObjectDataSource,and DataRowView for SqlDataSource if use DataSet or DbDataRecord if use DataReader) .But in the RowCreated event,the DataItem hasn't been bound to the current row being created.so we can not access the binding data by using e.Row.Cells.
But after this event,the GridViewRow actually binding to the DataItme before it comes to the RowDataBound(This is my own guess,and I cound not find this event in the Event Property window:( ).So in the RowDataBound event , we can access the binding data via both way:the e.Row.DataItem or the e.Row.Cells .
But I still wonder why the DataKeys collection is accessable in the RowCreated event since it hasn't bound the data??
Thanks again.
------------------------------------------------------------------------
We learn from the history that we do not learn from the history
|
|

August 12th, 2007, 04:17 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Haha, yeah, exactly. I think the official saying is:
"If you feed a man a fish, you feed him for a day.
If you teach a man to fish, you feed him for life..."
Anyway, I am not so surprised the data keys are available. After all, the RowCreated event is triggered to create a row that matches the data. So, I think that ASP.NET adds the key of the DataItem to the DataKeys collection before it fires this event, so you have it at your disposal in RowCreated and RowDatabound.
Why do you think this is odd?
Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
|
|

August 12th, 2007, 09:36 AM
|
|
Authorized User
|
|
Join Date: Aug 2007
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hehe,I translate the old saying into English by myself,so it's a little strange.Anyway,it express my appreciation.:)
I hope someday I come here to post to help,not to ask for help,like you.Hope this day will not too far away.
I still not quite understand why the DataKeys is fill before the Row is DataBound.Why this is needed.
and I find out that the DataKeys is fill row by row for the first time.
And I also find out something interesting:that is when I click a button, something interesting happen:
The RowDataBound is not fired,and in the RowCreated event ,the DataRowView dataRowView=e.Row.DataItem as DataRowView
is null !! and the GridView's DataKeys.Count is 3 not like the first time increase from 1 to 3 by 1 !!
The outcome result is as follow:
[code]
---------------------- in gvwTest_RowCreated -------------------------
---------------------- in gvwTest_RowCreated -------------------------
---------------------- in gvwTest_RowCreated -------------------------
gvwTest.DataKeys[0]:8
gvwTest.DataKeys[1]:9
gvwTest.DataKeys[2]:10
---------------------- in gvwTest_RowCreated -------------------------
---------------------- in gvwTest_RowCreated -------------------------
gvwTest.DataKeys[0]:8
gvwTest.DataKeys[1]:9
gvwTest.DataKeys[2]:10
---------------------- in gvwTest_RowCreated -------------------------
---------------------- in gvwTest_RowCreated -------------------------
gvwTest.DataKeys[0]:8
gvwTest.DataKeys[1]:9
gvwTest.DataKeys[2]:10
---------------------- in gvwTest_RowCreated -------------------------
---------------------- in gvwTest_RowCreated -------------------------
---------------------- in gvwTest_RowCreated -------------------------
ArticleID AddedBy Title
8 marco Top 10 of brewing methods
9 marco Drink & Food contest
10 marco New skin online
I suppose it was because the after the first time , which is postback,the GridView load the data from
ViewState rather than binding the data again.Is this means that we can not access the Bounding Data when postback?
------------------------------------------------------------------------
We learn from the history that we do not learn from the history
|
|

August 12th, 2007, 09:46 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
The way the GridView works is by looping through the data source which is an IEnumerable. This means that for each item in the data source, it creates a row. As soon as it retrieves the first item, it can already access its unique ID. So why shouldn't the DataKey be available at this point? On each iteration, the GridView adds the relevant key to the DataKeys collection. The GridView knows what DataItem it is going to bind to, it knows its ID so why shouldn't it expose that key already so you can use it in the relevant events? The sooner you have access to data like this, the better.
And you're right; RowDataBound is not fired because the data is reconstructed from ViewState. This also means that DataItem is null.
Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
|
|

August 12th, 2007, 10:13 AM
|
|
Authorized User
|
|
Join Date: Aug 2007
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks for your quick reply.
Is it means that GridView bind data for the first time (not post back) something like this :
Code:
foreach( DataItemType dataItem in datasource)
{
Greate a DataRow for the GridView ,and fill the DataKeys collections for this row.
//contruct a GridViewRowEventArgs
GridViewRowEventArgs e=new GridViewRowEventArgs(..);
e.Row.DataItem=datatItem;
...
trigger the RowCreated event by passing the GridViewRowEventArgs e,
so that at the RowCreated event the e.Row.DataItem is accessable;
trigger the RowDataBound ;
}
and for the postback request,it just load every thing from the ViewState,include the DataKeys?
------------------------------------------------------------------------
We learn from the history that we do not learn from the history
|
|
 |