DataGrids, SortCommands and Microsoft IE WebControl TabStrips Problem

C

Cole Trickle

Hello, I wonder if anyone has had any experience with this???

I have a page with an IE web control tab strip and a single datagrid. The
datagrid's columns are created manually depending on the SelectedIndex of
the tabstrip.

In order to get the SelectedIndex of the tabstrip I have to wait until the
Page_Load event.
In order to properly initialise the columns so they respond the SortCommand
event I have to use the Page_Init event.

The problem I have is that if I want the events of the column to fire I
can't get the right SelectedIndex (always 0) and if I want to generate the
different columns dependant on SelectedIndex, I get no events firing. I
keep going round in circles...

I have noticed from previous posts that it is suggested I add
<Columns><asp:BoundColumn></asp:BoundColumn></Columns>
the events will fire. They all do except for the SortCommand event. I
experimented and found that adding
<Columns><asp:HyperLinkColumn
SortExpression="Dummy"></asp:HyperLinkColumn></Columns>
will initialise the SortCommand event as well.

Now using the above solves the problem but I was wondering if anyone had
another method as I hate using this sort of workaround in my code.
Cheers
 
S

samuelrobertson

I create DataGrids all the time in page load. The sort column header
control gets a LoadPostData event and thats how the DataGrid control
knows to update its sort column. That's why you have to build the
DataGrid before LoadPostData (and that's why your dummy columns work -
however it is an unstable solution). However, there is an undocumented
LoadPostData2 that occurs immediately after PageLoad, so you should be
able to build the DataGrid at PageLoad and it should grab that event
and update its sort column.

Another possibility is detecting and using the Form.Request variables
directly.
 
C

Cole Trickle

Thanks for your reply Samuel...

I came up with an alternative solution, that seems to work OK. I create all
the columns in the Page_Init event (get all sorting and paging capabilities)
and then in the Page_Prerender event (get the SelectedIndex of the tabstrip)
I remove the columns I don't need prior to binding the data.
 
C

Cole Trickle

I thought I had solved it but was wrong...I'm a bit of a beginner so I'm not
sure I'll be using the correct terminology...Any help is VERY appreciated.

When I initialise all the columns in the Page_Init event the SortCommand
events seem to get 'registered' for all columns. Then I remove the columns I
don't need using the tabstrip's selectedindex property in the Page_Load.

In this instance I have 10 columns, 3 for the first tab, 4 for the second
and 3 for the last tab. So the first tab will only show columns 0-2, second
tab shows columns 3-6 and the last tab shows columns 7-9.

When I navigate to the second tab, extract data I need, bind data to
datagrid and click the sortcommand button the first column in the displayed
datagrid's column collection has a SortExpression of 'y' (this is column 3)
but in the SortCommand event the e.SortExpression is 'x' which is the
SortExpression of column 0 of all 10.

