Postback question

T

Terry Holland

I have an asp page that contains a user control. This control is a panel
containing a number of link buttons that get displayed if certain conditions
in the db are met and these conditions can take quite a while to evaluate.
Consequently that page can take a while to open. These links are alert for
users indicating that they need to perform some actions. If they click any
of the links they will get redirected to the appropriate page.
Unfortunately, the action of clickin the link causes a postback which in
turn build the control which in turn checks the conditions which is a slow
process... All of this when all the user really wants to do is navigate to
a new page. Is there any way of avoiding all of this processing and just
drop straight into the event that caused the postback?
I have tried checking the IsPostback property on the Page_Load event and not
building the control if it is, but this does not work as there are no link
buttons to respond to!!

I have the same problem in other areas of the application where I have other
dynamically created controls (mainly datagrids). These grids generally have
a column containing an Edit button which, when clicked, causes a postback,
rebuilds the grid and populates the grid before it is able to detect which
rows Edit button was clicked and then redirect to another page. Again, if I
dont recreate the grid and populate at runtime, nothing happens in terms of
redirecting to required page.

Help appreciated

Terry Holland
 
J

Jose Rodriguez

Terry, this is a tough one, for starters, the lifecycle can not be
shortcircuited so it is going to go to the page_load to the control_load and
then and only then to your event.
IsPostBack will not do it, because it will always be a postback relative to
your control. We had this issue on a project,doing a google search we
found the following:
(it uses a property to determine whether the control has been loaded for the
first time or not).HTH - Jose
On the user control code-behind add this property:





Private Property IsFirstLoad() As Boolean



Get



Dim o As Object = ViewState("MyUC-FirstLoad")



Return (o = Nothing)



End Get



Set(ByVal Value As Boolean)



ViewState("MyUC-FirstLoad") = True



End Set



End Property



Then add the code on your user control Page_Load:



Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load



If IsFirstLoad = True Then



' Do Something



Else

' Whatever.

End If



IsFirstLoad = True



End Sub
 
S

Steven Cheng[MSFT]

Thanks for Jose's input.

Hi Terry,

As Jose has mentioned, ASP.NET page has fixed server-side processing model,
each request(no matter postback or not) will go through all the pipeline
and events. I think you're currently dynamically query the database and
constructing the controls in Page's Init or Load event, correct? IMO, if
you want to avoid the additional overhead when the user will click the
certain link button (dynamically created?), you can consider the following
options:

1. Make the redirection completely occur at client-side. That means do not
postback the page and user hyperlink instead of linkbutton.

2. Still postback, however, we no longer use the LinkButton's Click event
to do the redirection(or other server-side task) because click event of the
Linkbutton require that LinkButton be created again and added into Page's
control collection(this is not possible for your scenario since you do not
want to involve the addtional evaludation and control construction).
Instead, we can put a html input hidden field on the page. And for our
linkbuttons, we can register some client-side onclick script for them,
these script will set the sufficient information in the hidden field.
Then, when the page is postback (because of one of the linkbutton get
clicked), we can programmtically check that hidden field's value(directly
through Request.Forms Collection) and determine what operation to do or
where to redirect , all this is done before the evaluation and control
constructing code. How do you think?

Hope this helps.

Regards,

Steven Cheng
Microsoft Online Community Support


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

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

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


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
T

Terry Holland

I have overcome my linkbutton problem by modifying the stored procedure and
it now runs much faster.

I am however interested in using your suggestion for my dynamically created
datagrids. Could you prove some example code to get this to work please.

As previously mentioned, I dynamically add custom columns to my datagrid.
If the grid requires an Edit column then I call AddEditButton in my
InitialiseControl routine. This adds a clsImageButtonColumn column to my
grid with "Edit" as the command name.
If this button is clicked then DataGrid1_ItemCommand event is fired and
e.CommandName will be "Edit" and I fire my Datagrid's custom Edit event.

Im not sure where I would put the client side script that you are
suggesting.

Terry Holland

'=================================================
'Custom DataGrid Control - Add Edit Button To Grid
'=================================================
Private Sub AddEditButton()
Dim objImageInfo As clsImage_ROC.clsImageInfo =
m_objImage_ROC("List_Edit")
Dim objEC As New clsImageButtonColumn("Edit", objImageInfo)
'"../images/Edit.ICO")

With objEC
.HeaderStyle.Width = Unit.Pixel(25)
.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
End With
DataGrid1.Columns.Add(objEC)
End Sub

'=================================================
'Custom DataGrid Control - Edit event
'=================================================
Private Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
DataGrid1.ItemCommand
Dim intID As Integer = CType(e.Item.FindControl("lblID"),
Label).Text

Select Case e.CommandName
Case "Edit"
RaiseEvent Edit(intID)
Case "Delete"
RaiseEvent Delete(intID)
End Select
End Sub

'=================================================
'clsImageButtonColumn
'=================================================
Public Class clsImageButtonColumn
'Inherits System.Web.UI.UserControl

Inherits System.Web.UI.WebControls.DataGridColumn

Private m_strCommandName As String
'Private m_strImageURL As String
Private m_objImageInfo As clsImage_ROC.clsImageInfo

Private Sub New()

End Sub

Public Sub New(ByVal CommandName As String, ByVal objImageInfo As
clsImage_ROC.clsImageInfo)
m_strCommandName = CommandName
m_objImageInfo = objImageInfo

End Sub

Public Overrides Sub InitializeCell(ByVal cell As TableCell, ByVal
columnIndex As Integer, ByVal itemType As ListItemType)
MyBase.InitializeCell(cell, columnIndex, itemType)
If ((itemType <> ListItemType.Header) And (itemType <>
ListItemType.Footer)) Then

Dim ctl As WebControl = Nothing
Dim ibt As ImageButton = New
System.Web.UI.WebControls.ImageButton

ibt.ImageUrl = m_objImageInfo.Url ' m_strImageURL
ibt.AlternateText = m_objImageInfo.AltText
ibt.CommandName = m_strCommandName
ctl = ibt
cell.HorizontalAlign = HorizontalAlign.Center
cell.Controls.Add(ctl)
End If
End Sub
End Class
 
S

Steven Cheng[MSFT]

Thanks for your response Terry,

Do you mean you want to register some client-script for your custom "edit"
column so that when the user clicks it, the column will use client-script
to do the redirection rather than let it postback? Or if I misunderstand,
would you provide some further description on this? Anyway, in your
scenario, if you want to add client script for your custom column, I
recommend you use GridView/DataGrid's ItemDataBound event which is fired
for each row's databinding, and you can get the the certain inner control
from each row and do some customization on them (such as registering client
script). e.g:

===========================
private void DataGrid1_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
Button btn = e.Item.FindControl("MyButtonid") as Button;
// do the customization on the button here....
}
}
=================================

Hope this helps.

Regards,

Steven Cheng
Microsoft Online Community Support


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

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

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


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
S

Steven Cheng[MSFT]

You're welcome :)

Good luck!

Regards,

Steven Cheng
Microsoft Online Community Support


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

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

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


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

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

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top