Accessing Controls inside the ASP.NET Table - c#

My question is regarding ASP.Net Table control.
Is there any way to access a control properties after it is put inside the table?
The problem is control does not seem to appear inside the properties window any way.
I want to know if there any way to access these properties "Other than" going to HTML and changes them there.

From the MSDN docs on asp:Table:
It is important to remember that any programmatic addition or
modification of table rows or cells will not persist across posts to
the server. This is because table rows and cells are controls of their
own, and not properties of the Table control. To persist any changes
to the table, rows and cells must be reconstructed after each
postback. In fact, if substantial modifications are expected, it is
recommended that a DataList, DataGrid, or GridView control be used
instead of the Table control. As a result, the Table class is
primarily used by control developers.
are you trying to build a server control?

Related

C# show data from database in label

I've got a program which is linked to a Microsoft Access database and what I want to do is to display data from a data column, for example, customerName from the table customerDetails into a label. The drag and drop feature from the data sources panel puts the data into text boxes by default however, I want mine in labels. I've looked around but I couldn't find anything directly referencing how to put data into different controls like labels so any help is appreciated!
One way is to change the default control type for your fields in Data Sources window. Since you are already using that method, simply go to Data Sources window, expand your table and select required field(s). Then select a different control type (Label in your case) from the list.

Where to store web custom control data

I am creating a custom control extending WebControl. This web control allows the consumer to define a collection of columns in markup, something like this:
<Custom:CustomGrid>
<Columns>
<Custom:DataColumn HeaderText="FirstName" />
<Custom:DataColumn HeaderText="LastName" />
</Columns>
and put an IEnumerable in a DataSource property and this is rendered out to a table.
This control also allows paging. The IEnumerable in DataSource is the full list, and I display a page of the list at a time. I am already saving the current page, number of rows per page, etc. to viewstate. Should I also put the full list in viewstate? Maybe session?
This list can become a bit hefty. Maybe save in session with a random key, which is saved in viewstate?
What is the best practice here?
Edit: I don't think it's right to impose that all types in the IEnumerable be serializable. Is that fair? So do I need to copy the data source to some other data structure for serialization?
Edit 2: Even if I do use a base control instead of implementing RenderChildControls I will need to implement CreateChildControls, but I will still need to persist the data somewhere, or did I miss the point of the base class?
Indeed, not all IEnumerable instances will be serializable.
If the query is cheap to run I wouldn't persist the whole data set but just run the query again for a different page or a change in the sort order.
If you put the data in viewstate you'll end up with huge pages. Session state might be acceptable if you don't have many users, but large data sets with lots of users won't scale well. What if I bind a million rows to your control? Or what if your control was used in a repeater and shown 100 times on a page?
Are you sure you need to persist the data? This isn't premature optimisation is it?
Remember that your control is a UI component. The viewstate should hold enough information to maintain the UI state as it is. A change in state (e.g.: switching to a different page of results) is something for which your control should pass responsibility to the data source.
Take a look at good old GridView. It displays what you give it and remembers that. If you're using paging then it raises an event to say "the user has changed page; give me page x of data". For me, that's the best practice for a UI control.
For implementing databound control it is better to use base class which was designed to perform such task. For example in ASP.NET exist CompositeDataboundControl which can be used as a base class to implement custom data bound controls. I can advice to review the following Dino Esposito article:
http://msdn.microsoft.com/en-us/library/aa479016.aspx.
Basically if you create control like ASP.NET gridview then it is store values in viewstate. To be more clear it is create number of the DataRow controls which saved assigned values in viewstate. During postback it recreates the same number of rows and values are restored from viewstate. If you will save only datasource for example in session without using viewstate then you will need to redatabind data to your grid during every postback. So, if you create Server control similar to gridview then approach described in the Dino Esposito post will be very helpful because it shows how to create control similar to ASP.NET Server GridView control.

Storing a collection into a list and session - Front End

I have this program in which I am trying to store a collection of values into a list and session
Example:
valueCollection = (List<Values>)Session["Value"];
I want to do the front end in a way which will show a table with each row showing a label and textbox. This would be too simple to do obviously but I want it to show 4 rows of the table by default and then the user can select "add another" this will then add another row onto the table with a label and textbox exactly similar to the 4 default. Everytime the user selects "add another" the table increments by 1.
How do you do something like that, or is there an easy way?
Here is a screenshot to explain it better:
http://img828.imageshack.us/img828/9986/idead.png
The basic idea is that you bind a control to your data and provide GUI controls to allow users to add/edit/delete records. Once the records are modified you re-bind the control to see the changes.
Take a look at the Databound controls in the .Net Framework. For a quick first pass you could use a ListView - a control which provides built-in functionality to automatically generate and mange the add/edit/delete GUI elements.
In 'real world' usage, storing big datasets isn't a great idea as more users = more sessions and each session uses server resources. You should really be getting/storing your data in a database or perhaps an XML file.
HTH

Allowing to Customize (Add/Remove) ListView Columns and Add/Edit the records (WinForm)

I have an Employee class ( Id,Name,Age) with few objects. I am showing all the records in "Employee View" form using ListView control and allowing user to Add/Edit/Delete these records. And have separate Form to Add a new record or to edit an existing one.
(No database interaction, just have a List to persist these changes)
Now, I want to support this setup (Employee View => Add/Edit Employee) with a Customizable number of columns (by default, showing the columns as per fields in my Employee object). So that, if user wants to send in more information (like, DoB for example) then he should be able to add that column in ListView and Add/Edit Employee forms should show one appropriate control for each column ( example DateTimePicker for DoB) to add/edit a desired record.
Thanks in advance.
From memory the standard WinForms ListView control does not provide built-in data-binding. I would suggest that you are better off using the DataGridView in bound mode. That will give you the functionality where fields in the data source automatically appear as columns in the DataGridView.
The DataGridView also provides built-in editing of rows. This editing is "in-place" in the DataGridView itself, so this may not be ideal for certain types of applications. However you can disable in-place editing (using the ReadOnly and AllowUserToAddRows properties) and continue to display a separate add/edit form if required.
In your add/edit form, a common way to do what you ask is to add controls dynamically to form according to the number of fields in the data source. This involves creating the controls in code, setting position and format properties and then passing them to Form.Controls.Add(). You can then populate these fields manually or possibly use Simple Data Binding.

Gridview and Column sets

I have a Gridview that is data bound to an array of objects, with a ton of properties attached to them. The grid would need to be too wide to show all of them at once (and also overwhelming for the user), so I'd like to have some link buttons that post back to the server and show different sets of columns (all from this same data set array of objects), based on what "tab" the user clicked.
In the GridView I use TemplateFields to bind the columns to the object properties. What would be the best way to implement the different columns and views?
Should I just bind all the data, and then on the post back event for a tab press, show and hide only the columns I need for that tab? This seems like since it would be binding a lot more data than I am showing, that it might be unnecessarily slow.
Should I dynamically create the columns before the binding, and only create the columns and bind the data for the columns I want to show? What is the performance hit the page would take for dynamically creating the columns each time based on which tab was pressed?
Thanks!
If you have that many columns, you may want to think about using a different control instead of a gridview. A DetailsView with paging would probably render better and be more manageable for the users.
To answer your question, though, I don't believe option 1 would be that much of a performance hit. And I believe that option would be less strenuous than option 2. That mostly my opinion, though.

Categories