Here is the code with a lot of stuff removed to try and make it clearer...
public class Search : UserInterfaceLayer.LineIntBasePage {
//Control Definitions
protected System.Web.UI.WebControls.DataGrid dgResults;

private void Page_Init(object sender, EventArgs e) {
// Initialise Datagrid
this.InitDataGrids();
}

private void Page_Load(object sender, System.EventArgs e) {
// Remove columns not required for current view...
this.FormatDataGrids();
}

private void Page_PreRender(object sender, EventArgs e) {
if(this.ResultsDataSource != null){
//Now Sort the datagrids
SortGridData((ICollection)this.ResultsDataSource);

// Now bind the data
this.dgResults.DataSource = this.ResultsDataSource; //ResultDataSource
stored in Session
this.dgResults.DataBind();
this.dgResults.Visible = true;
}
}



//Web Form Designer generated code
#region Web Form Designer generated code
override protected void OnInit(EventArgs e) {
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent() {
this.dgResults.SortCommand += new
System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.dgResults_SortCommand);
this.Load += new System.EventHandler(this.Page_Load);
this.PreRender += new EventHandler(this.Page_PreRender);
this.Init += new EventHandler(Page_Init);
}
#endregion

//Event Handlers
private void dgResults_SortCommand(object source,
System.Web.UI.WebControls.DataGridSortCommandEventArgs e) {
Trace.Write("SORTCOMMAND", e.SortExpression);
this.SortField = e.SortExpression;
}

/// <summary>
/// Create all the columns required by the DataGrid
/// </summary>
private void InitDataGrids(){
Trace.Write("INITDATAGRIDS", "start of init: column count = " +
this.dgResults.Columns.Count);
// Initialise
HyperLinkColumn hc = null;
this.dgResults.Columns.Clear();

// Set up the columns
// Members
hc = new HyperLinkColumn();
hc.HeaderStyle.ForeColor = Color.White;
hc.HeaderText = "Member ID";
hc.SortExpression = "MemberID";
this.dgResults.Columns.Add(hc);

hc = new HyperLinkColumn();
hc.HeaderText = "Email";
this.dgResults.Columns.Add(hc);

hc = new HyperLinkColumn();
hc.HeaderStyle.ForeColor = Color.White;
hc.HeaderText = "Name";
hc.SortExpression = "Surname";
this.dgResults.Columns.Add(hc);

// Courses
hc = new HyperLinkColumn();
hc.HeaderStyle.ForeColor = Color.White;
hc.HeaderText = "Course Reference";
hc.SortExpression = "CourseReference";
this.dgResults.Columns.Add(hc);

hc = new HyperLinkColumn();
hc.HeaderStyle.ForeColor = Color.White;
hc.HeaderText = "Title";
hc.SortExpression = "Title";
this.dgResults.Columns.Add(hc);

hc = new HyperLinkColumn();
hc.HeaderStyle.ForeColor = Color.White;
hc.HeaderText = "Version";
hc.SortExpression = "Version";
this.dgResults.Columns.Add(hc);

hc = new HyperLinkColumn();
hc.HeaderStyle.ForeColor = Color.White;
hc.HeaderText = "Live";
hc.SortExpression = "Live";
this.dgResults.Columns.Add(hc);

// Course Instances
hc = new HyperLinkColumn();
hc.HeaderStyle.ForeColor = Color.White;
hc.HeaderText = "Course Instance Title";
hc.SortExpression = "CourseTitle";
this.dgResults.Columns.Add(hc);

hc = new HyperLinkColumn();
hc.HeaderStyle.ForeColor = Color.White;
hc.HeaderText = "Tutor";
hc.SortExpression = "Tutor";
this.dgResults.Columns.Add(hc);

hc = new HyperLinkColumn();
hc.HeaderStyle.ForeColor = Color.White;
hc.HeaderText = "Start Date";
hc.SortExpression = "CourseDate";
this.dgResults.Columns.Add(hc);

// Set up the search results pager style
this.dgResults.PagerStyle.Mode = PagerMode.NumericPages;
this.dgResults.PagerStyle.PageButtonCount =
PageHelper.DG_PAGEBUTTONCOUNT;
Trace.Write("INITDATAGRIDS", "end of init: column count = " +
this.dgResults.Columns.Count);
}

/// <summary>
/// Remove the columns that are not
/// required by the currently selected tab
/// </summary>
private void FormatDataGrids(){
const int MEMLOWER = 0; // Lower Column Index Boundary
const int MEMUPPER = 2; // Member Upper Column Index Boundary
const int COURSEUPPER = 6; // Course Upper Column Index Boundary
const int CIUPPER = 9; // Course Instance Upper Column Index Boundary

Trace.Write("FORMAT", this.tbsSearch.SelectedIndex.ToString());
// Set up the columns
switch(this.tbsSearch.SelectedIndex){
case 0: // Members
for(int i = CIUPPER; i > MEMUPPER; i--){
this.dgResults.Columns.RemoveAt(i);
}
break;

case 1: // Courses
for(int i = CIUPPER; i > COURSEUPPER; i--){
this.dgResults.Columns.RemoveAt(i);
}
for(int i = MEMUPPER; i >= MEMLOWER; i--){
this.dgResults.Columns.RemoveAt(i);
}
break;

case 2: // Course Instances
for(int i = COURSEUPPER; i >= MEMLOWER; i--){
this.dgResults.Columns.RemoveAt(i);
}
break;
}
Trace.Write("FORMAT", "end of FormatDataGrids: column count = " +
this.dgResults.Columns.Count + ", sort column 0 = " +
this.dgResults.Columns[0].SortExpression );



}

private void SortGridData(ICollection list) {
Trace.Write("SORTGRIDDATA", this.tbsSearch.SelectedIndex + ", " +
this.SortField);

// Set up the columns
switch(this.tbsSearch.SelectedIndex){
case 0: // Members
// Initialise
MemberCollection.MemberFields sortMem =
MemberCollection.MemberFields.InitValue;

switch(this.SortField) {
case "MemberID":
sortMem = MemberCollection.MemberFields.Id;
break;
case "Surname":
sortMem = MemberCollection.MemberFields.Surname;
break;
}

((MemberCollection)list).Sort(sortMem, this.SortAscending);
break;

case 1: // Courses
// Initialise
CourseCollection.CourseFields sortCo =
CourseCollection.CourseFields.InitValue;

switch(this.SortField){
case "CourseReference":
sortCo = CourseCollection.CourseFields.CourseReference;
break;
case "Title":
sortCo = CourseCollection.CourseFields.Title;
break;
case "Live":
sortCo = CourseCollection.CourseFields.Live;
break;
case "Version":
sortCo = CourseCollection.CourseFields.Version;
break;
}
((CourseCollection)list).Sort(sortCo, this.SortAscending);
break;

case 2: // Course Instances
// Initialise
CourseInstanceCollection.CourseInstanceFields sortCI =
CourseInstanceCollection.CourseInstanceFields.InitValue;

switch(this.SortField){
case "CourseReference":
sortCI =
CourseInstanceCollection.CourseInstanceFields.CourseReference;
break;
case "CourseTitle":
sortCI =
CourseInstanceCollection.CourseInstanceFields.CourseTitle;
break;
case "CourseDate":
sortCI =
CourseInstanceCollection.CourseInstanceFields.CourseDate;
break;
case "Tutor":
sortCI =
CourseInstanceCollection.CourseInstanceFields.Tutor;
break;
}
((CourseInstanceCollection)list).Sort(sortCI, this.SortAscending);
break;
}
}

#endregion
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top