I know that this question has been asked multiple times, and i have searched the forum but it's still not working for me.
Here's the background:
On my page, I have a grid and a combobox. The grid uses datatable that is created dynamically when the the user selects some value from the combo box. Initially, when the page loads, only the combobox is visible and the grid is not visible. When the user selects a group name from the combobox, RadComboBox's SelectedIndexChanged is fired and it will populate the DataTable (this datatable is initially null) based on the selected group name (I also save the selected group name in a variable on the page). This will then call Rebind() method to rebind the grid using the table, and consequently fired the NeedDataSource event. At this point, the grid is properly populated. However if I clicked the Edit button or try to filter the columns, the grid will disappear. When I try to debug this, I found that when the edit button is clicked, the DataTable is now null, hence the reason why the grid is not displayed due to the null datasource. Even the variable that holds the selected group name is also reset.
I'm stuck here. Can anyone tell me how to get around this? Why were the datatable and the variable reset even though the page is not reloaded? Is there a way to stop the program from resetting the datatable and the variables?
Please help.
Here's the background:
On my page, I have a grid and a combobox. The grid uses datatable that is created dynamically when the the user selects some value from the combo box. Initially, when the page loads, only the combobox is visible and the grid is not visible. When the user selects a group name from the combobox, RadComboBox's SelectedIndexChanged is fired and it will populate the DataTable (this datatable is initially null) based on the selected group name (I also save the selected group name in a variable on the page). This will then call Rebind() method to rebind the grid using the table, and consequently fired the NeedDataSource event. At this point, the grid is properly populated. However if I clicked the Edit button or try to filter the columns, the grid will disappear. When I try to debug this, I found that when the edit button is clicked, the DataTable is now null, hence the reason why the grid is not displayed due to the null datasource. Even the variable that holds the selected group name is also reset.
I'm stuck here. Can anyone tell me how to get around this? Why were the datatable and the variable reset even though the page is not reloaded? Is there a way to stop the program from resetting the datatable and the variables?
Please help.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Data;
namespace Project1
{
public partial class frmUserGroupPrivillage : System.Web.UI.Page
{
private DataTable table;
private bool displayNullDataSourceErrorMessage = false; // enable error message to
// be hidden the first time the page is loaded
private string groupID, groupName;
protected void Page_Load(object sender, EventArgs e)
{
// reset error and status messages' visibility
errorMessage.Visible = false;
statusMessage.Visible = false;
//get module authorization for the logged in user
Authorization a = new Authorization();
bool[] authorization = a.GetModuleAuthorization(userID, "UserGroupPrivilege");
if (authorization != null)
{
// is the user is allowed to view this page?
if (authorization[0])
{
//the user is allowed to view this page
DisplayUserContent(authorization);
}
else
{
// the user is not allowed to view this page.
//Hence redirect user to main page
Response.Redirect("Default.aspx");
}
}
else
{
//unable to get authorization information
errorMessage.Text = a.authorizationErrorMessage;
errorMessage.Visible = true;
}
}
//Post: display or not display add, edit and delete buttons based on user's
// privilege level
private void DisplayUserContent(bool[] authorization)
{
// should update button inside edit form to be be displayed?
if (authorization[2])
{
GridColumn editColumn = RadGrid1.Columns.FindByUniqueName("EditCommandColumn");
editColumn.Visible = true;
}
}
protected void RadComboBox1_SelectedIndexChanged(object sender, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
{
displayNullDataSourceErrorMessage = true;
errorMessage.Visible = false;
groupID = e.Value.Trim();
groupName = e.Text.Trim();
// rebind the grid if and only if the table is successfully populated
if (PopulateTable(groupID, groupName))
{
RadGrid1.Rebind();
// make the grid visible not that it has data to display
RadGrid1.Visible = true;
}
}
// pre: invoked by radgrid1 when when it found that there is no data source during data binding
// post: returns the datasource for the grid
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
if (table != null)
{
RadGrid1.DataSource = table;
}
else
{
// do not display the error message when the user first viewed the module
if (displayNullDataSourceErrorMessage)
{
// the data table is null
errorMessage.Text = "Error. Unable to get grid data source from DataTable. The User Group Privilege cannot be displayed";
errorMessage.Visible = true;
errorMessage.Focus();
}
}
}
// pre: info: asuthorization information from database.
// accessType: 1 to indicate view, 2 to indicate add, 3 to indicate edit, 4 to indicate delete
// post: returns the boolen status for the view, add, edit, OR delete privilege
private bool GetBoolStatus(string info, int accessType)
{
bool status = false;
char[] c = info.ToCharArray();
switch (accessType)
{
case 1:
{
// caller method wants to know the View status
status = (c[0] == '1');
break;
}
case 2:
{
// caller method wants to know the View status
status = (c[2] == '1');
break;
}
case 3:
{
// caller method wants to know the View status
status = (c[4] == '1');
break;
}
case 4:
{
// caller method wants to know the View status
status = (c[6] == '1');
break;
}
default:
{
//should not get here
break;
}
}
return status;
}
protected void RadGrid1_EditCommand(object sender, GridCommandEventArgs e)
{
bool b = PopulateTable(groupID, groupName);
}
// post : return true if the table is successfully populated
private bool PopulateTable(string userGroupID, string userGroupName)
{
string query = "SELECT RTRIM(user_group_id) AS user_group_id, RTRIM(user_group) AS user_group "
+ "WHERE user_group_id = '" + userGroupID + "'";
int recordCount = 0;
db db = new db();
if (db.Load())
{
// query record corresponding to the selected userID ONLY
if (db.QuerySQL(query, ref recordCount) && recordCount == 1)
{
// create a new data table
table = new DataTable();
// declare columns for table
table.Columns.Add("Module Name", typeof(string));
table.Columns.Add("View", typeof(bool));
table.Columns.Add("Add", typeof(bool));
table.Columns.Add("Edit", typeof(bool));
table.Columns.Add("Delete", typeof(bool));
// get the number of columns in "records" table
int tableColumnsCount = db.SqlDataSet.Tables["records"].Columns.Count;
//get the selected group's privilege information
DataRow tempRow = db.SqlDataSet.Tables["records"].Rows[0];
// add records to the table by iterating the tempRow
DataColumn tempColumn;
for (int i = 0; i < tableColumnsCount; i++)
{
//get the column in at position i inside table
tempColumn = db.SqlDataSet.Tables["records"].Columns[i];
// get the COLUMN NAME for tempColumn DEBUG
string moduleInfo = tempColumn.ColumnName.Trim();
switch (moduleInfo)
{
case "user_group":
{
// do nothing
break;
}
case "user_group_id":
{
// do nothing
break;
}
default:
{
// variables to hold the privilage information in bool type
bool viewStatus = false; //dummy default value because the compiler refused to compile
bool addStatus = false; //dummy default value because the compiler refused to compile
bool editStatus = false; //dummy default value because the compiler refused to compile
bool deleteStatus = false; //dummy default value because the compiler refused to compile
// switch based on the COLUMN NAME for the columns with 0 is set to indicate true
if (moduleInfo.Equals("reconcile") || moduleInfo.Equals("report") || moduleInfo.Equals("admin")))
{
// get the value in tempRow at column position i. Set status to true if the value is "0"
viewStatus = "0".Equals(tempRow[tempColumn].ToString().Trim());
}
// switch based on the COLUMN NAME for the columns with 1 is set to indicate true
if (moduleInfo.Equals("SpeedKey") || moduleInfo.Equals("WebReports"))
{
// get the value in the cell at tempRow and tempColumn. Set status to true if the value is "1"
string info = tempRow[tempColumn].ToString().Trim();
viewStatus = GetBoolStatus(info, 1);
addStatus = GetBoolStatus(info, 2);
editStatus = GetBoolStatus(info, 3);
deleteStatus = GetBoolStatus(info, 4);
}
//add a new row to table
table.Rows.Add(moduleInfo, viewStatus, addStatus, editStatus, deleteStatus);
break;
}
}
}
return true;
}
else
{
//either the connection cannot be opened or there is more than one record returned
if (recordCount == 1 || recordCount == 0)
{
//connection could not be opened
errorMessage.Text = db.LastError;
errorMessage.Visible = true;
errorMessage.Focus();
}
else
{
// there is more than one record returned
errorMessage.Text = "Error. There is more than one privilege information for UserGroup " + userGroupName + ". "
+ "Please ensure that there is only one privilege information per user group.";
errorMessage.Visible = true;
errorMessage.Focus();
}
}
return false;
}
else
{
// cannot load db
errorMessage.Text = db.LastError;
errorMessage.Visible = true;
errorMessage.Focus();
return false;
}
}
}
}
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeBehind="frmUserGroupPrivilege.aspx.cs" Inherits="Project1.frmUserGroupPrivillage" %>
<
asp:Content
ID
=
"Content1"
ContentPlaceHolderID
=
"head"
runat
=
"server"
>
<
style
type
=
"text/css"
>
.style1
{
width: 98%;
}
</
style
>
</
asp:Content
>
<
asp:Content
ID
=
"Content2"
ContentPlaceHolderID
=
"Title"
runat
=
"server"
>
User Group Privilege Management
</
asp:Content
>
<
asp:Content
ID
=
"Content3"
ContentPlaceHolderID
=
"MainContent"
runat
=
"server"
>
<
telerik:RadAjaxManager
ID
=
"RadAjaxManager1"
runat
=
"server"
>
<
AjaxSettings
>
<
telerik:AjaxSetting
AjaxControlID
=
"RadComboBox1"
>
<
UpdatedControls
>
<
telerik:AjaxUpdatedControl
ControlID
=
"errorMessage"
/>
<
telerik:AjaxUpdatedControl
ControlID
=
"RadComboBox1"
/>
<
telerik:AjaxUpdatedControl
ControlID
=
"statusMessage"
/>
<
telerik:AjaxUpdatedControl
ControlID
=
"Label1"
/>
<
telerik:AjaxUpdatedControl
ControlID
=
"Label2"
/>
<
telerik:AjaxUpdatedControl
ControlID
=
"RadGrid1"
/>
</
UpdatedControls
>
</
telerik:AjaxSetting
>
<
telerik:AjaxSetting
AjaxControlID
=
"RadGrid1"
>
<
UpdatedControls
>
<
telerik:AjaxUpdatedControl
ControlID
=
"errorMessage"
/>
<
telerik:AjaxUpdatedControl
ControlID
=
"statusMessage"
/>
<
telerik:AjaxUpdatedControl
ControlID
=
"Label1"
/>
<
telerik:AjaxUpdatedControl
ControlID
=
"Label2"
/>
<
telerik:AjaxUpdatedControl
ControlID
=
"RadGrid1"
/>
</
UpdatedControls
>
</
telerik:AjaxSetting
>
</
AjaxSettings
>
</
telerik:RadAjaxManager
>
<
br
/>
<
table
align
=
"center"
class
=
"style1"
>
<
tr
>
<
td
>
<
asp:Label
ID
=
"errorMessage"
runat
=
"server"
Text
=
"Error Message"
Visible
=
"False"
CssClass
=
"frmElement_ErrorText"
></
asp:Label
>
<
br
/>
To begin, please select a user group from the combo box below:<
br
/>
<
br
/>
<
telerik:RadComboBox
ID
=
"RadComboBox1"
Runat
=
"server"
EmptyMessage
=
"Select User Group to be viewed"
Skin
=
"Vista"
DataSourceID
=
"GroupNameDataSource"
DataTextField
=
"user_group"
DataValueField
=
"user_group_id"
onselectedindexchanged
=
"RadComboBox1_SelectedIndexChanged"
AutoPostBack
=
"True"
Width
=
"250px"
>
</
telerik:RadComboBox
>
<
br
/>
<
br
/>
<
telerik:RadGrid
ID
=
"RadGrid1"
runat
=
"server"
CellSpacing
=
"0"
GridLines
=
"None"
Skin
=
"Vista"
onneeddatasource
=
"RadGrid1_NeedDataSource"
AutoGenerateEditColumn
=
"True"
oneditcommand
=
"RadGrid1_EditCommand"
Visible
=
"False"
>
<
MasterTableView
AllowFilteringByColumn
=
"True"
AllowSorting
=
"True"
AutoGenerateColumns
=
"True"
AllowPaging
=
"True"
EditMode
=
"InPlace"
>
<
CommandItemSettings
ExportToPdfText
=
"Export to PDF"
></
CommandItemSettings
>
<
RowIndicatorColumn
Visible
=
"True"
FilterControlAltText
=
"Filter RowIndicator column"
>
<
HeaderStyle
Width
=
"20px"
></
HeaderStyle
>
</
RowIndicatorColumn
>
<
ExpandCollapseColumn
Visible
=
"True"
FilterControlAltText
=
"Filter ExpandColumn column"
>
<
HeaderStyle
Width
=
"20px"
></
HeaderStyle
>
</
ExpandCollapseColumn
>
<
Columns
>
<
telerik:GridEditCommandColumn
ButtonType
=
"ImageButton"
CancelImageUrl
=
"Images/Cancel.gif"
EditImageUrl
=
"Images/Edit.gif"
FilterControlAltText
=
"Filter EditCommandColumn column"
InsertImageUrl
=
"Images/Update.gif"
UpdateImageUrl
=
"Images/Update.gif"
Visible
=
"False"
>
<
ItemStyle
HorizontalAlign
=
"Center"
Width
=
"30px"
/>
</
telerik:GridEditCommandColumn
>
</
Columns
>
<
EditFormSettings
>
<
EditColumn
FilterControlAltText
=
"Filter EditCommandColumn column"
UniqueName
=
"EditCommandColumn1"
></
EditColumn
>
</
EditFormSettings
>
</
MasterTableView
>
<
FilterMenu
EnableImageSprites
=
"False"
></
FilterMenu
>
</
telerik:RadGrid
>
<
br
/>
<
asp:Label
ID
=
"statusMessage"
runat
=
"server"
ForeColor
=
"#006600"
Text
=
"StatusMessage"
Visible
=
"False"
></
asp:Label
>
<
br
/>
<
asp:Label
ID
=
"Label1"
runat
=
"server"
Text
=
"Label1"
></
asp:Label
>
<
br
/>
<
asp:Label
ID
=
"Label2"
runat
=
"server"
Text
=
"Label2"
></
asp:Label
>
</
td
>
</
tr
>
</
table
>
<
asp:SqlDataSource
ID
=
"GroupNameDataSource"
runat
=
"server"
ConnectionString="<%$ ConnectionStrings:MyConnectionString %>" SelectCommand="SELECT RTRIM(user_group_id) AS user_group_id, RTRIM(user_group) AS user_group
FROM User_Group ORDER BY (user_group) "></
asp:SqlDataSource
>
<
br
/>
</
asp:Content
>