Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
|
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 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 Professional 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 July 7th, 2008, 03:09 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 Binding GridView to ObjectDataSource which...

...get's it's one select paremeter from a calendar's selected date (see below).

My problem is that when you select a new date - it never fires the Gridview. I tried puttins some custom code in the "Calendar1_SelectionChanged" function - but can't seem to know how to populate the "myDate" parameter inside the objectDataSource. When I try the following, I get an error "ObjectDataSource 'ObjectDataSource1' could not find a non-generic method 'GetMembersInEventByDate' that has parameters: myDate, myDate1. "

So I believe it's adding my paremeter to the one already declared - but not sure the syntax to simply put my paremeter as a value into what was declared (or maybe I'm completely off base here?)

Protected Sub Calendar1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Calendar1.SelectionChanged
        Dim selectedDate As Date
        selectedDate = Calendar1.SelectedDate
        Dim month As Integer = selectedDate.Month
        Dim year As Integer = selectedDate.Year
        RefreshCalendar(Calendar1, month, year)
        ObjectDataSource1.SelectParameters.Add("myDate", selectedDate)
        GridView1.DataBind()
    End Sub


<asp:GridView ID="GridView1" runat="server" GridLines="None" ShowHeader="False"
            AutoGenerateColumns="False" DataSourceID="ObjectDataSource1">
            <Columns>
                <asp:BoundField DataField="FName" HeaderText="FName" SortExpression="FName" />
                <asp:BoundField DataField="LName" HeaderText="LName" SortExpression="LName" />
                <asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
                <asp:BoundField DataField="MobPhone" HeaderText="MobPhone"
                    SortExpression="MobPhone" />
            </Columns>
        </asp:GridView>
        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
            SelectMethod="GetMembersInEventByDate"
            TypeName="Searing.Sprint.CustomerAdvocacy.Bll.Memb erManager">
            <SelectParameters>
                <asp:ControlParameter ControlID="Calendar1" Name="myDate"
                    PropertyName="SelectedDate" Type="DateTime" />
            </SelectParameters>
        </asp:ObjectDataSource>

Thanks,
Rob

 
Old July 7th, 2008, 03:18 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Have you tried:

Protected Sub Calendar1_SelectionChanged(ByVal sender As Object, _
       ByVal e As System.EventArgs) Handles Calendar1.SelectionChanged
  GridView1.DataBind()
  ' Or
  ' ObjectDataSource1.DataBind()
End Sub

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
 
Old July 7th, 2008, 03:51 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

Imar,

In my original post, the last line of the SelectionChanged event is a call to GridView1.DataBind. This wasn't working for some reason.

I fixed this by doing:

ObjectDataSource1.SelectParameters.Clear() then
ObjectDataSource1.SelectParameters.Add("myDate", selectedDate.tostring)

...but I'd still like to know how to programatically change the "value" of a parameter in code behind - as the "Add" method simply adds to the already declared parameters. What I did works ok since I only have one parameter - but I'd be out of luck with more than one without knowing how to programatically set it. I even tried creating a myParameter as New Parameter and then setting it's DefaultValue to the selectedDate and type as DateTime - but I kept getting that darn error message stating "can't find method with parameters ,.

Like I said - got it to work - but not sure that was the best way.

Thanks,
Rob

 
Old July 7th, 2008, 04:16 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Quote:
quote:In my original post, the last line of the SelectionChanged event is a call to GridView1.DataBind. This wasn't working for some reason.
I know. However, it was preceded by a lot of unnecessary code that tries to *create* another parameter that already exist, which in turn is the true reason for "some reason".

Did you try it without that, exactly as I posted it?

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
 
Old July 7th, 2008, 07:30 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

Imar,

Yes - I tried both of the statements and tried them each in a different order (although I would think you would want the objectdatasoure to bind before the gridview) For whatever reason, when I put a breakpoint on the MemberManager function below, the date being sent is always 7/1/2008

Public Shared Function GetMembersInEventByDate(ByVal myDate As Date) As MemberList
            Return MemberDB.GetMembersEnrolledInEventByDate(myDate)
        End Function

