One for the Experts

G

Guest

Hello all,

I have a sticky situation that needs an expert solution. Please help!

My ASP.NET page has a datagrid which is bound to a dataset. No surprise
there. As the data is bound and rendered, I add a button to the cell in one
of the columns at run-time in the ItemDataBoundEvent. The button is only
added for rows that meet a specific rule condition. For example, suppose
the rows show books. I only add the "buy" button for books that are in
stock.

When a user clicks the dynamically created "buy" button, a postback occurs.
At that point, I have two options to retrieve the dataset and rebind.

Optiona 1 - I can go to the database again and retreive a fresh set of data.
In this case, if the book associated with the button is no longer in stock,
the button never gets created and the click event never fires. I understand
why this happens but I'd like to at least be able to tell the user "the book
has gone out of stock, please call for availability". Is there a way to get
the object that triggered the postback? If so, I wouldn't have to do option
2 which is...

Option 2 - Store the dataset in the users session and rebind to that on the
postback. This ensures that the postback grid gets drawn exactly as the
user saw it and that the clicked button is rendered so I can receive the
event. In the business rule processing for the event, I check to make sure
the book is in stock and show an error message if it is not. I also refresh
the grid with the latest set of data.

Option 2 is working great right now, but it means I have to put a dataset in
the Session object for each user that hits the page. Ugggghhh.

If you have a suggestion on how to better implement this, please share.

Thanks!

Scott Mitchell
(e-mail address removed)
 
D

Damon Payne

Hi,
If you want to know what control caused the postback event there are a
couple of options. You can draw a custom control that implements
IPostBackHandler and use the built in methods to render postback javascript
and process postback data; OR
maybe check the __EVENTTARGET item of the form data and try to figure out
which one of your buttons this name corresponds to.
 
J

Jos

Hello all,

I have a sticky situation that needs an expert solution. Please help!

My ASP.NET page has a datagrid which is bound to a dataset. No
surprise there. As the data is bound and rendered, I add a button to
the cell in one of the columns at run-time in the ItemDataBoundEvent.
The button is only added for rows that meet a specific rule
condition. For example, suppose the rows show books. I only add the
"buy" button for books that are in stock.

When a user clicks the dynamically created "buy" button, a postback
occurs. At that point, I have two options to retrieve the dataset and
rebind.

Optiona 1 - I can go to the database again and retreive a fresh set
of data. In this case, if the book associated with the button is no
longer in stock, the button never gets created and the click event
never fires. I understand why this happens but I'd like to at least
be able to tell the user "the book has gone out of stock, please call
for availability". Is there a way to get the object that triggered
the postback? If so, I wouldn't have to do option 2 which is...

Option 2 - Store the dataset in the users session and rebind to that
on the postback. This ensures that the postback grid gets drawn
exactly as the user saw it and that the clicked button is rendered so
I can receive the event. In the business rule processing for the
event, I check to make sure the book is in stock and show an error
message if it is not. I also refresh the grid with the latest set of
data.

Option 2 is working great right now, but it means I have to put a
dataset in the Session object for each user that hits the page.
Ugggghhh.

I have an Option 3.
I guess you are creating your buttons dynamically in OnItemDataBound.
Instead, I suggest adding them to the template (statically).
You only have change their size to 1x1 pixel in OnItemDataBound
for the ones you don't want the user to see.
This ensures that your postback will always work.

If a user inadvertently clicks your 1x1 pixel button, he will get
an "out of stock" error, which is ok.
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top