getting a value from a datalist

i've got this working but it returns the 'index' property whereas i'd like the value of what's in the property.i'll include the minimalist of code here









<%# Container.DataItem("ot_job_id")%>




<%# Databinder.Eval(Container.DataItem,"ot_job_id") %>
<%# Convert.ToDateTime(Databinder.Eval (Container.DataItem,"balance_date")).ToShortDateString %>
<%# Databinder.Eval(Container.DataItem,"bid_area") %>
<%# Databinder.Eval(Container.DataItem,"sched_start") %>
<%# Databinder.Eval(Container.DataItem,"sched_end") %>



thats the datalist, and i think that part is fine.
here's where i believe i get stuck, im only pulling the index and i need to get to the
actual value of the selection.


Sub getSelected(s as object, e as DataListCommandEventArgs )
dim mychoice as string
DataList1.SelectedIndex = e.Item.ItemIndex

mychoice = Datalist1.SelectedIndex.toString (i know im just pulling an index here, i
just wanted to see that i'm reaching anything)

lblMsg.text = mychoice
End Sub



thanks again to anyone who can shed some light on this
rik

Comments

  • : i've got this working but it returns the 'index' property whereas i'd like the value of what's in the property.i'll include the minimalist of code here
    :
    :
    :
    :
    :
    :
    :
    :
    :
    : <%# Container.DataItem("ot_job_id")%>
    :
    :
    :
    :
    : <%# Databinder.Eval(Container.DataItem,"ot_job_id") %>
    : <%# Convert.ToDateTime(Databinder.Eval (Container.DataItem,"balance_date")).ToShortDateString %>
    : <%# Databinder.Eval(Container.DataItem,"bid_area") %>
    : <%# Databinder.Eval(Container.DataItem,"sched_start") %>
    : <%# Databinder.Eval(Container.DataItem,"sched_end") %>
    :
    :
    :
    : thats the datalist, and i think that part is fine.
    : here's where i believe i get stuck, im only pulling the index and i need to get to the
    : actual value of the selection.
    :
    :
    : Sub getSelected(s as object, e as DataListCommandEventArgs )
    : dim mychoice as string
    : DataList1.SelectedIndex = e.Item.ItemIndex
    :
    : mychoice = Datalist1.SelectedIndex.toString (i know im just pulling an index here, i
    : just wanted to see that i'm reaching anything)
    :
    : lblMsg.text = mychoice
    : End Sub
    :
    :
    :
    : thanks again to anyone who can shed some light on this
    : rik
    :
    :

    Are you trying to access the text from the ItemTemplate or SelectedItemTemplate?

  • : : i've got this working but it returns the 'index' property whereas i'd like the value of what's in the property.i'll include the minimalist of code here
    : :
    : :
    : :
    : :
    : :
    : :
    : :
    : :
    : :
    : : <%# Container.DataItem("ot_job_id")%>
    : :
    : :
    : :
    : :
    : : <%# Databinder.Eval(Container.DataItem,"ot_job_id") %>
    : : <%# Convert.ToDateTime(Databinder.Eval (Container.DataItem,"balance_date")).ToShortDateString %>
    : : <%# Databinder.Eval(Container.DataItem,"bid_area") %>
    : : <%# Databinder.Eval(Container.DataItem,"sched_start") %>
    : : <%# Databinder.Eval(Container.DataItem,"sched_end") %>
    : :
    : :
    : :
    : : thats the datalist, and i think that part is fine.
    : : here's where i believe i get stuck, im only pulling the index and i need to get to the
    : : actual value of the selection.
    : :
    : :
    : : Sub getSelected(s as object, e as DataListCommandEventArgs )
    : : dim mychoice as string
    : : DataList1.SelectedIndex = e.Item.ItemIndex
    : :
    : : mychoice = Datalist1.SelectedIndex.toString (i know im just pulling an index here, i
    : : just wanted to see that i'm reaching anything)
    : :
    : : lblMsg.text = mychoice
    : : End Sub
    : :
    : :
    : :
    : : thanks again to anyone who can shed some light on this
    : : rik
    : :
    : :
    :
    : Are you trying to access the text from the ItemTemplate or SelectedItemTemplate?
    :
    :
    i've tried both ways. im sure im doing something wrong here.
    i'm trying to get the value from ot_job_id and will be happy to get it from either one. i do appreciate any help
    thnks
    rik
  • Here is your code ...

    [code]

    Sub getSelected(s as object, e as DataListCommandEventArgs)
    dim mychoice as string
    DataList1.SelectedIndex = e.Item.ItemIndex

    mychoice = Datalist1.SelectedIndex.toString

    lblMsg.text = mychoice
    End Sub

    [/code]

    Now depending on what you want to read, you have to write different code. If you only care about the text from the ItemTemplate then do this ...

    [code]
    Sub getSelected(s as object, e as DataListCommandEventArgs)
    DataList1.SelectedIndex = e.Item.ItemIndex

    ' HERE YOU SHOULD REBIND YOUR DATALIST SO THAT THE SELECTED ...
    ' ITEM TEMPLATE KICKS IN. HERE I CALL A FABRICATED FUNCTION.
    BindDataList()

    ' READ THE TEXT FROM THE LINK BUTTON
    Dim btnClick As LinkButton = _
    DirectCast(e.Item.FindControl("btnClick"), LinkButton)

    lblMsg.text = btnClick.Text
    End Sub
    [/code]

    This code assumes that you change the LinkButton declaration in the ItemTemplate to ...

    [code]

    '
    Runat="server" />

    [/code]

    We needed to give the LinkButton an ID and place the databind expression in the Text property. Note that single quotes are required.

    Now if you need the text from the SelectedItemTemplate, then you must do something trickier:

    [code]
    Sub getSelected(s as object, e as DataListCommandEventArgs )
    DataList1.SelectedIndex = e.Item.ItemIndex

    ' HERE YOU SHOULD REBIND YOUR DATALIST SO THAT THE SELECTED ...
    ' ITEM TEMPLATE KICKS IN. HERE I CALL A FABRICATED FUNCTION.
    BindDataList()

    ' WE HAVE TO GET THE DataListItem OBJECT A DIFFERENT WAY.
    ' FOR SOME REASON THE e.Item OBJECT ALWAYS POINTS TO ...
    ' A DataListItem UNDER THE ItemTemplate CONTEXT.
    Dim item As DataListItem = DataList1.Items(e.Item.ItemIndex)

    ' GET THE TEXT FROM AN UNEXPECTED CONTROL
    Dim myChoice As DataBoundLiteralControl = _
    DirectCast(item.Controls(0), DataBoundLiteralControl)

    lblMsg.text = myChoice.Text
    End Sub
    [/code]

    As you see here, you have to be a bit more slick. As a note, if the content of a template (ItemTemplate or SelectedItemTemplate) is purely literal text, minus any other controls, then the ASP.NET framework will instantiate a DataBoundLiteralControl, set its Text property to your content, and place it in the DataListItem control hierarchy. As you see, I took advantage of that knowledge above when parsing out the text from the SelectedItemTemplate.
  • : Here is your code ...
    :
    : [code]
    :
    : Sub getSelected(s as object, e as DataListCommandEventArgs)
    : dim mychoice as string
    : DataList1.SelectedIndex = e.Item.ItemIndex
    :
    : mychoice = Datalist1.SelectedIndex.toString
    :
    : lblMsg.text = mychoice
    : End Sub
    :
    : [/code]
    :
    : Now depending on what you want to read, you have to write different code. If you only care about the text from the ItemTemplate then do this ...
    :
    : [code]
    : Sub getSelected(s as object, e as DataListCommandEventArgs)
    : DataList1.SelectedIndex = e.Item.ItemIndex
    :
    : ' HERE YOU SHOULD REBIND YOUR DATALIST SO THAT THE SELECTED ...
    : ' ITEM TEMPLATE KICKS IN. HERE I CALL A FABRICATED FUNCTION.
    : BindDataList()
    :
    : ' READ THE TEXT FROM THE LINK BUTTON
    : Dim btnClick As LinkButton = _
    : DirectCast(e.Item.FindControl("btnClick"), LinkButton)
    :
    : lblMsg.text = btnClick.Text
    : End Sub
    : [/code]
    :
    : This code assumes that you change the LinkButton declaration in the ItemTemplate to ...
    :
    : [code]
    :
    : '
    : Runat="server" />
    :
    : [/code]
    :
    : We needed to give the LinkButton an ID and place the databind expression in the Text property. Note that single quotes are required.
    :
    : Now if you need the text from the SelectedItemTemplate, then you must do something trickier:
    :
    : [code]
    : Sub getSelected(s as object, e as DataListCommandEventArgs )
    : DataList1.SelectedIndex = e.Item.ItemIndex
    :
    : ' HERE YOU SHOULD REBIND YOUR DATALIST SO THAT THE SELECTED ...
    : ' ITEM TEMPLATE KICKS IN. HERE I CALL A FABRICATED FUNCTION.
    : BindDataList()
    :
    : ' WE HAVE TO GET THE DataListItem OBJECT A DIFFERENT WAY.
    : ' FOR SOME REASON THE e.Item OBJECT ALWAYS POINTS TO ...
    : ' A DataListItem UNDER THE ItemTemplate CONTEXT.
    : Dim item As DataListItem = DataList1.Items(e.Item.ItemIndex)
    :
    : ' GET THE TEXT FROM AN UNEXPECTED CONTROL
    : Dim myChoice As DataBoundLiteralControl = _
    : DirectCast(item.Controls(0), DataBoundLiteralControl)
    :
    : lblMsg.text = myChoice.Text
    : End Sub
    : [/code]
    :
    : As you see here, you have to be a bit more slick. As a note, if the content of a template (ItemTemplate or SelectedItemTemplate) is purely literal text, minus any other controls, then the ASP.NET framework will instantiate a DataBoundLiteralControl, set its Text property to your content, and place it in the DataListItem control hierarchy. As you see, I took advantage of that knowledge above when parsing out the text from the SelectedItemTemplate.
    :
    that was absolutely awesome. i cant thank you enough. and you are indeed at the level i'd love to be at. you know i go to accredited schools - read like a posssessed fiend, and have a huge library, and spend most of my time on the web looking for some of the same answers that you provided so easily.
    does this just come from experience or do i just need to buy every asp.net book i can get my hands on? in the 10+ books i have right now on asp.net, none of them went into any appreciable detail on the datalist control.
    again, i really do want to thank you.
    take care and have a good one
    rik
  • To be honest, I came up with the solution through trial and error. However, I am a professional .NET programmer for a living and have been so, for the past 3 years.

    Like you, I read a lot of books. In fact I have a mini library of my own. Sometimes I will read a book 2 or 3 times. Examples of such books, are:

    "ASP.NET Unleashed" by Steve Walther

    This book is written for those who use the .NET commmand line tools. Seeing the style you picked, this book will be perfect for you.

    "Profession Visual Basic.NET" by Franscesco Balena

    This is the authority on VB.NET. The book covers many aspects of the .NET Framework and has two huge chapters on ASP.NET Web Forms. This book teaches ASP.NET from the Visual Studio.NET point of view, which you may not be use to at first.

    If you really want to learn the platform, but do not have the money to spend on Visual Studio.NET, then you may download a free program called "Web Matrix" at http://www.asp.net.

    Also, if you want to see fully written .NET applications, that are free to download and modify, you can do so at http://www.asp.net as well.

    Finally, you will not have a perfect understanding of the underlying mechanics of an ASP.NET web page, unless you read specifically about custom web control development. The best book I browsed through on that subject was:

    "Developing Microsoft ASP.NET Server Controls and Components"
    By Nikhil Kothari, Vandana Datje

    In this book, they explain each step in the process of loading an ASPX page, how to create custom web controls, and more. However, the book was written with C#, but the concepts are easily translated to VB.NET.

    Basically, keep up what you are doing. Practice, practice, practice. Experiment with code. Try properties and functions you never tried before and just try to think of ways they could be useful in different scenarios. To be honest, some of the things I learned (through hard knocks) were through experimenting and some such things cant be found in any book. I've also had the liberty of working on many full-scale ASP.NET applications, some that have taken a couple of months to develop.

    Finally, participate in message boards like Programmers Heaven. Try to answer peoples questions. If you dont know an answer, do research on it and try to find the answer. For me, it is a great way for me to stay current on the technology.

    So good luck and I hope to see more participation in this message board.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories