Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Ok, I'll be speciffic! I have a casting problem:

Ok, I'll be speciffic! I have a casting problem:

Scheduled Pinned Locked Moved ASP.NET
databasecsharpasp-netdotnetdata-structures
11 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    davidstern100
    wrote on last edited by
    #1

    I'm working on an asp.net project, that involves a database. I am selecting some data from the db (more than one cell from the table), and I want to cast it into an array, such as a string array or an int array (datum in a cell will be an array item). I don't want to use a tableview or a datagrid or stuff like that. I want complete control on displaying the data on the web page. I'll thank you very very much for replies!

    David Stern Online!

    M 1 Reply Last reply
    0
    • D davidstern100

      I'm working on an asp.net project, that involves a database. I am selecting some data from the db (more than one cell from the table), and I want to cast it into an array, such as a string array or an int array (datum in a cell will be an array item). I don't want to use a tableview or a datagrid or stuff like that. I want complete control on displaying the data on the web page. I'll thank you very very much for replies!

      David Stern Online!

      M Offline
      M Offline
      minhpc_bk
      wrote on last edited by
      #2

      davidstern100 wrote:

      I want complete control on displaying the data on the web page.

      I'm not really sure how you want to display your data on the web page, and I'm doubt you'll have to build up your custom control to do what you need as the built-in ones are not so power.

      D 1 Reply Last reply
      0
      • M minhpc_bk

        davidstern100 wrote:

        I want complete control on displaying the data on the web page.

        I'm not really sure how you want to display your data on the web page, and I'm doubt you'll have to build up your custom control to do what you need as the built-in ones are not so power.

        D Offline
        D Offline
        davidstern100
        wrote on last edited by
        #3

        I want to display it in a repeater. As I understand, the repeater's datasource is an arraylist. So I'll ask the question again: How do I display data from a database in a repeater?

        David Stern Online!

        M 1 Reply Last reply
        0
        • D davidstern100

          I want to display it in a repeater. As I understand, the repeater's datasource is an arraylist. So I'll ask the question again: How do I display data from a database in a repeater?

          David Stern Online!

          M Offline
          M Offline
          minhpc_bk
          wrote on last edited by
          #4

          Actually, you can specify an ArrayList object or an object implementing the IEnumerable or IListSource interface in general as the datasource for the repeater control. In addition, you can simply use a data source control like SqlDataSource or ObjectDataSource to quickly bind data from a datatore to the control. For more information, you can see Binding to Data Using a Data Source Control[^]

          D 1 Reply Last reply
          0
          • M minhpc_bk

            Actually, you can specify an ArrayList object or an object implementing the IEnumerable or IListSource interface in general as the datasource for the repeater control. In addition, you can simply use a data source control like SqlDataSource or ObjectDataSource to quickly bind data from a datatore to the control. For more information, you can see Binding to Data Using a Data Source Control[^]

            D Offline
            D Offline
            davidstern100
            wrote on last edited by
            #5

            Thanks for the help. I actualy soved the problem with a "Fill" command. Now I have a new question: Once the data is bound, the ItemTemplate in the Repeater is static. I want not just to display information in it in like in a simple html file, but to add controls (a button in my case) that will use the data in the speciffic ItemTemplate. How do I do that?

            David Stern Online!

            M 1 Reply Last reply
            0
            • D davidstern100

              Thanks for the help. I actualy soved the problem with a "Fill" command. Now I have a new question: Once the data is bound, the ItemTemplate in the Repeater is static. I want not just to display information in it in like in a simple html file, but to add controls (a button in my case) that will use the data in the speciffic ItemTemplate. How do I do that?

              David Stern Online!

              M Offline
              M Offline
              minhpc_bk
              wrote on last edited by
              #6

              There are 2 common ways to do that: + Use the data binding expression in the ItemTemplate of the Repeater control: How to: Bind to Data in a Templated Control in Visual Studio [^] + Use the ItemCreated or ItemDataBound events of the control, and you can see how to use the events in MSDN.

              D 1 Reply Last reply
              0
              • M minhpc_bk

                There are 2 common ways to do that: + Use the data binding expression in the ItemTemplate of the Repeater control: How to: Bind to Data in a Templated Control in Visual Studio [^] + Use the ItemCreated or ItemDataBound events of the control, and you can see how to use the events in MSDN.

                D Offline
                D Offline
                davidstern100
                wrote on last edited by
                #7

                ok. new question: I am using a Repeater control in my form. I have filled it up with data from my Sql database. I have placed a button in the ItemTemplate and linked it to a function on an event of the Repeater's ItemCommand. I am able to refer to the index of the item in the Repeater (e.Item.ItemIndex). From that function - how do I refer to an item from the data displayed in the ItemTemplate (DataBinder.Eval(blabla))?

                David Stern Online!

                M 1 Reply Last reply
                0
                • D davidstern100

                  ok. new question: I am using a Repeater control in my form. I have filled it up with data from my Sql database. I have placed a button in the ItemTemplate and linked it to a function on an event of the Repeater's ItemCommand. I am able to refer to the index of the item in the Repeater (e.Item.ItemIndex). From that function - how do I refer to an item from the data displayed in the ItemTemplate (DataBinder.Eval(blabla))?

                  David Stern Online!

                  M Offline
                  M Offline
                  minhpc_bk
                  wrote on last edited by
                  #8

                  + Basically, to access the data item that is associated with each RepeaterItem, you can use the code Repeater1.Items[indexer].DataItem and then cast it to the real type, and the DataItem property only has data when you bind data to the control and normally has the null value on postback as the Repeater control basically does not persist the bound data on postback. + You normally use the ItemCommand event to handle the Click event of any button placed inside the Repeater on postback. So in the ItemCommand event handler, you cannot access the data item using the (DataBinder.Eval(blabla)) or use the DataItem property (except that you rebind data to the control again). Instead, you can access the UI elements placed inside the ItemTemplate to get the displayed values. Another option is that you'll have to rebuild the data source again or store it somewhere such as the Session object after you first populate it and now you can reuse it, you then access the DataItem from the data source using the e.Item.ItemIndex or e.CommandArgument. You can see an example from the DataGrid control but that might give you an idea. DataGrid.ItemCommand Event [^]

                  D 1 Reply Last reply
                  0
                  • M minhpc_bk

                    + Basically, to access the data item that is associated with each RepeaterItem, you can use the code Repeater1.Items[indexer].DataItem and then cast it to the real type, and the DataItem property only has data when you bind data to the control and normally has the null value on postback as the Repeater control basically does not persist the bound data on postback. + You normally use the ItemCommand event to handle the Click event of any button placed inside the Repeater on postback. So in the ItemCommand event handler, you cannot access the data item using the (DataBinder.Eval(blabla)) or use the DataItem property (except that you rebind data to the control again). Instead, you can access the UI elements placed inside the ItemTemplate to get the displayed values. Another option is that you'll have to rebuild the data source again or store it somewhere such as the Session object after you first populate it and now you can reuse it, you then access the DataItem from the data source using the e.Item.ItemIndex or e.CommandArgument. You can see an example from the DataGrid control but that might give you an idea. DataGrid.ItemCommand Event [^]

                    D Offline
                    D Offline
                    davidstern100
                    wrote on last edited by
                    #9

                    How do I access the UI elements placed inside the ItemTemplate?

                    David Stern Online!

                    M 1 Reply Last reply
                    0
                    • D davidstern100

                      How do I access the UI elements placed inside the ItemTemplate?

                      David Stern Online!

                      M Offline
                      M Offline
                      minhpc_bk
                      wrote on last edited by
                      #10

                      For example, you have a label control named Label1 in the ItemTemplate, then there are two options to get reference to the Label control: + Use the Controls collection:

                      //Assuming the Label is the first child control.
                      Label lbl = e.Item.Controls[0] as Label;

                      + Use the FindControl method which is more flexible:

                      Label lbl = e.Item.FindControl("Label1") as Label;

                      D 1 Reply Last reply
                      0
                      • M minhpc_bk

                        For example, you have a label control named Label1 in the ItemTemplate, then there are two options to get reference to the Label control: + Use the Controls collection:

                        //Assuming the Label is the first child control.
                        Label lbl = e.Item.Controls[0] as Label;

                        + Use the FindControl method which is more flexible:

                        Label lbl = e.Item.FindControl("Label1") as Label;

                        D Offline
                        D Offline
                        davidstern100
                        wrote on last edited by
                        #11

                        Thanks a lot! You've been very helpfull!

                        David Stern Online!

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups