I have a DropDownList control which is databound via a SqlDataSource whose query returns 3 fields (Label, Value, and Active) - Is there a way to access the databound dataset programatically to check whether the Active flag is true or false?
I know that this can be done DURING the databind, but I need the Active flag to checked during PreRender phase as the control may need to select a value where Active=false if loading an old record where this selection had been made. (During PreRender, I'm removing all Active=false items, unless it's the value that I'm setting the DropDownList to).
I have this working perfectly by populating a DataView with a SqlDataSource. Select and comparing/removing items based on this, but hitting the database twice to get this info a second time for each control seems inefficient.
Also, I can't write a piece of reusable/generic code for all controls as my current method only works when the SqlDataSource doesn't require any SelectArguments. If I can access the databound data directly, this would bypass this problem too.
Any advice is much appreciated!
I think the main problem you're having comes from using a SqlDataSource as your data-access. SqlDataSources are easy to set up and give you instant results, but they are extremely inflexible and require reuse across pages.
I would create a function that grabs the data from the database and stores it in an List or IEnumerable. If the data is in a List then you have access to it programmatically and validation is much easier to achieve.
Here is a sample below that uses the Entity Framework to access the database
Mark up
<asp:DropDownList ID="ddlTest" runat="server"></asp>
Code Behind
public List<Record> GetAllRecordsByUserName(string credentials)
{
List<Record> recordList;
using (CustomEntities context = new CustomEntities())
{
IQueryable<Record> recordQuery = from records in context.Records
where records.UserName == credentials
select records;
recordList = recordQuery.ToList<Record>();
}
return recordList;
}
public void ValidateAndBind(string username)
{
List<Record> recordList = GetAllRecordsByUserName(username);
// Do validation here
ddlTest.DataSource = recordList;
ddlTest.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{
ValidateAndBind("test.username");
}
You can use whatever data-access method you desire, but setting it programmatically allows you to reuse it across pages. In the methods above I created it in the page for terms of space. In practice, you should create a class that handles specific data retrieval, which is usually called a Data Access Layer.
Related
So I have a Winforms program that is set up such that There is a single form with a datagridview that is responsible for displaying Litigation Matters. A matter is just a class that contains an ID, Description, some Dates, and then some Collections (Navigation properties) eg:
public int MatterID
public string Description
public DateTime? DateFiled
public ICollection<Contact> Plaintiffs
public ICollection<Contact> Attorneys
Now here is where the problem starts:
The user can view different sets of Matters; All Matters, Open Matters, Closed Matters, Searched Matters (based on search criteria), Matters associated with a given contact, etc (many more)
This MatterViewer form has a datagridview that is responsible for displaying the matters and refreshing the data as things get updated. (eg. A user could right click on a Matter in the datagridview and edit it, then the Matters in the Datagridview need to be refreshed).
In order for this to work, I currently have form constructors that take in a querystring so that I have access to the query that was used to obtain these specific matters and use it to refresh the specific data as needed:
public FormViewMatters(string matterQueryString, string dataSetName = null)
{
//Code here
}
If I were to pass in just a list of Matters to the Form, then I would only ever be able to refresh items in that list, not refresh the all potential matters based on the query.
Using the querystring however, limits me to lazy loading navigation properties. I currently need to eager load some of the properties and I am unable to do so.
So what is the best approach to go about refreshing specific data in general (including navigation properties)?
I've got a Page, a GridView using an ObjectDataSource with a SelectMethod and a DropDownList. The SelectMethod, among other things, gets a string-array containing several IDs (to filter the Data) - but I also need it as DataSource for the DropDownList.
Alas, I cannot DataBind the DropDownList inside the SelectMethod since it's null.
An Idea would be to bind this string[] to a Session-Variable, but then I'd have to either re-set it upon every Page_Load or remove it from Session on every other page if I want it to update in case something on the Database changed.
What I'm looking for is some kind of variable that is available both in Page_Load and my ObjectDataSources SelectMethod, but that removes itself upon leaving the page (i.e. navigating to any other page on my Web-Application (preferably without having to call a method on EVERY other Page).
I hope you could understand my problem.
Thanks,
Dennis
As I understand the need to fetch the string array arises from the performance hit that a separate roundtrip will cause. To work around this you may create a separate object to feed your object data source. This object will have two methods one for getting the string array and another for getting the data for the grid (i.e. the select method)
You may then put an object like this in your page and fetch the data in it in a lazy manner. If the object makes a call for any of the data it stores the other part in a field. You can then use the ObjectDataSource ObjectCreating event to pass this object on your page to the ObjectDataSource
protected void odsSomething_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
{
e.ObjectInstance = YourInsntanceAlreadyInThePage;
}
This way you will avoid the roundtrip.
Also consider making two web service calls at the same time using the asynchronous client calls so that you can make both calls for the same time. If this is viable depends on the flow of your logic.
What I'm looking for is some kind of variable that is available both in Page_Load and my ObjectDataSource's SelectMethod, but that removes itself upon leaving the page (i.e. navigating to any other page on my Web-Application (preferably without having to call a method on EVERY other Page).
In a similar situation, I've used the Items property of the current HttpContext. It's an IDictionary (non-generic), so can hold arbitrary objects keyed by arbitrary objects, and its lifetime is precisely the duration of the current request, so will go away as soon as the request is ended. To use:
// Where you first get the data
HttpContext.Current.Items["SomeKey"] = new [] { "string1", "string2" };
// Where you want to to use the data
var strings = (string[])HttpContext.Current.Items["SomeKey"];
I've been looking into how best to do this and wisdom would be appreciated. For read only purposes, I've been happily using LINQ and binding it to a grid. For editing purposes, I've used the LinqDataSource control, enabled the Edit/Delete operations in the process, and I have a nice editable grid bound to some or all of the table's fields.
Now I have a situation where I want to edit a few fields in table A, but there are various values in linked table B that I want to display in that grid too (no editing of those). So my query looks like the below. The fields in tblDupes (cleared, notes) are what I want to edit, but I'd like to display those tblVoucher ones.
var theDupes = from d in db.tblDupes
where d.dupeGroup == Ref
select new
{
Ref = d.dupeGroup,
InvoiceNum = d.tblVoucher.invoiceRef,
Value = d.tblVoucher.invoiceAmtDecimal,
VendorNum = d.tblVoucher.vendorID,
VendorName = d.tblVoucher.vendorName,
Cleared = d.Cleared
Notes = d.Notes
};
A similar but different question LINQDataSource - Query Multiple Tables? sent me looking at scott Guthrie's blog entry http://weblogs.asp.net/scottgu/archive/2007/09/07/linq-to-sql-part-9-using-a-custom-linq-expression-with-the-lt-asp-linqdatasource-gt-control.aspx, where he handles various events to have a LinqDataSource with a custom query across tables. This still seems aimed at explicitly designed classes though, even if the class has only a subset of the fields.
So my question is: is there an easy way to allow committing of the changes made to the anonymous collection (a changes.Submit type action), or just an easy way to 'display' fields from another table while not involving them in the updating?
EDIT: Thinking more, it doesn't have to be anonymous really. I'd be happy to define a class to contain the elements in that query, since it won't change often. But, those elements would be across two tables, even though only one needs updating. Not sure if that suggests entity framework would be more suitable - I get the feeling it wouldn't - I don't want the whole 'model' always grouping the fields in this way.
Thanks!
Taking a wild guess here, but couldn't you listen to the LINQDataSource.Updating event and perform your save there? You would, of course, have some problems with the mapping since you cannot type the object in the LinqDataSourceUpdateEventArgs.OriginalObject.
What if you create a ViewModel instead of the anonymous type. Something like DupeVoucherViewModel.
Then in the Updating event, you could cast the LinqDataSourceUpdateEventArgs.OriginalObject to the DupeVoucherViewModel object and start mapping and saving your data.
Example
Given that you create a view model (a class) that you call DupeVoucherViewModel and bind to that like so:
var theDupes = from d in db.tblDupes
where d.dupeGroup == Ref
select new DupeVoucherViewModel
{
Ref = d.dupeGroup,
InvoiceNum = d.tblVoucher.invoiceRef,
Value = d.tblVoucher.invoiceAmtDecimal,
VendorNum = d.tblVoucher.vendorID,
VendorName = d.tblVoucher.vendorName,
Cleared = d.Cleared
Notes = d.Notes
};
Then the server tag should map the updating event like so:
<asp:LinqDataSource EnableUpdate="true" OnUpdating="LinqDataSource_Updating" />
And the code behind should contain the following method:
protected void LinqDataSource_Updating(object sender, LinqDataSourceUpdateEventArgs e)
{
// originalObject contains the unchanged object
var originalObject = (DupeVoucherViewModel)e.OriginalObject;
// newObject contains the changed object
var newObject = (DupeVoucherViewModel)e.NewObject;
// Perform your save using newObject
yourDataAccessComponent.SaveSomething(newObject);
}
You might need to include some more information in the DupeVoucherViewModel such as an ID of tblDupes or something, so that you can load that and change it.
You can read more about the LinqDataSource here: http://msdn.microsoft.com/en-us/library/bb514963.aspx
I've created two classes in business layer.
the first one is called Users with id (int), pass (string) and privileges (Privilege) properties and the second one is called Privilege and has id (int) and privilegeName (string) properties.
I've a method that returns all the users, and I use a repeater (actually I bind it to a DataList to auto create the ItemTemplate for me and then use a repeater) and it works and displays all the properties well except for my List property. it generates instead something like this System.Collections.Generic.List`1[WebApplication2.Public.BLL.Users]
I want to display it in a friendly way like "User Privileges : Privi1, Privi2" but still I want to keep the layers of my application clean and structured, for example I won't store them in a database in the same table and just store them as a text and append it.
I hope to find a simple and good solution...Thanks in advance guys =)
PS : I don't want to display the object Privilege, I want to display privilege.privilegeName
When using repeaters, there are two approaches, one is the one suggested by Bugai13: to have a custom property that displays it. This is fine for certain types of nested data.
Your other option is to just have a repeater inside a repeater, and bind it appropriately (to what would be a list assigned to your main data object, depending on how you O/R Mapper works).
You can have the code for the custom display property not in the data model, but in your presentation layer somewhere (depending on your framework/design), so it's not a "bad" thing to do that. It's up to you, with whatever "feels" best.
Just create property at your Bussiness object, and bind it:
public string PrivilegiesString
{
get
{
var sb = new StringBuilder("User Privileges : ");
foreach(var item in privileges)
{
sb.AppendFormat("{0}, ",item.privilegeName);
}
return sb.ToString();
}
}
I've looked all over and haven't been able to find a clear answer to a seemingly common question: How can I do two-way databinding over a many-to-many relationship in ASP.net?
I have the following database structure:
I am currently writing a page for editing or adding a User record. Databinding things such as name and password is simple, but what I really need it to be able to display a list of all PhoneGroups and choose one or more from the list. How do I do this?
I tried a CheckBoxList, but while I can display the list of PhoneGroups, How do I bind the Checked state of each box based on whether the user has access? Some solutions use a loop in the OnDataBound event of the CheckBoxList. If I do this, how do I update the database when the checked state changes? I could go the brute force approach and write code to do this, but isn't there something that can make this simpler? It seems like such a common scenario.
Update #1
I am currently using Devart's LinqConnect, but I am open to change. The backend database is MySQL.
Yeah it is a common scenario and binding to that event is the solution i see used.
It is fairly simple when you consdier what the code is doing int he background. You could write your own custom server control, but thats a lot more difficult.
MVC may offer you an alternative ...
really why not redesign and only return the objects that they ahve permission for?
as for updating items in the database you need to say more about the architecture. But ultimatley to update an item you have to take the new item ... you have to do womthing like this
public void StoreTheUpdatedData(YourBusinessObject theBusinessObject)
{
var yourDataContext = new DataContext()
var oldObject = (from i in yourDataContext.YourbusinessObjects
where (blah equals blah to select your item and only your item)
select i).First();
//repeat for all properties in the object
oldObject.Property = theBusinessObject.Property;
yourDataContext.SaveChanges();
}
code liek that is what you need to do the update.
the save method varies depending on which ORM you are using ... I think linq2SSql uses commitChanges for instance. Been a while since i used that one.