From: Harlan Messinger on
Mr. Arnold wrote:
> Harlan Messinger wrote:
>> Harlan Messinger wrote:
>>> In ASP.NET 3.5: Suppose I have a ListView whose data source (using
>>> LINQ-to-SQL) is set with an expression for a DataSource instead of a
>>> control assigned to the ListView's DataSourceId field:
>>>
>>> DataSource='<%#((IEnumerable<Bid>) Eval("Bids")).OrderBy(bid =>
>>> bid.BidDateTime)%>'
>>>
>>> This ListView is nested inside another one that shows information for
>>> an Item in my Item table, and exposes Item.Bids, the related rows
>>> from the Bid table. (This is for an auction.)
>>>
>>> Since I'm not using a LinqDataSource for this ListView, I don't have
>>> any built-in functionality for insertion, updating, deletion, and I
>>> can't seem to find out just how to do that. Any guidance, please?
>>
>> I've tried this, where a user has entered a bid:
>>
>> ListView lv = (ListView)sender;
>> decimal bidPrice =
>> decimal.Parse(((TextBox)lv.InsertItem.FindControl("txtBidCount")).Text);
>>
>> //Insert validation code here.
>>
>> Bid bid = new Bid();
>> bid.ItemId = ???;
>> bid.ParticipantId = Session["userParticipantId"];
>> bid.BidPrice = bidPrice;
>> bid.BidDateTime = DateTime.Now;
>> bid.IsOnlineBid = true;
>>
>> AuctionData data = new AuctionData();
>> data.Bids.InsertOnSubmit(bid);
>> data.SubmitChanges();
>>
>>
>> But I don't know what my source should be for the itemId with which
>> this Bid should be associated. It's the same as the ItemId of the
>> current item in the outer ListView, but I don't know how to get at
>> that from within the inner ListView. I thought it might have something
>> to do with inspecting lv.DataSource, but that actually seems to be
>> null at this point, according to the Locals window.
>
> Wouldn't just use a asp:HiddenField that's global to the page? The
> outter Listview would save itemid to the HF, and then Listview inner
> would get the value from the HF.

There's a thought. Even with the experience I *have* had with ASP.NET, I
keep forgetting that the entire page is a single form and all the
controls are available. On the other hand, there are the usual security
provisos persisting data that way--vulnerable to monkeying around on the
client side.
>
> And I don't see how you're going to use a datasource that belongs to
> another control.

As it happens, I've solved that problem:

ListView lv = (ListView)sender;
int itemId =
(int) ((ListView)lv.NamingContainer.NamingContainer)
.SelectedPersistedDataKey.Value;

The first NamingContainer call gets me the placeholder from the
LayoutTemplate; the second gets me the parent ListView.
>
> The ObjectContext is there for the Linq-2-SQL from a codebehind file
> standpoint, which you may have to go to the codebehind file and manually
> do the CURD for the other control.
>
OK, I'll take a look at that. I hadn't come across ObjectContext.

So, actually, the above solved my problem, and I'm left with just one
more. After extracting and validating values, I create a new Bid object
containing those values, and I insert it into the table:

data.Bids.InsertOnSubmit(bid);
data.SubmitChanges();

But then, what ought to happen next is that the page should be redrawn,
with the newly added row appearing under the previous rows in my
ListView, and I'd like to see a message, "your bid has been placed". Or,
if there's an error (because nonnumeric characters were entered for the
amount bid, or the amount bid is lower than the previous bid), I'd also
like the page to redisplay as is but with an error message. What
actually happens, upon correct entry, is that the page is redrawn
looking as it was before I pressed the Insert button. But when I then
GET the page again, my new bid is displayed as an existing row along
with the previous ones. I'm guessing I need to create an OnItemInserted
handler that redirects, but then I'm not sure how to handle the success
or error message.