Ok, I'll be speciffic! I have a casting problem:
-
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!
-
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!
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.
-
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.
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?
-
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?
Actually, you can specify an ArrayList object or an object implementing the
IEnumerable
orIListSource
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[^] -
Actually, you can specify an ArrayList object or an object implementing the
IEnumerable
orIListSource
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[^]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?
-
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?
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
orItemDataBound
events of the control, and you can see how to use the events in MSDN. -
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
orItemDataBound
events of the control, and you can see how to use the events in MSDN.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))?
-
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))?
+ 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 [^]
-
+ 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 [^]
How do I access the UI elements placed inside the ItemTemplate?
-
How do I access the UI elements placed inside the ItemTemplate?
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;
-
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;
Thanks a lot! You've been very helpfull!