Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 15th, 2007, 10:10 PM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 238
Thanks: 0
Thanked 2 Times in 2 Posts
Send a message via MSN to rsearing
Default

Actually--it didn't..not only did it work, but it used the value and not the one from the newvalues I added. NOW..a couple things I did since pasting

a) I noticed that I don't actually have a binding "title" in here which makes me wonder how the title was actually updating. If you look at the line I added in the code behind--like you suggested..it didn't actually change the title to new value-it actually pulled the value from the edit field.

b) Now, when I did change to a field that is in the page (URL) it actually did assign the value "New Value" to URL--but that contradicts what you said in the reply above that it wouldn't allow to insert a duplicate "newvalue"



Here is my page:

<%@ Page Language="VB" MasterPageFile="~/Admin/AdminMasterPage.master" AutoEventWireup="false" CodeFile="editpictures.aspx.vb" Inherits="Admin_editpictures" title="Edit Picture" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">
<div style="padding-top:15px; margin-left:15px">
    <div style="font-size:1.5em; text-align:center;">
        <asp:Label ID="titleLabel" Font-Bold="true" Font-Underline="true" runat="server" ></asp:Label>
    </div>
   <div style="padding-left:200px; margin-top:10px; padding-bottom:10px">
    <asp:DetailsView CssClass="MyView" ID="DetailsView1" runat="server" Height="50px" Width="350px" AllowPaging="True" AutoGenerateRows="False" DataSourceID="ObjectDataSource1" DataKeyNames="paintingID,url" >
        <Fields>
            <asp:TemplateField SortExpression="URL" ShowHeader="False">
                <EditItemTemplate>
                    <span style="color:Blue; font-weight:bold"> Old File: </span>
                    <asp:Label ID="Label3" runat="server" Text='<%# Eval("URL") %>'></asp:Label><br />
                    <span style="color:Blue; font-weight:bold;">New File: </span>
                    <asp:FileUpload ID="FileUpload1" runat="server" />

                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("URL") %>'></asp:TextBox>
                </InsertItemTemplate>
                <ItemTemplate>
                    <asp:Image ID="Image1" BorderColor="Black" BorderStyle="Solid" BorderWidth="2px" runat="server" ImageUrl='<%# Eval("URL", "~/Paintings/{0}") %>' Width="60px" /><br />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
            <asp:BoundField DataField="Size" HeaderText="Size" SortExpression="Size" />
            <asp:TemplateField HeaderText="Ptype" SortExpression="Ptype" >
                <EditItemTemplate>
                    <asp:DropDownList ID="DropDownList1" runat="server" OnDatabound = "DropDownList1_DataBound" DataSourceID="ObjectDataSource2" DataTextField="PaintType" DataValueField="PaintTypeID" SelectedValue='<%# Bind("Ptype") %>' >
                    </asp:DropDownList>
                    <asp:ObjectDataSource ID="ObjectDataSource2" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="GetPaintingTypeList" TypeName="Searing.SunArtSite.Bll.PaintingTypeManag er"></asp:ObjectDataSource>
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:DropDownList ID="DropDownList1" runat="server" OnDatabound = "DropDownList1_DataBound" DataSourceID="ObjectDataSource2" DataTextField="PaintType" DataValueField="PaintTypeID" SelectedValue='<%# Bind("Ptype") %>' >
                    </asp:DropDownList>
                    <asp:ObjectDataSource ID="ObjectDataSource2" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="GetPaintingTypeList" TypeName="Searing.SunArtSite.Bll.PaintingTypeManag er"></asp:ObjectDataSource>
                </InsertItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("Ptype") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="Value" HeaderText="Value" SortExpression="Value" />
            <asp:TemplateField HeaderText="Comments" SortExpression="Comments">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Comments") %>' Height="200px" TextMode="MultiLine"></asp:TextBox>
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Comments") %>'></asp:TextBox>
                </InsertItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("Comments") %>' ></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:CheckBoxField DataField="PrintAvail" HeaderText="PrintAvail" SortExpression="PrintAvail" />
            <asp:CheckBoxField DataField="NotecardAvail" HeaderText="NotecardAvail" SortExpression="NotecardAvail" />
            <asp:CommandField ShowEditButton="True" ShowDeleteButton="True" />
        </Fields>

    </asp:DetailsView>

    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="GetPaintingList" TypeName="Searing.SunArtSite.Bll.PaintingManager" DataObjectTypeName="Searing.SunArtSite.BO.Painting " InsertMethod="Upsert" UpdateMethod="Upsert" DeleteMethod="DeletePainting">
        <SelectParameters>
            <asp:QueryStringParameter Name="id" QueryStringField="id" Type="Int32" />
        </SelectParameters>
    </asp:ObjectDataSource>
    <asp:Label ID="FileUploadReport" runat="server" ></asp:Label>
  </div>
