I am confused about something really simple in ASP.NET. I have seen many times a pattern where there is a three column table, with label, control and validator in each column, which I can put together pretty straightforwardly. However, what I don't understand is how to handle binding here. If I have a table with Customer record and FirstName, LastName and PhoneNum, I want a page that takes customerId in the query string, and binds that record to the page so that I can use Bind() as the text box values.
However, there is no DataSource property on the page to bind the record to. I know I am missing something obvious, but I can't figure it out.
Any help would be appreciated.
You need to put your controls (labels, textboxes, validators) inside a control that accepts data binding (ie. FormView, GridView, Repeater, etc.etc.) and then bind your data to that control.
One way to do this is to use a DataSource Control such as a SqlDataSource and use it in conjunction with a Data Control that will bind to the previous datasource control like a DetailsView. Then you configure your DataSource control to only bring in data for the particular CustomerId; the SqlDataSource allows you to use a QueryStringParameter as part of the select statement to the database.
Each of the above controls have wizards that will allow you to easily configure them.
Related
I have to maintain an ASP.net application in VB.Net.
There is a page with a FormView bound to a ObjectDataSource.
I have to add some business logic on the ItemUpdating event of this FormView.
Unfortunately, some the data that I need to add this business logic is not exposed on the FormView user-interface itself, so I can not use FindControl to get the values (I could add the controls, bind them to the fields I need and set their visible property to true, but that's ugly).
So, what I would need to do is to get access to the Data Row corresponding to the currently selected item in the FormView from the code behind as it has the data I need to add my business logic code.
Unfortunately, I don't manage to get access to the row.
Thanks in advance for your help.
Try this:
Dim myData As Object = DirectCast(formview1.DataItem, DataRowView)("MyColumn")
EDIT:
If I remember correctly the DataItem is Nothing on ItemUpdating so my solution above does not work, does it? Then you have to load it from your Datasource with the given ID(CommandArgument).
Thanks, I managed to sort it using the Select method of the ObjectDataSource object.
This returned me a DataView containing the row that was currently being edited.
I'd like to set the text of two labels to values found in a FormView on a page (whose data comes from an SQLDataSource.)
What's the best way to do this? I'm thinking of using the DataBound event for the FormView to set the label text to the value of a field in the FormView, or of using the SQLDataSource Selected event to set the labels to values retrieved by the query. Could I use the Page_Load event in conjunction with the FormView?
The FormView only displays one of the two values, though the other value is retrieved by the SQLDataSource.
I'm unfamiliar with accessing the data structures behind these controls but figure the data is there so I might as well use it rather than run the same SQL query twice.
My question then is which event do I use, which control do I access the data from, and how do I access the data from that control?
I'd use the OnDataBound event and get the value from the underlying datasource using:
lblExample.Text = ((DataRowView)((FormView)sender).DataItem)["fieldName"].ToString();
Hope it helps.
// CeriQ
If you're just trying to set a label, just set the label at the Page_load event:
myLabel.Text = "someValue";
ASP.NET newbie here. I would like to create a form with multiple types of controls for inserting a single record into a database table. This record has a "Type" field which is a foreign key, and I would like to populate a combobox with the possible values for it. I tried drag'n'dropping the table in design view (like in windows forms), but it always generates a gridview. How can I make it generate a form where I can specify the types of controls?
Thanks in advance
you could check detailsview and formview control.
http://quickstart.developerfusion.co.uk/QuickStart/aspnet/doc/ctrlref/data/detailsview.aspx
http://quickstart.developerfusion.co.uk/QuickStart/aspnet/doc/ctrlref/data/formview.aspx
Sounds like you either want to use a DetailsView or a FormView control.
I am trying to bind a datatable to a repeater. To do this, according to a code sample, I need to call dataview() when choosing the datasource property of the repeater and then write databind().
When I do this, I still get the exception that the source to databind to is not derived from IDatasourceControl.
What is the correct way to bind a datatable to a repeater? I want each record to repeat itself in the repeater (obviously).
I have seen this link (http://blogs.x2line.com/al/archive/2008/06/21/3469.aspx). What exactly does Container.Dateitem in the expression on the ASPX page mean?
Thanks
With a repeater you should be able to just do the following:
rpYourRepeater.DataSource = dtYourDataTable;
rpYourRepeater.DataBind();
That's it.
Just verify that your DataSourceID is tied to a real field in your datatable or just leave the DataSourceID out completely as you don't really need it depending on what you are doing.
You don't need any of that dataview stuff unless you want to start putting filters on your data.
I'm guessing that you are attempting to bind to DataSourceID. You should bind to DataSource in your repeater.
Well i have a gridview where i have defined the columns on my own and turned autogenerating off but now i have the problem that i cant access GridView.SelectedRow.DataItem.
As it turns out to be null now, when it had a value when auto generation was turned on..
Edit:
What i need is a way to save the ID of the row while not showing the ID to the user so if there is any way to do this?
I'm guessing DataItem is only properly filled when you are using DataBinding.
Are you using DataBinding?
Ok from this url:
The GridView (and actually, all our
data controls) does not save data
items across postbacks. This reduces
ViewState (if the objects are even
serializable) and enables garbage
collection to happen and clean up your
objects. So, when you click the
button to post back, the GridView has
not called DataBind and therefore your
data item isn't there. This is what
you've discovered.
Guessing you're reading the value from a postback, might just be the problem.
Try using SelectedValue, if you've setup the (primary) key for the items.
I've always used that and it worked.
msdn about SelectedValue
You can create a new hidden template column that will have a label with the ID . and in the cs file you use .FindControl on the rows.
You also have DataKeys property on the gridview, witch I think also does what you want