Yet, when I did it the other way, it worked. (by clearing the params and then adding the "selected date".

Besides trying to figure out why that date (7/1/08) is being picked...

It would *really* help out to know how to programatically tie into the actual parameter of the objectdatasource to set it's value in code behind - i've had that issue before. I realize that objectdatasource.selectparameters.add - will add a new parameter - what is the syntax to actually tie to the parameter I define in the .aspx page inside the control? I tried various different ways only to have an error that it couldn't find the method with paremeter...then it will list a variety of different things....all depending upon what I try.

Sincerely,
Rob
 
Old July 8th, 2008, 01:47 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You'd be surprised what a good Google search gives you. Searching for:

assign value parameter objectdatasource

gives you articles like this:

http://bytes.com/forum/thread480440.html

which shows you how to use the DefaultValue.

However, I still think simply calling DataBind should work as well. If you call DataBind, the databinding process starts again and the controls should be able to figure out the changed date on the Calendar1 control.

Cheers,

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
 
Old July 8th, 2008, 02:06 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

I wrote a very simple example for you (which is always a good idea to do if you're unsure what behavior to expect) that shows how it's supposed to work. Simply add a page called Default.aspx to a new Web Site, paste in the following HTML and then drop the VB class in App_Code.

[Default.aspx]
Code:
<%@ Page Language="VB" AutoEventWireup="false" 
      CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 
       Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>Untitled Page</title>
</head>
<body>
  <form id="form1" runat="server">
  <div>
    <asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>
      <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataSourceID="ObjectDataSource1">
        <Columns>
          <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
          <asp:BoundField DataField="Dob" HeaderText="Dob" SortExpression="Dob" />
          <asp:BoundField DataField="Age" HeaderText="Age" SortExpression="Age" />
        </Columns>
      </asp:GridView>
      <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
        SelectMethod="GetList" TypeName="Person">
        <SelectParameters>
          <asp:ControlParameter ControlID="Calendar1" Name="someDate" 
            PropertyName="SelectedDate" Type="DateTime" />
        </SelectParameters>
      </asp:ObjectDataSource>
  </div>
  </form>
</body>
</html>
[Person.vb]
Code:
Imports System
Imports System.Collections.Generic

Public Class Person

  Private _name As String
  Public Property Name() As String
    Get
      Return _name
    End Get
    Set(ByVal Value As String)
      _name = Value
    End Set
  End Property

  Private _dob As Date
  Public Property Dob() As Date
    Get
      Return _dob
    End Get
    Set(ByVal Value As Date)
      _dob = Value
    End Set
  End Property

  Private _age As Integer
  Public Property Age() As Integer
    Get
      Return _age
    End Get
    Set(ByVal Value As Integer)
      _age = Value
    End Set
  End Property

  Public Function GetList(ByVal someDate As Date) As List(Of Person)
    Dim myList As New List(Of Person)

    Dim test As New Person()
    test.Name = "Joop"
    test.Dob = someDate

    myList.Add(test)
    test = New Person
    test.Name = "Piet"
    test.Dob = someDate

    Return myList
  End Function

End Class
Whenever the Calendar changes its date, the page posts back, and GetList is called. Inside GetList I simply construct a few Person instances and assign them the requested date.

As you can see when you run this, it works as expected: the dates on the people in the list changes with the selected date on the calendar.

If this isn't the behavior you are seeing, something else must be wrong. Maybe RefreshCalendar is messing up the process?

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
 
Old July 8th, 2008, 10:23 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

Imar,

That is odd-I ran your example, and you're right, the correct date was passed through.

(JUST EDITED - Imar - you are on to something as when I comment out the refresh function, it works---so, now need to step back and figure out how to get this to work - as I am using the refresh function to "highlight" or select the dates that have events tied to them so a user can see, at first glance, which dates have events...it must be doing something to "selecteddates"....hmmm)

Here's my page:

Here's the RefreshCalendar Method:
I was finding myself using this quite often in a similar page. I'm trying to pre-populate the Calendar with all of the Events in the database that match the current month's month and year so that a user could see all the dates where events are listed.

Public Sub RefreshCalendar(ByVal myCalendar As Calendar, ByVal myMonth As Integer, ByVal myYear As Integer)
        Dim myEventDates As New List(Of Date)
        Dim myEvents As New CAGEventList
        myEvents = CAGEventDB.GetEventDates(myMonth, myYear)
        If Not myEvents Is Nothing Then
            For Each myCAGEVent As CAGEvent In myEvents
                myEventDates.Add(myCAGEVent.eventDate)
            Next

            For Each myDate As Date In myEventDates
                myCalendar.SelectedDates.Add(myDate)
            Next
        End If
    End Sub

CheckEnrollments.aspx:

<div id='enrollleft'>
        <h2>&nbsp &nbsp Check Enrolls</h2>
        <asp:Calendar ID="Calendar1" runat="server">
            <OtherMonthDayStyle CssClass="calendarotherday" />
        </asp:Calendar>
        <asp:GridView ID="GridView1" runat="server" GridLines="None" ShowHeader="False"
            AutoGenerateColumns="False" DataSourceID="ObjectDataSource1">
            <Columns>
                <asp:BoundField DataField="FName" HeaderText="FName" SortExpression="FName" />
                <asp:BoundField DataField="LName" HeaderText="LName" SortExpression="LName" />
                <asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
                <asp:BoundField DataField="MobPhone" HeaderText="MobPhone"
                    SortExpression="MobPhone" />
            </Columns>
        </asp:GridView>
        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
            SelectMethod="GetMembersInEventByDate"
            TypeName="Searing.Sprint.CustomerAdvocacy.Bll.Memb erManager">
            <SelectParameters>
                <asp:ControlParameter ControlID="Calendar1" Name="myDate"
                    PropertyName="SelectedDate" Type="DateTime" />
            </SelectParameters>
        </asp:ObjectDataSource>

Here's the Select Method in my MemberManager.vb class:

Public Shared Function GetMembersInEventByDate(ByVal myDate As Date) As MemberList
            Return MemberDB.GetMembersEnrolledInEventByDate(myDate)
        End Function


Here's the method in the MemberDB Class:


Public Shared Function GetMembersEnrolledInEventByDate(ByVal myDate As Date) As MemberList
            Dim tempList As MemberList = Nothing
            ' Using
            Dim myConnection As SqlConnection = New SqlConnection(AppConfiguration.ConnectionString)
            Try
                Dim myCommand As SqlCommand = New SqlCommand("sprocMemberSelectListByEventDate", myConnection)
                myCommand.CommandType = CommandType.StoredProcedure
                myCommand.Parameters.AddWithValue("@myDate", myDate)
                myConnection.Open()
                ' Using
                Dim myReader As SqlDataReader = myCommand.ExecuteReader
                Try
                    If myReader.HasRows Then
                        tempList = New MemberList
                        While myReader.Read
                            tempList.Add(FillDataRecord(myReader))
                        End While
                    End If
                    myReader.Close()
                Finally
                    CType(myReader, IDisposable).Dispose()
                End Try
            Finally
                CType(myConnection, IDisposable).Dispose()
            End Try
            Return tempList
        End Function
 
Old July 8th, 2008, 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

Ok..I think I have it figured out, but my solution isn't working yet. I believe that the selected date is fired, but then my refresh changes all the selected dates - to the list (of which 7/1/2008 is the first one in the list)---so I simply tried re-ordering my selectionChanged event so that the GridView and ObjectDataSource binding happens before refreshCalendar is triggered - but it's not working yet---still working on it though...more to come.

-Rob

 
Old July 8th, 2008, 11:12 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

Some more info--when I move GridView1.Databind before the RefeshCalendar function - and put a breakpoint in the Calendar1_SelectionChanged method - it actually gets to Gridview1 Databind and goest through to my MemberManager and MemberDB methods and actually gets the correct date---but the GridView is never populated (it returns the MemberList with the correct members picked for that date) but the GridView doesn't ever populate.?






Similar Threads
Thread Thread Starter Forum Replies Last Post
GridView binding with Arraylist myself.panku .NET Framework 2.0 2 November 5th, 2008 05:52 AM
binding GridView at runtime! veda Visual Studio 2005 3 August 4th, 2008 06:44 AM
Gridview problem in Binding ndramkumar ASP.NET 2.0 Professional 1 September 2nd, 2007 11:45 PM
Binding dropdownlist to gridView mcgarry101 ASP.NET 2.0 Basics 0 February 13th, 2007 07:12 AM
Binding fields on a gridview aldwinenriquez ASP.NET 2.0 Professional 0 June 1st, 2006 07:50 PM





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