</div>

</asp:Content>

Here is my code behind:

Imports System.IO

Partial Class Admin_editpictures
    Inherits System.Web.UI.Page


    Protected Sub DropDownList1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim myDropDownList As DropDownList
        myDropDownList = CType(DetailsView1.FindControl("DropDownList1"), DropDownList)
        'myDropDownList.Items.Insert(0, "Note Cards")
        'myDropDownList.Items.Insert(1, "Prints")
        ' myDropDownList.Items(0).Value = 1
        'myDropDownList.Items(1).Value = 2

    End Sub

    Protected Sub DetailsView1_ItemDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewDeleteEventAr gs) Handles DetailsView1.ItemDeleting
        Dim fileName As String = e.Keys(1).ToString
        File.Delete(MapPath("~/Paintings/") & fileName)
    End Sub

    Protected Sub DetailsView1_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewInsertEventAr gs) Handles DetailsView1.ItemInserting
        Dim myDropDownList As DropDownList
        myDropDownList = CType(DetailsView1.FindControl("DropDownList1"), DropDownList)
        e.Values.Insert(e.CommandArgument, "ptype", myDropDownList.SelectedValue)


    End Sub

    Protected Sub DetailsView1_ItemUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewUpdateEventAr gs) Handles DetailsView1.ItemUpdating

        Dim myDropDownList As DropDownList
        myDropDownList = CType(DetailsView1.FindControl("DropDownList1"), DropDownList)
        'e.NewValues.Insert(e.CommandArgument, "ptype", myDropDownList.SelectedValue)
        e.NewValues("ptype") = myDropDownList.SelectedValue
        e.NewValues.Insert(e.CommandArgument, "title", "newvalue")
        Dim myFileUpload As FileUpload
        Dim oldFileName As String = CType(DetailsView1.FindControl("Label3"), Label).Text
        myFileUpload = CType(DetailsView1.FindControl("FileUpload1"), FileUpload)
        If myFileUpload.HasFile Then
            Try
                File.Delete(MapPath("~/Paintings/") & oldFileName)
                myFileUpload.SaveAs(Server.MapPath("~/Paintings/" & myFileUpload.FileName))
                Dim url As String = myFileUpload.FileName
                If (url = String.Empty) Then
                    url = oldFileName
                End If
                e.NewValues("url") = myFileUpload.FileName

            Catch ex As Exception
                FileUploadReport.Text = "Failed because: <br/>" & ex.Message
            End Try
        End If


    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        titleLabel.Text = Request.QueryString("name")
    End Sub


End Class


 
Old August 16th, 2007, 04:07 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

It makes sense you can add a field called Title. The dictionary allows you to add new fields on the fly using unique keys. If they don't exist, they get updated.

I don't see you setting the URL field. Maybe I overlooked it as there's quite a bit of code to digest, not everything relevant to the situation.

Anyway, where you set the (non existing) title now set a break point. Then look at e.NewValues in a watch window. What do you see?

So far, I have been testing this with an SqlDataSource, for a project I am working on. Maybe it's different with an ODS? Will try later.

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
 
