how to split a datalist bound item in asp

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bassemG
    New Member
    • Oct 2009
    • 7

    how to split a datalist bound item in asp

    Hello every body
    I created a simple program usine web development 2008. The program consists of a datalist control that is bound to an access database, a textbox and a button. When a string is entered in the txtbox and click the button I get the value of the string. The problem I am facing is that the value I am getting is something like: John-William-Henry-Jack. This format is already in the database. What I need is to split the string so that each name is separated and displayed on a new line. Now I know how to split a string but I need to get the string from <%# Eval("datafield ")%> before it is diplayed in order to split it. I am using vb language here.

    your help is appreciated
  • Nicodemas
    Recognized Expert New Member
    • Nov 2007
    • 164

    #2
    This is the ASP forum. Not ASP.NET.

    Comment

    • phvfl
      Recognized Expert New Member
      • Aug 2007
      • 173

      #3
      Hi Bassem,

      Try using the ItemDataBound event of the datalist. You should be able to access each item being bound through the event arguments - the method is called once for each item in the data source. Using this method allows for more manipulation of the information in the code behind.

      Have a go - I'll provide some example code when I get a chance, not a my development machine at the moment.

      Comment

      • bassemG
        New Member
        • Oct 2009
        • 7

        #4
        Thank You so much for your professional. I would love to have an example code
        Thank you again and appreciate your help

        Comment

        • phvfl
          Recognized Expert New Member
          • Aug 2007
          • 173

          #5
          Example code

          Here is some example code - this is as basic as possible. There is no error checking - you will have to make this functional for the real world.

          Code:
          Partial Public Class _Default
              Inherits System.Web.UI.Page
          
              Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
          
              ' Create a fake data source
              Dim dataSource As String() = New String() {"a-b-c", "d-e-f", "g-h-i"}
          
              ' Set the DataList datasource
              myList.DataSource = dataSource
          
              ' Bind the list
              myList.DataBind()
          
              End Sub
          
            Private Sub myList_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles myList.ItemDataBound
              
              ' e.Item is the ItemTemplate in the page code.
              ' Find the textbox control
              Dim textbox As TextBox = DirectCast(e.Item.FindControl("itemText"), TextBox)
          
              ' Get the current DataItem
              Dim item As String = DirectCast(e.Item.DataItem, String)
          
              ' Set the TextBox Text property
              textbox.Text = item.Replace("-", Environment.NewLine)
            End Sub
          End Class
          The corresponding page is:
          Code:
          <!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></title>
          </head>
          <body>
              <form id="form1" runat="server">
              <div>
              <asp:DataList runat="server" id="myList">
          <ItemTemplate><p><asp:TextBox id="itemText" runat="server" TextMode="MultiLine" /></p></ItemTemplate>
              </asp:DataList>
              </div>
              </form>
          </body>
          </html>

          Comment

          • bassemG
            New Member
            • Oct 2009
            • 7

            #6
            Originally posted by phvfl
            Here is some example code - this is as basic as possible. There is no error checking - you will have to make this functional for the real world.

            Code:
            Partial Public Class _Default
                Inherits System.Web.UI.Page
            
                Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            
                ' Create a fake data source
                Dim dataSource As String() = New String() {"a-b-c", "d-e-f", "g-h-i"}
            
                ' Set the DataList datasource
                myList.DataSource = dataSource
            
                ' Bind the list
                myList.DataBind()
            
                End Sub
            
              Private Sub myList_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles myList.ItemDataBound
                
                ' e.Item is the ItemTemplate in the page code.
                ' Find the textbox control
                Dim textbox As TextBox = DirectCast(e.Item.FindControl("itemText"), TextBox)
            
                ' Get the current DataItem
                Dim item As String = DirectCast(e.Item.DataItem, String)
            
                ' Set the TextBox Text property
                textbox.Text = item.Replace("-", Environment.NewLine)
              End Sub
            End Class
            The corresponding page is:
            Code:
            <!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></title>
            </head>
            <body>
                <form id="form1" runat="server">
                <div>
                <asp:DataList runat="server" id="myList">
            <ItemTemplate><p><asp:TextBox id="itemText" runat="server" TextMode="MultiLine" /></p></ItemTemplate>
                </asp:DataList>
                </div>
                </form>
            </body>
            </html>
            Thank you so much. that was very helpful

            Comment

            • bassemG
              New Member
              • Oct 2009
              • 7

              #7
              Your code was very helpful but I am getting the following error message:
              "Unable to cast object of type 'System.Data.Da taRowView' to type 'System.String' "
              Is there a way to fix this error
              Thanks a lot

              Comment

              • phvfl
                Recognized Expert New Member
                • Aug 2007
                • 173

                #8
                Originally posted by bassemG
                Your code was very helpful but I am getting the following error message:
                "Unable to cast object of type 'System.Data.Da taRowView' to type 'System.String' "
                Is there a way to fix this error
                Thanks a lot
                As mentioned this is a rough template - the data source in the example was an array of strings. This means that each individual item was a string. If you are passing in a DataView then each item will be a DataRowView. You need to convert the "e.Item.DataIte m" object to the correct type - and in this case refer to the correct column to get your value.

                Comment

                • bassemG
                  New Member
                  • Oct 2009
                  • 7

                  #9
                  Thank you so much expert phvfl
                  You showed me the starting road and finally I wasw able to capture the string in the datalist that I want to split. The code is shown below:
                  Code:
                  Protected Sub DataList1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles DataList1.ItemDataBound
                  
                   If e.Item.ItemType = ListItemType.Item Or _
                      e.Item.ItemType = ListItemType.AlternatingItem Then
                              Dim ds As SqlDataSource
                              ds = CType(e.Item.FindControl("Arabic"), _
                                  SqlDataSource)
                              Dim Words() As String
                              Dim aryBassem() As String
                             
                              Dim strString As String
                              Dim i As Integer
                              Dim f As Integer
                  
                              strString = DataBinder.Eval(e.Item.DataItem, _
                                  "column-Id").ToString()
                  
                              Words = strString.Split("-")
                              aryBassem = strString.Split("-")
                              strString = ""
                  
                              For i = 0 To UBound(aryBassem) 'aryBassem.Length - 1
                  
                                  'MsgBox(aryBassem(i))
                                  strString = strString & aryBassem(i) & Environment.NewLine
                                  'vbNewLine
                                  TextBox1.Text = strString
                              Next
                                      End If
                      End Sub
                  Thank you again
                  Last edited by Frinavale; May 18 '10, 07:09 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.

                  Comment

                  Working...