Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Datagrid Control > Datagrid postback issue

Reply
Thread Tools

Datagrid postback issue

 
 
RRB
Guest
Posts: n/a
 
      01-17-2005
Hello,

I have a postback issue concerning the Datagrid. I posted this problem
to several forums before and didnít receive a single response from
anyone. I initially thought my issue was related to sorting but now
understand it as a larger issue. Here goes:

I have a Datagrid that for various business reasons has the following
relevant characteristics:

- It does not keep viewstate
- It has template columns, manually generated
- it supports in-place editing (which cells are editable varies by
row-type).
- it supports column sorting by clickable column headers.

It is in implementing the column-sorting that I ran into trouble. From
what I have determined, once you databind the datagrid upon postback,
the datagrid becomes immutable from postback event handlers.

The normal flow is as follows:

Upon postback I manually add my columns, then set my grid datasource to
a session-cached DataView. Next, I call DataBind() on my grid.

Next, the column headerís click event fires and in the event handler, I
determine the sort order and set the sort property of my dataview.
Finally, I call DataBind() on the grid again.

Result: The grid displays as if I had not clicked a column header. And
if I click another column header, I will then see the grid sorted by the
column Iíd previously clicked. The apparent behavior is that that the
grid is ignoring the second databind, as no exception is being thrown.

===========================

Test 1:

1. Upon postback, I manually add my columns, then set my grid datasource
to a session-cached DataView. Next, I call DataBind() on my grid.

1A. Next, I set the grid datasource to a simple string array, and call
DataBind() on the grid again.

2. Next, the column headerís click event fires and in the event handler,
I determine the sort order and set the sort property of my dataview.
Finally, I call DataBind() on the grid again.

Result: The grid displays empty rows (once for each string in my string
array) since my data didnít match my columns. Thus my second databind
was successful.

===========================

Test 2:

1. Upon postback, I manually add my columns, then set my grid datasource
to a session-cached DataView. Next, I call DataBind() on my grid.

2. Next, the column headerís click event fires.

2A. In the event handler, I set the grid datasource to a simple string
array, and call DataBind() on the grid again. All other code is
commented out.

Result: The grid displays as if I had not clicked a column header. And
if I click another column header, I will then see the grid sorted by the
column Iíd previously clicked. My second databind was NOT successful.

===========================

All I have changed between Test 1 and Test 2 is where I do the second
databind. In Test 1, I perform it in Page_Load before the click event is
fired. In Test 2, I perform it afterward in the click event handler.

Any insight on this issue would be of immense help! Thank you in
advance.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
 
 
 
Scott M.
Guest
Posts: n/a
 
      01-18-2005
You should not be databinding your grid in the Page_Load postback section
only in the Not Postback section of Page_Load. On all postbacks, you should
be calling databind from the event handler that caused the postback (so
databinding should be the last thing you do in your SortCommand event
handler as well as your UpdateCommand, EditCommand, PageIndexChanged, etc.

Also, since you have viewstate turned off for the grid, you must realize
that you will have to persist the state information of the grid yourself
(this is not just the data that the grid was holding, but the current sort
criteria, the selected item, the current page index, etc.


"RRB" <(E-Mail Removed)> wrote in message
news:uB5A1wN$(E-Mail Removed)...
> Hello,
>
> I have a postback issue concerning the Datagrid. I posted this problem
> to several forums before and didn't receive a single response from
> anyone. I initially thought my issue was related to sorting but now
> understand it as a larger issue. Here goes:
>
> I have a Datagrid that for various business reasons has the following
> relevant characteristics:
>
> - It does not keep viewstate
> - It has template columns, manually generated
> - it supports in-place editing (which cells are editable varies by
> row-type).
> - it supports column sorting by clickable column headers.
>
> It is in implementing the column-sorting that I ran into trouble. From
> what I have determined, once you databind the datagrid upon postback,
> the datagrid becomes immutable from postback event handlers.
>
> The normal flow is as follows:
>
> Upon postback I manually add my columns, then set my grid datasource to
> a session-cached DataView. Next, I call DataBind() on my grid.
>
> Next, the column header's click event fires and in the event handler, I
> determine the sort order and set the sort property of my dataview.
> Finally, I call DataBind() on the grid again.
>
> Result: The grid displays as if I had not clicked a column header. And
> if I click another column header, I will then see the grid sorted by the
> column I'd previously clicked. The apparent behavior is that that the
> grid is ignoring the second databind, as no exception is being thrown.
>
> ===========================
>
> Test 1:
>
> 1. Upon postback, I manually add my columns, then set my grid datasource
> to a session-cached DataView. Next, I call DataBind() on my grid.
>
> 1A. Next, I set the grid datasource to a simple string array, and call
> DataBind() on the grid again.
>
> 2. Next, the column header's click event fires and in the event handler,
> I determine the sort order and set the sort property of my dataview.
> Finally, I call DataBind() on the grid again.
>
> Result: The grid displays empty rows (once for each string in my string
> array) since my data didn't match my columns. Thus my second databind
> was successful.
>
> ===========================
>
> Test 2:
>
> 1. Upon postback, I manually add my columns, then set my grid datasource
> to a session-cached DataView. Next, I call DataBind() on my grid.
>
> 2. Next, the column header's click event fires.
>
> 2A. In the event handler, I set the grid datasource to a simple string
> array, and call DataBind() on the grid again. All other code is
> commented out.
>
> Result: The grid displays as if I had not clicked a column header. And
> if I click another column header, I will then see the grid sorted by the
> column I'd previously clicked. My second databind was NOT successful.
>
> ===========================
>
> All I have changed between Test 1 and Test 2 is where I do the second
> databind. In Test 1, I perform it in Page_Load before the click event is
> fired. In Test 2, I perform it afterward in the click event handler.
>
> Any insight on this issue would be of immense help! Thank you in
> advance.
>
> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it!



 
Reply With Quote
 
 
 
 
RRB
Guest
Posts: n/a
 
      01-18-2005
Hi Scott, and thanks for your reply.

Unfortunately, because I am not maintaining Viewstate for the datagrid,
the DataBind() call in the Page_Load() method upon postback is necessary
in order for the events to fire. I'll post relevant documentation on
that to the thread.

I'm currently maintaining the EditItem index property but nothing else,
so I'll take a look down that avenue. Again, thank you.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
Scott M.
Guest
Posts: n/a
 
      01-18-2005
I wouldn't handle it that way. As I stated, with ViewState turned off, you
have to persist the EditItem, SortCriteria, PageIndex, etc. Since you are
tracking these values anyway, you can simply compare the old value against
the new value and call the appropriate event handler yourself. This would
allow you to NOT call DataBind in the Page_Load postback section and instead
call it in the event handler as it should be.


"RRB" <(E-Mail Removed)> wrote in message
news:%23lK8O3Q$(E-Mail Removed)...
> Hi Scott, and thanks for your reply.
>
> Unfortunately, because I am not maintaining Viewstate for the datagrid,
> the DataBind() call in the Page_Load() method upon postback is necessary
> in order for the events to fire. I'll post relevant documentation on
> that to the thread.
>
> I'm currently maintaining the EditItem index property but nothing else,
> so I'll take a look down that avenue. Again, thank you.
>
> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it!



 
Reply With Quote
 
RRB
Guest
Posts: n/a
 
      01-18-2005
Scott,
fair enough. I should say here that I'm new to ASP.Net so I'm certain
I'm doing several things the hard way. Your approach is attractive to me
because it means I'd also halve my processing on the back-end. Articles
I'd come acriss had led me to believe there was no other way to properly
fire the event.

Ok, so now I have a question:

Currently, my event handler is being passed a reference to the column
button that was clicked (ie the sender object). If I bypass the
binding, how would I determine the column clicked? I looked in the
Request object but didn't see anything beyond my having to sleuth
through the parameter name value collection.

Thanks for your help on this, by the way.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
Scott M.
Guest
Posts: n/a
 
      01-18-2005
There are several ways to make this work. The simplest is to just turn
ViewState on for the grid, but I'm assuming your data either changes from
minute to minute or you are persisting the data yourself in some other way.

To know which column was clicked (I assume here you are talking about
Select, Delete, Edit, Update, Cancel), you need to test for the event
differently than normal controls. You could follow a sample I have below:

Add the following code to your code-behind (this assumes the checkbox is
named "chkDynamic"):

Protected Sub chkDynamic_CheckedChanged(ByVal Sender As Object, ByVal e As
System.EventArgs)
'This sub will be run for every row that has had its value changed
Dim chk As CheckBox = CType(Sender, CheckBox)
Dim item As DataGridItem = CType(chk.NamingContainer, DataGridItem)
' "item" is now a reference to the DataGrid selected row
If chk.Checked Then item.BackColor = Color.Gray
'From here, you could grab some data from this row with:
item.FindControl("ControlName")
'and then you would know which record was selected.
End Sub
Private Sub dg_ItemCreated(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles dg.ItemCreated
'This sub runs anytime the DataGrid needs to dynamically create a cotrol
'but we only are interested in rows that may have controls (not header,
footer or pager rows)
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
ListItemType.AlternatingItem Then
Dim chk As CheckBox = CType(e.Item.FindControl("chkDynamic"),
CheckBox)
AddHandler chk.CheckedChanged, AddressOf chkDynamic_CheckedChanged
'This adds a client-side JavaScript event handler.
'You must also have added a client-side JavaScript function called
chkShow(sender)
chk.Attributes.Add("onClick", "chkShow(this," & rowNum.ToString &
")")
End If
End Sub



"RRB" <(E-Mail Removed)> wrote in message
news:%2356ahcX$(E-Mail Removed)...
> Scott,
> fair enough. I should say here that I'm new to ASP.Net so I'm certain
> I'm doing several things the hard way. Your approach is attractive to me
> because it means I'd also halve my processing on the back-end. Articles
> I'd come acriss had led me to believe there was no other way to properly
> fire the event.
>
> Ok, so now I have a question:
>
> Currently, my event handler is being passed a reference to the column
> button that was clicked (ie the sender object). If I bypass the
> binding, how would I determine the column clicked? I looked in the
> Request object but didn't see anything beyond my having to sleuth
> through the parameter name value collection.
>
> Thanks for your help on this, by the way.
>
> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it!



 
Reply With Quote
 
RRB
Guest
Posts: n/a
 
      01-19-2005
Hi Scott,
Thanks for your helpful replies. I didn't go with your solution but you
pointed me in the right direction and I'm now no longer reliant on
binding my stateless datagrid upon postback.

In a nutshell, I'm setting the eventtarget and eventargument fields on a
button click, then testing for the specific value on postback and
calling my sort method, which sets the sort, then binds the grid.

Works like a charm!

Thanks again.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
Scott M.
Guest
Posts: n/a
 
      01-19-2005
HTH

"RRB" <(E-Mail Removed)> wrote in message
news:%232MG3Hm$(E-Mail Removed)...
> Hi Scott,
> Thanks for your helpful replies. I didn't go with your solution but you
> pointed me in the right direction and I'm now no longer reliant on
> binding my stateless datagrid upon postback.
>
> In a nutshell, I'm setting the eventtarget and eventargument fields on a
> button click, then testing for the specific value on postback and
> calling my sort method, which sets the sort, then binds the grid.
>
> Works like a charm!
>
> Thanks again.
>
> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it!



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Datagrid Javascript Postback issue John Doe ASP .Net Datagrid Control 1 01-08-2007 09:01 PM
ShowDialogModal postback on close (this is not related to multiple dialogs opening upon postback) Jacky Chau ASP .Net 2 06-08-2005 05:49 PM
Can I raise a postback ? Session variable only visible after postback. Please help gce ASP .Net 2 05-08-2005 05:55 AM
Postback not being set to True on first postback =?Utf-8?B?Q2hhZA==?= ASP .Net 2 04-04-2005 06:17 PM
effecting postback without auto postback controls Psych971 ASP .Net 5 12-17-2004 08:53 PM



Advertisments