Old August 16th, 2007, 10:41 AM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 238
Thanks: 0
Thanked 2 Times in 2 Posts
Send a message via MSN to rsearing
Default

What I don't understand is even though title doesn't have a bound field in the page, it is a field on the detailsview. In fact, it's only the fields that required some template control (like using a drop down for the ptype field or a fileupload for the "url" field) that show the bound property. There are many other fields getting updated--such as title, size, description, etc. What confused me is that even though I declared a title in enewvalues.insert it used the value in the field of the detailsview--not the "newvalue" I put in the e.newvalues.

Secondly, URL is a field that shows up in the template of the detailsview. I did the e.newvalues.insert for URL and it actually worked--**and** it took "newvalue" rather than the value that was in the field entered by the user in the udpate.

Just weird--I would have expected it to error since URL was a bound field.

I would certainly not stress about it. At this point, I got it working--I just am trying to figure out the behavior and why it somewhat worked before--which basically means understanding how e.newvalues works.

Thanks for all your help!
Rob

 
Old August 16th, 2007, 01:03 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

It's difficult for me to say what's going on without seeing your code running.

The best way to find these kind of things out is by debugging. Set breakpoints at relevant stages in the code, and then look at things like e.NewValues. Are the keys already there? What values do you get?

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
 
Old August 17th, 2007, 05:26 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Rob,

I briefly looked at some of the source you sent me and I noticed this:
Code:
e.NewValues.Insert(e.CommandArgument, "ptype", myDropDownList.SelectedValue)
Notice how you write ptype in all lower case. However, in the markup of the page you declare it as Ptype with a capital P.

The OrderedDictionary that e.NewValues is, is case sensitive. This means that when you call this:
Code:
e.NewValues.Insert(e.CommandArgument, "ptype", ....
you essentially create a *new* entry in the list called ptype. At this stage, your collection has grown by one and you have a ptype and a Ptype entry. Things will break directly if you change the original code to this:
Code:
e.NewValues.Insert(e.CommandArgument, "Ptype", myDropDownList.SelectedValue)
With this code, you get the error I referred to earlier because you try to insert an item with a key that already exists (Ptype, which comes from the markup).

So far so good; you expanded the dictionary with one key. But how did it end up correctly in your object? (If you set a breakpoint on ObjectDataSource1_Updating you can see your object now has the correct Ptype property that I set in code.)

When the ObjectDataSource needs to recreate your BO that is eventually passed to your Upsert method, it creates an instance of your object, loops through all the keys in the dictionary, finds the connected property on the object and copies over the value. So, the code first finds Ptype, and sets the Ptype property on the painting. Then it finds ptype and again it sets the Ptype property, effectively overwriting the previous value.

You'll find that your manually inserted values with a different casing won't stick if you add them to the *beginning* of the list. E.g.:
Code:
e.NewValues.Insert(0, "ptype", 55)
won't give Ptype a new value on your object. .Net finds the Ptype property with a capital P in the collection *later* and uses that value instead.

Hope this clarifies things. Maybe a bit too technical, but IMO, it's interesting to look really deep into how things work in the .NET Framework.

Good luck,

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
 
Old August 17th, 2007, 07:27 AM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 238
Thanks: 0
Thanked 2 Times in 2 Posts
Send a message via MSN to rsearing
Default

Thanks so much Imar--makes sense!!

 
Old August 17th, 2007, 07:38 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

It's amazing what you can do with a little debugging and Reflector: http://www.aisto.com/roeder/dotnet/ :-)

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004





Similar Threads
Thread Thread Starter Forum Replies Last Post
question on subscript out of range error? kwik10z Excel VBA 1 November 30th, 2007 10:14 PM
detailsview not updating table when null column davej ASP.NET 2.0 Professional 3 April 11th, 2007 06:59 AM
Problem with updating the detailsview control rakesh225 ASP.NET 2.0 Professional 1 August 2nd, 2006 10:45 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.