- Home
- LLBLGen Pro
- LLBLGen Pro Runtime Framework
Two GridViews and a DetailsView for updating, best method?
Joined: 25-Nov-2007
Hello, I am using LLBLGen Pro 2.6 (Version 2.6 Final, June 2008 ) and Adapter, .NET 2.0 C# with VisualStudio 2005 and an Oracle 10g database.
Somehow I have so far not added new data to the database, but have only displayed and edited existing data. I now have a page for editing or adding new notes related to a particular student. There are two database tables used here, one with core student data and a second with notes for each student. The notes table has only a rowid, student id, date, topic and note for each record.
The page update and edit seems to be working (after a lot of trial and error!), but I'm not sure if I am using the best approach. Two questions I have are whether I should be using a PrefetchPath and whether I should be using a UnitOfWork. Or is the filtering and updating below an OK way to accomplish this edit/update? I did most tasks through the designer and template fields and added filters and some additional functionality in the code-behind file.
Also, although I added some code (the filter and Select) under DetailsView2_PageIndexChanging, this doesn't seem to have any effect. (?)
Putting the filter and _studentNotesDS.Select() in Page_Load and in GridView2 does seem to be needed to get the list of notes to refresh when the DetailsView2 is updated.
After an update, if you go back to either GridView1 or GridView2 the list is refreshed.
I have added details about creating the page below and have listed some of the code from the .cs file.
Thanks for any suggestions, and let me know if you need more information.
Carol
In the main .aspx file I added three datasources (adapter), one for the main students table (a 'students' entity) and two for a second notes table (a 'student notes' entity). GridView1 displays a list of students using the students' entity datasource. GridView2 displays all the notes for a student. The GridView1 control and a Select parameter (StCepStudentId) determines GridView2 data, which is a list of all notes for a particular student.
DetailsView2 displays one of the notes, based on the selected rowid, and lets someone edit a note or choose to insert a new note. I set a required id for the student in a new note by using an Update parameter based on the current selected student id, from GridView1.SelectedValue.
All three datasources and the Select and Update parameters were set in the designer, using the properties of the datasource.
Code behind file:
// using statements and namespace removed
public partial class academic_notes_update : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { // Select DeAnza CDS and Richmond CDS Codes to filter data and select only active students RelationPredicateBucket B = new RelationPredicateBucket();
IPredicateExpression A = new PredicateExpression();
// filter by school, De Anza and Richmond HS
A.Add((CepStudentsTbFields.StCdsCode == "07617960732164") | (CepStudentsTbFields.StCdsCode == "07617960735902"));
// include only active students
A.AddWithAnd(CepStudentsTbFields.StActive0910 == 'Y');
B.PredicateExpression.Add(A);
_studentEntityDS.FilterToUse = B;
// set up for list of notes, when page loads no student is selected, needed here.
_studentNotesDS.FilterToUse = new RelationPredicateBucket(CepStudentNoteTbFields.NtCepStudentIdFk == GridView1.SelectedValue);
// adding this helps to reset the event to refresh the datasource
_studentNotesDS.Select();
}
// Button1_Click, Button2_Click, and an unrelated string variable removed
// two grid views and a details view (below) are used to select a student (one table) and update or add a new note (second table, with notes stored related to a student)
protected void GridView1_SelectedIndexChanged1(object sender, EventArgs e) { /* for debug / / string selectedStudentId = GridView1.SelectedValue.ToString(); string selectedLastName = GridView1.SelectedRow.Cells[4].Text; string selectedFirstName = GridView1.SelectedRow.Cells[2].Text; Response.Write("Selected Id is: " + selectedStudentId); Response.Write(" and First Name is: " + selectedFirstName); Response.Write(" and Last Name is: " + selectedLastName); */ // set session variable with selected student name, to display while edit or add a new note // note: this was also previously also done in Page_Load, but editing // the details view caused the page to reload and the session var was cleared while still needed
string selectedStudent = GridView1.SelectedRow.Cells[2].Text + " " + GridView1.SelectedRow.Cells[4].Text;
Session["studentName"] = selectedStudent;
// Want to update Grid2, with list of notes, when the selected student in GridView1 changes
_studentNotesDS.FilterToUse = new RelationPredicateBucket(CepStudentNoteTbFields.NtCepStudentIdFk == GridView1.SelectedValue);
}
protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
{
/* for debug
string selectedNtRidPk = GridView2.SelectedValue.ToString();
string selectedNtCepStudentIdFk = GridView2.SelectedRow.Cells[2].Text;
Response.Write("Selected RowId from Notes is: " + selectedNtRidPk);
Response.Write(" and NtCepStudentIdFk from Notes is: " + selectedNtCepStudentIdFk);
*/
// want the details view to show the selected note
_studentNoteDetailsDS.FilterToUse = new RelationPredicateBucket(CepStudentNoteTbFields.NtRidPk == GridView2.SelectedValue);
// refresh GridView2 when a note is edited or a new note added; GridView1.SelectedValue is the current student
_studentNotesDS.FilterToUse = new RelationPredicateBucket(CepStudentNoteTbFields.NtCepStudentIdFk == GridView1.SelectedValue);
// adding this helps to reset the event to refresh the datasource
_studentNotesDS.Select();
}
// is this needed? It doesn't seem to have an effect
protected void DetailsView2_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
{
// refresh GridView2 when a note is edited or a new note added; GridView1.SelectedValue is the current student
// refresh of a note just edited or added happens after one of the GridViews is selected, not immediately after details view is updated.
_studentNotesDS.FilterToUse = new RelationPredicateBucket(CepStudentNoteTbFields.NtCepStudentIdFk == GridView1.SelectedValue);
// adding this helps to reset the event to refresh the datasource
_studentNotesDS.Select();
}
Hi Carol,
After reading your post I think you are using a correct approach. We uploaded a Databinding example in the main site. I uses a similar approach of yours. I see that (after trial and error) you found a reasonable approach.
Joined: 25-Nov-2007
Thanks for the quick reply and taking the time to read this long post. I am planning on creating another four or five pages based on this one, and would like to make sure the first works correctly.
Their are several pages I have to create with more complex entities, so I'll probably have more questions, but in the meantime I'll use this approach.