Never is postback ?

C

Craig Kenisston

Hi,

I'm writing a little asp site, with just a single main default.aspx page and
a couple of web user's control.
I have the page in a way, that I have a left hand menu, and alternatively,
according to the selection, any of the .ascx components is loaded into a
tables's cell with the proper content.
This works great except for one thing : The menu on the left is a large list
of items retrieved from a database, in a datalist with an hypelink . Each
time any of these items is clicked, the query to retrieve this list is hit
again.
I have something like this :

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if (Page.IsPostBack == false)
{

DataList1.DataSource = get data reader; // SQL activity here
DataList1.DataBind();

if (Request.Params["goto"] != null)
{
string sGoto;
sGoto = Request.Params["goto"];
CenterPane.Controls.Add(Page.LoadControl("~/ResourcesList.ascx"));
CenterPane.Visible = true;
}
}

I thought that with the IsPostBack = false, I would avoid this code being
called in each click. I think that was wrong.

I'm looking advise on how to codify this in order to load the left hand menu
only once per user's session.


Thanks in advance,
 
C

cbDevelopment

Sounds to me like your hyperlinks you are generating in your datalist are
really hyperlinks. This would cause a fresh request to the page and
would reload your controls.

Solution:

Convert your hyperlinks to linkbuttons and handle the itemcommand event
of the datalist. The LinkButton performs a postback. Your list probably
had the ID value of whatever was chosen in the list passed on the
querystring. You can duplicate that functionality by using the
CommandArgument property of the LinkButton.

The code would look something like: (Sorry, I'm a VB guy, so you'll have
to do some rough translation)

private sub PopulateLinkbuttons(s, e) handles datalist1.itemdatabound
' This gets the linkbutton object and populates
' its command argument property.
' This runs for each row in your datalist
' only use item and altitem. header and foots and pagers
' don't have the linkbutton control in them.
' The link button is in your itemtemplate.
if e.item.itemtype=item or e.item.itemtype=alternatingitem then
dim l as linkbutton
l=e.item.findcontrol("TheLinkbutton")
l.commandname="LoadControl"
' You called this parameter "goto" below
l.commandargument=e.item.dataitem("goto")
end if
end sub

private sub LoadControl(s,e) handles datalist1.itemcommnd
if e.commandname="LoadControl" then
' Do what you do when you load the control
' Grab what you use "goto" from the commandargument
end if
end sub

You can see that you can have lots of link buttons each with their own
command name, and your itemcomand handler sub can do a case statement to
determine which button was clicked and which parameter was sent.

Best of luck!


Hi,

I'm writing a little asp site, with just a single main default.aspx
page and a couple of web user's control.
I have the page in a way, that I have a left hand menu, and
alternatively, according to the selection, any of the .ascx components
is loaded into a tables's cell with the proper content.
This works great except for one thing : The menu on the left is a
large list of items retrieved from a database, in a datalist with an
hypelink . Each time any of these items is clicked, the query to
retrieve this list is hit again.
I have something like this :

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if (Page.IsPostBack == false)
{

DataList1.DataSource = get data reader; // SQL activity here
DataList1.DataBind();

if (Request.Params["goto"] != null)
{
string sGoto;
sGoto = Request.Params["goto"];
CenterPane.Controls.Add(Page.LoadControl("~/ResourcesList.ascx"));
CenterPane.Visible = true;
}
}

I thought that with the IsPostBack = false, I would avoid this code
being called in each click. I think that was wrong.

I'm looking advise on how to codify this in order to load the left
hand menu only once per user's session.


Thanks in advance,
 

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