locked
DataItem is NULL----pls help urgent RRS feed

  • Question

  • User-1671279171 posted

    HI,

    I need to add an object as well value in the selectionList for Mobile.

    I am using,

    if (!IsPostBack)
    {

        foreach (ScheduleService.RpSchedule item in list)
         {
              slRegSchedule.Items.Add(new MobileListItem(item, item.RpDttm.ToString(), item.RpId));
         } 

     

    In the Button Click event  when I am trying

        slRegSchedule.Selection.DataItem

    its giving NULL.

    I have tried the follwoing also, but it gives the same  result

    if (!IsPostBack)
    {

        foreach (ScheduleService.RpSchedule item in list)
         {
             datalist.Add(new MobileListItem(item, item.RpDttm.ToString(), item.RpId));
             slRegSchedule.DataSource = datalist;
             slRegSchedule.DataBind();
         } 

    }

     

    Guys pls tell me how can I get the dataItem value?

    Wednesday, January 24, 2007 10:40 PM

All replies

  • User52920949 posted

    Have you tried slRegSchedule.Selection.value?

    Martin

    Wednesday, January 24, 2007 11:08 PM
  • User-1671279171 posted

    HI Martin

    Thanx for your quick reply.

    yes  slRegSchedule.Selection.value is giving theright value.

    Wednesday, January 24, 2007 11:18 PM
  • User-1671279171 posted
    I couldn't find any solution yet, somebody help me.
    Thursday, January 25, 2007 2:11 AM
  • User52920949 posted

    I'm not absolutelt clear on what it is you are trying to do.  But, in the reference for the DataItem property of MobileListItem it says "This property is available only on a request where the list item is data-bound; on subsequent postbacks, it is always a null reference", does this apply to your problem?

    Martin

    Sunday, January 28, 2007 5:24 PM
  • User-1671279171 posted

    Hi Martin,

    Thanks for your reply. 

    Yes this is the problem.

    I want to get the object that I have assigned to dataItem field  for the a item on button click.

    Can you tell me how can I get that.

    Thanks again.

     

    Monday, January 29, 2007 12:58 AM
  • User52920949 posted

    When you are populating the selectionlist you are iterating through some other list type object -

    foreach (ScheduleService.RpSchedule item in list)

    If you are able to get the value (presumably some sort of id), then can't you use that value to get the object from the original 'list'?

    Thanks

    Martin

    Monday, January 29, 2007 8:41 PM
  • User52920949 posted

    Please ignore the previous post, I've had a go at reproducing your problem and now realise it was a dumb question.  I've made a little headway, but have no time left today, will let you know how I went tomorrow.

    Cheers

    Martin

    Tuesday, January 30, 2007 12:46 AM
  • User52920949 posted

    Sorry, I can't seem to figure out the purpose of being able to add an object to the list, there doesn't seem to be any way of retrieving the object after postback (and I can't think of any benefit of being able to access it before postback!).  Here's my bare-bones demo. 

    1    Partial Class Default2
    2        Inherits System.Web.UI.MobileControls.MobilePage
    3    
    4        Private Class Class1
    5            Private _id As Integer
    6            Private _name As String
    7    
    8            Public ReadOnly Property Id() As Integer
    9                Get
    10                   Return _id
    11               End Get
    12           End Property
    13   
    14           Public ReadOnly Property Name() As String
    15               Get
    16                   Return _name
    17               End Get
    18           End Property
    19   
    20           Public Sub New(ByVal id As Integer, ByVal name As String)
    21               _id = id
    22               _name = name
    23           End Sub
    24   
    25       End Class
    26   
    27       Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
    28           If Not Page.IsPostBack Then
    29               Dim list As New SortedList
    30               list.Add("one", New Class1(1, "One"))
    31               list.Add("two", New Class1(2, "Two"))
    32               list.Add("three", New Class1(3, "Three"))
    33               SelectionList1.DataTextField = "Key"
    34               SelectionList1.DataValueField = "Value"
    35               SelectionList1.DataSource = list
    36               SelectionList1.DataBind()
    37           End If
    38       End Sub
    39   
    40       Protected Sub Command1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Command1.Click
    41           Label1.Text = SelectionList1.Selection.Value
    42       End Sub
    43   
    44   End Class
    45   
    
     
    <body>
        <mobile:Form id="Form1" runat="server">
            <mobile:SelectionList ID="SelectionList1" Runat="server">
            </mobile:SelectionList>
            <mobile:Command ID="Command1" Runat="server">Command</mobile:Command>
            <mobile:Label ID="Label1" Runat="server">Label</mobile:Label>
        </mobile:Form>
    </body>
    
    

    I create a sortedlist full of objects of Class1 and bind it to a selectionlist.  I tell the selectionlist to use Key and Value as datatextfield and datavaluefield respectively because the sortedlist is a a collection of dictionaryentry objects, and the object is stored in their Value property.  The problem is that the selectionlist serialises the object on databinding and the only result I get inspecting it is a string "Class1"?!

    The only thing I can suggest is to store a unique ID in the value field and use that ID to requery the DB on postback.  Alternatively, you could try using the ObjectList class instead.  Here's an example in which all the data from an object is bound to fields in an ObjectList and some of the data is hidden until a selection is made...ObjectList.

    Sorry I couldn't be of more assistance.

    Martin
     

    Tuesday, January 30, 2007 6:54 PM