How to populate many textboxes from a sql query automatically? - asp.net

This might be a silly question. But I have a lot of textboxes in an in asp.net form I need to populate with data from a sql query.
If the query column names and textbox names are all the same, is it possible to populate the textboxes from the query data automatically? Maybe using loop? There are too many fields so I think it is stupid to populate each field individually using something like:
textboxX.text = data.getValue(0).ToString();
textboxY.text = data.getValue(1).ToString();
...
I am thinking there must be a simpler way to populate all of the textboxes. Can anyone please help me to do this? A simple example would be great.

If you were using something like MVC or MVVM then it would be in a manner of speaking. You could tie your view page to a model that has properties corresponding to the fields. Then in your view page you would bind the model properties to the fields.

If you don't work with MVC/MVVM/knockout then the best thing you could do is to loop your form's controls and assign them some values from your list. For this purpose it's simplier if your controls are named like Ctrl1, Ctrl2, etc - it's just to simplify the looping/assignment

Related

Displaying data with ASP.NET

I have a tbl_categories and a tbl_items. I want to display tbl_categories in a horizontal manner and list objects from tbl_items vertically below each category name. I am confused how to get all this data using TSQL stored procedures and displaying them using ASP.NET native controls.
Columns with headers of category names. rows of items keyed with category_id.
The db is set up correctly. It is the ASP.NET controls I have trouble with.
I would use a Repeater myself, and make it output an HTML Table. The categories row would be in the HeaderTemplate, the closing tags in the FooterTemplate, and the actual data inside the ItemTemplate
http://blogs.sitepoint.com/asp-net-repeater-control/
The best way to handle this is to setup business objects which support the data in a way you wish to present it, which may not always be the way it is handled by your database.
Then you can use those objects directly to bind or feed data into the UI.
You could use Pivot to do that. See this referece http://msdn.microsoft.com/en-us/library/ms177410.aspx

Easiest method to build where clause from checked items in DataList of PreviousPage

I have a selection list that is generated dynamically, it lists a number of checkboxes that are based on an end-user editable table, as such I have no idea what data, or how much, might be contained in this table and how many checkboxes or what they might contain, other than the primary keys of the table.
The user will select the checks they wish to see, and then control is passed to another page via PostBackUrl. The second page has to figure out which records to show (build it's where clause) based on the checkboxes checked in the previous page.
So, my problem is several-fold. First, asp:CheckBoxes don't have values. This can be worked around by a number of methods. Right now, i'm using a placeholder and dynamically creating the checkboxes in the ItemDataBound event of the DataList. I set the ID to "CheckboxKey1Key2" (where Key1 and Key2 are the primary keys of the check items).
Second, I have to walk through the controls of the PreviousPage to dig out all these values. That in itself is also a pain, but doable.
Now, my thinking is to build the where clause of my Linq2Sql query based on the keys I got from decoding the checked checkbox names. This all seems like a lot of jumping through hoops for something that shouldn't be this difficult. Am I missing something? Does anyone have any better solutions?
Make a class with a structure for your values that will be useful and easy for you to use on the result page. Then when the user clicks whatever it is they click to go to the result page, loop through the DataList, create a collection from the checked items, and you will only need to grab one object instead of everything, when you need to format your query.
Then when you get to the result page, loop through the collection and build the query in the loop. Pretty easy and not much code.

Gridview binding without using a Database

Can a GridView be bound to a List<>?
I want to create a Master Details scenario. So The first List<> contains Master data and the second Child data. When the user clicks on the Master Data, the child data, should appear in a pop.
Is that possible? Please share some code.
Yes, this is possible. Take a look at the ObjectDataSource to use for databinding a list to a GridView. It requires some more manual coding, but it works.
There's a good discussion of one possible solution here that manually constructs a DataTable from your business object list to make filtering/sorting/paging easier than manually implementing those methods.
You can create on temporary table and then bind that table with grid.
Like,
dataset.DataSource=tempTable;
tempTable.DataBind();
GridView1.DataSource = theList;<br/>
GridView1.DataBind();

Telerik GridViewDataControl - load from datatable

does anyone uses this control and knows how to load data to it from simple datatable. There is not such a trick as DataSource...
Im assuming you just mean a datatable you have created programatically?
It will bind straight up like it would from a DataSource.
Just make sure your DataField name in the columns mark up matches the name of the column and your good to go, just as you would with a different datasource.
Then just do
grvFoo.DataSource = yourProgrammticallyCreatedTable;
grvFoo.DataBind();
But, since your doing this progrmatically I would recommend making a "BindTable" function. Eg a function that builds your datatable and bind. You can then use this for sorting / paging etc. As your doing this all programatically you must handle such events by hand, they are normally done with the SQL / Object Datasource etc.
you mean Telerik Radgrid ? If yes, you can also specify a "NeedDataSource" event in the aspx/ascx
http://www.telerik.com/help/aspnet-ajax/grdadvanceddatabinding.html
On a side note, just to let you know, we are having a lot of performance problems with Telerik. For the most part, you can use the inbuilt asp.net gridview (and do some more styling if needed) than taking a performance hit by using telerik (for their fancy grid)
We must use ItemsSource property like this:
dataGridView1.ItemsSource = LoadDataTable.DefaultView;
LoadDataTable - this is some DataTable, filled from SQL.

how do I create an array or list of ASP.NET checkboxlists

I know how to use the checkboxlist in ASP.NET to display options retrieved from a database. What I don't know how to do is to make this 2-dimensional. That is, I need a list of checkboxlists where I don't know how long the list is; both dimensions of the checkboxlist will be determined by
list of people (pulled from database)
list of tasks (pulled from database)
and the user of the web page will click in column/row to specify which people will be assigned which tasks.
Right now I'm thinking that my only option is to brute-force it by creating a table and populate each cell with its own checkbox. (yuck)
Is there a more elegant way to create a 2-dimensional array of checkboxes with labels for both rows and columns?
I would use a repeater along with a checkboxlist. Depending on how your database is setup you could have each checkboxlist databound.
I've done this before and resorted to the brute-force method you suggest.
It's not as nasty as you'd think. Other solutions that were declarative and databound would likely be just as convoluted and confusing.
I use the ASPxGridView from DevExpress. It has a control column type of Selected (or something like that) which will display a checkbox in the column with the other column populated from your bound datasource. The User can select any rows desired by checking the checkbox on the row and you can get all the selected rows easily inot a collection to process. DevExpress components really do get rid of a lot of brute-force programming.
You can programmitaclly use a GridView control. It's inherently two-dimensional and you can use databound CheckBoxFields for it.
If you're looking for a quick and dirty way, you can use the AJAX Control Toolkit with the two controls and can populate one based on the other. If that's not what you're looking for, I'd do it the brute force way.

Resources