Database driven menu is too slow - asp.net

in ASP.Net using C#, I am populating an ASP.Net menu control from database dynamically. I am trying to make those items unusable which have some child items below them. This is to ensure that on mobile, where we dont have a mouse to hover, on clicking the parent menu item, user will surely get child items in stead of a postback. The code below is working fine but with a drawback, it takes too much time to load and in the mean time shows a bad result by displaying all the items (all parent + all child) vertically in a single view.
SqlCommand com = new SqlCommand("SELECT * FROM menutab order by sno", c.con);
if(c.con.State == ConnectionState.Closed)
c.con.Open();
da = new SqlDataAdapter(com);
table = new DataTable();
da.Fill(table);
parents = new int[table.Rows.Count];
for (int i = 0; i < table.Rows.Count; i++)
if (table.Rows[i].ItemArray.GetValue(2).ToString() != String.Empty)
parents[i] = Convert.ToInt32(table.Rows[i].ItemArray.GetValue(2).ToString());
DataRow[] drowpar = table.Select("menu_parent = " + 0);
view = new DataView(table);
view.RowFilter = "menu_parent is NULL";
foreach (DataRowView row in view)
{
MenuItem menuItem = new MenuItem(row["item"].ToString(), row["sno"].ToString());
//menuItem.NavigateUrl = row["menu_url"].ToString();
for (int i = 0; i < parents.Length; i++)
{
if (Convert.ToInt32(row["sno"].ToString()) == parents[i])
{
menuItem.Selectable = false;
}
}
NavigationMenu.Items.Add(menuItem);
AddChildItems(table, menuItem);
}
private void AddChildItems(DataTable table, MenuItem menuItem)
{
DataView viewItem = new DataView(table);
viewItem.RowFilter = "menu_parent=" + menuItem.Value;
foreach (DataRowView childView in viewItem)
{
MenuItem childItem = new MenuItem(childView["item"].ToString(), childView["sno"].ToString());
//childItem.NavigateUrl = childView["menu_url"].ToString();
for (int i = 0; i<parents.Length; i++)
{
if (Convert.ToInt32(childView["sno"].ToString()) == parents[i])
{
childItem.Selectable = false;
}
}
menuItem.ChildItems.Add(childItem);
AddChildItems(table, childItem);
}
}
Can this process be more faster and reliable with some other code implementation?

Database driven menu will surely show some lag in loading it for the first time and it depends on the speed of your web server too. However, the following code will help you in reducing the load time:
DataRow[] drowpar = table.Select("menu_parent = " + 0);
foreach (DataRow row in drowpar)
{
MenuItem menuItem = new MenuItem(row["item"].ToString(), row["sno"].ToString());
NavigationMenu.Items.Add(menuItem);
AddChildItems(table, menuItem);
}
private void AddChildItems(DataTable table, MenuItem menuItem)
{
DataView viewItem = new DataView(table);
viewItem.RowFilter = "menu_parent=" + menuItem.Value;
foreach (DataRowView childView in viewItem)
{
MenuItem childItem = new MenuItem(childView["item"].ToString(), childView["sno"].ToString());
menuItem.Selectable = false;
menuItem.ChildItems.Add(childItem);
AddChildItems(table, childItem);
}
}
If you are doing other tasks on page_load, make sure you write the menu code at beginning so that server starts the processing with menu and then works on other code.
Hope this helps.

Related

can't add all the selected rows to the dataset

I have added records chosen (checkbox) in a datatable shown below, records have been displaying in a datagrid populated using stored procedure. Then on the button click I save all the selected (added) records to the ds in database.
public DataSet getMain()
{
DataSet ds = CFObj.GetSchemaRequest();
DataRow dr;
dr = ds.Tables[0].NewRow();
for (int i = 0; i < dgAvailableCandidates.Items.Count; i++)
{
CheckBox saveChkBoxItem = (CheckBox)dgAvailableCandidates.Items[i].FindControl("chkbox_SelectCandidate");
if (saveChkBoxItem.Checked)
{
//ds.Tables[0].Columns["pk_scrndRecId"].ReadOnly = false;
dr["pk_scrndRecId"] = "0";
dr["fk_jobId"] = ddl_jobList.SelectedValue.ToString();
obj._fkjob_Id = ddl_jobList.SelectedValue.ToString();
dr["fk_recId"] = dgAvailableCandidates.DataKeys[i].ToString();
obj._fk_recId = dgAvailableCandidates.DataKeys[i].ToString();
}
}
ds.Tables[0].Rows.Add(dr);
return ds;
}
here my issue is when ds.Tables[0].Rows.Add(dr); i put outside of loop, it adds only one last selected record to ds, and when I put this inside loop, it throws following exception
[pk_scrndRecId] column is read only
and when I have placed this inside if block, it throws following exception:
This row already belongs to this table.
I am not able to understand why this code doesn't add all the selected rows of datagrid to the ds.
please help. thanks!
You have to create a new row and add it to datatable inside the loop and so each time it will create a new instance and add it to your datatable.
Check updated code.
public DataSet getMain()
{
DataSet ds = CFObj.GetSchemaRequest();
DataRow dr;
for (int i = 0; i < dgAvailableCandidates.Items.Count; i++)
{
CheckBox saveChkBoxItem = (CheckBox)dgAvailableCandidates.Items[i].FindControl("chkbox_SelectCandidate");
if (saveChkBoxItem.Checked)
{
dr = ds.Tables[0].NewRow();
//ds.Tables[0].Columns["pk_scrndRecId"].ReadOnly = false;
dr["pk_scrndRecId"] = "0";
dr["fk_jobId"] = ddl_jobList.SelectedValue.ToString();
obj._fkjob_Id = ddl_jobList.SelectedValue.ToString();
dr["fk_recId"] = dgAvailableCandidates.DataKeys[i].ToString();
obj._fk_recId = dgAvailableCandidates.DataKeys[i].ToString();
ds.Tables[0].Rows.Add(dr);
}
}
return ds;
}

Build Treeview with parent, child tables

I am building treeview using asp.net 2.0/3.5 from master detail table.
ParentTable Value
---ChildTable Value
---GrandChildTable1 Node
---GrandChildTable1 Value
---GrandChildTable2 Node
---GrandChildTable1 Value
I have created something like this to populate node values. But I am not sure how to display GrandChildNode and values. Could you please let me know if there are any ideas?
Thank you for any help.
protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{
switch (e.Node.Depth)
{
case 0:
GetParentTableValues(e.Node);
break;
case 1:
GetChildTableValues(e.Node);
break;
case 2:
}
}
Here is my solution:
string NodeText = string.Empty;
string NodeKey = string.Empty;
TreeRolesList.Nodes.Clear();
DataTable dtEnqRoles = bllENQRolesMaster.GetENQRolesMasterByRolesDetail();
if (dtEnqRoles.Rows.Count > 0)
{
for (int i = 0; i < dtEnqRoles.Rows.Count; )
{
NodeText = dtEnqRoles.Rows[i]["RoleDescriptionMaster"].ToString().Trim();
NodeKey = dtEnqRoles.Rows[i]["RoleMasterID"].ToString();
TreeNode pNode = new TreeNode(NodeText, NodeKey);
TreeRolesList.Nodes.Add(pNode);
foreach (DataRow dr in dtEnqRoles.Select("RoleMasterID = " + NodeKey))
{
TreeNode childNode = new TreeNode(dr["RoleDescriptionDetail"].ToString().Trim(), dr["RoleDetailID"].ToString());
pNode.ChildNodes.Add(childNode);
i++; //incrementing the outer loop here
}
}
TreeRolesList.ExpandAll();
}

Bind a multi-dimensional ArrayList to a Gridview

I have a DataGrid of seats available, each with a checkbox to be able to reserve the seat. In the button click event, if the CheckBox is clicked, I am adding the contents of the row to an ArrayList, then adding the ArrayList to a session before redirecting to the confirmation page:
protected void Reserve_Click(object sender, EventArgs e)
{
{
ArrayList seatingArreaList = new ArrayList();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
Guid SeatId = (Guid)GridView1.DataKeys[i][0];
CheckBox cbReserve = (CheckBox)GridView1.Rows[i].FindControl("cbReserve");
Label lblSection = (Label)GridView1.Rows[i].FindControl("lblSection");
Label lblRow = (Label)GridView1.Rows[i].FindControl("lblRow");
Label lblPrice = (Label)GridView1.Rows[i].FindControl("lblPrice");
if (cbReserve.Checked)
{
string tempRowInfo = lblSection.Text + "|" + lblRow.Text + "|" + lblPrice.Text;
seatingArreaList.Add(tempRowInfo);
}
}
// Add the selected seats to a session
Session["Seating"] = seatingArreaList;
}
Response.Redirect("Confirm.aspx?concertId=" + Request.QueryString["concertId"]);
}
On the confirmation page, Id like to split this array up and bind it to another gridview in their individual columns.
On the confirmation page, a session exists that has three columns separated with a pipe, I am struggling to split this up and bind it to a confirmation grid.
Please help!
This would probably be easier to just create a DataTable, then add it to the session variable. Once redirected to the confirmation page just bind GridView to the DataTable pulled from the session variable.
protected void Reserve_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("Section");
dt.Columns.Add("Row");
dt.Columns.Add("Price");
{
ArrayList seatingArreaList = new ArrayList();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
Guid SeatId = (Guid)GridView1.DataKeys[i][0];
CheckBox cbReserve = (CheckBox)GridView1.Rows[i].FindControl("cbReserve");
Label lblSection = (Label)GridView1.Rows[i].FindControl("lblSection");
Label lblRow = (Label)GridView1.Rows[i].FindControl("lblRow");
Label lblPrice = (Label)GridView1.Rows[i].FindControl("lblPrice");
if (cbReserve.Checked)
{
DataRow dr = dt.NewRow();
dr["Section"] = lblSection.Text;
dr["Row"] = lblRow.Text;
dr["Price"] = lblPrice.Text;
dt.Rows.Add(dr);
}
}
// Add the selected seats to a session
Session["Seating"] = dt;
}
Response.Redirect("Confirm.aspx?concertId=" + Request.QueryString["concertId"]);
}
var q = from dto in seatingArreaList
let z = dto.Split("|".ToCharArray())
select z;
and then just bing q to the grid.

Dynamically add checkboxlist into placeholder and get the checked value of the checkboxlist

I am creating an admin Page, in which checkboxlist(User list from DB)is to be created dynamically and its the checked users value is retrieved.
There are different types of users, so it is distinguised groupwise.
Now first a panel is defined, the checkboxlist is created dynamically and placed inside the panel, then the panel is placed inside a Placeholder.
What Iam doing here is placing the checkboxlist inside the panel, then the panel inside the placeholder. So the values of the Checkboxlist is not retrieved, due to the panel it doesnt get the checkboxlist and it doesn't loop through the Checkboxlist.
What I have done is.
private void AddControl(string pUserGrp, int pUserGrp_Id, int pName)
{
CheckBoxList chkList = new CheckBoxList();
CheckBox chk = new CheckBox();
User us = new User();
us.OrderBy = "Order By User_Name";
us.WhereClause = "Where UserRole_Id = " + pUserGrp_Id ;
chkList.ID = "ChkUser" + pName ;
chkList.AutoPostBack = true;
chkList.Attributes.Add("onClick", "getVal(ChkUser" + pName + ");");
chkList.RepeatColumns = 6;
chkList.DataSource = us.GetUserDS();
chkList.DataTextField = "User_Name";
chkList.DataValueField = "User_Id";
chkList.DataBind();
chkList.Attributes.Add("onClick", "getVal(this);");
Panel pUser = new Panel();
if (pUserGrp != "")
{
pUser.GroupingText = pUserGrp ;
chk.Text = pUserGrp;
}
else
{
pUser.GroupingText = "Non Assigned Group";
chk.Text = "Non Assigned group";
}
pUser.Controls.Add(chk);
pUser.Controls.Add(chkList);
Place.Controls.Add(pUser);
}
private void setChecked(int pPageGroupId)
{
ArrayList arr = new ArrayList();
PageMaster obj = new PageMaster();
obj.WhereClause = " Where PageGroup_Id = " + pPageGroupId;
arr = obj.GetPageGroupUserRights(null);
CheckBoxList chkList = (CheckBoxList)Place.FindControl("ChkUser");
if (chkList != null)
{
for (int i = 0; i < chkList.Items.Count; i++)
{
if (arr.Count > 0)
{
int ii = 0;
while (ii < arr.Count)
{
PageMaster oCand = (PageMaster)arr[ii];
if (oCand.User_Name == chkList.Items[i].Text)
{
if (!chkList.Items[i].Selected)
{
chkList.Items[i].Selected = true;
}
}
ii++;
oCand = null;
}
}
}
}
}
public string GetListCheckBoxText()
{
StringBuilder sbtext = new StringBuilder();
foreach (Control c in Place.Controls)
{
if (c.GetType().Name == "CheckBoxList")
{
CheckBoxList cbx1 = (CheckBoxList)c;
foreach (ListItem li in cbx1.Items)
{
if (li.Selected == true)
{
sbtext.Append(",");
sbtext.Append(li.Value);
}
else
{
sbtext.Append(li.Value);
}
}
}
}
return sbtext.ToString(); }
It doesnt get through the Checkboxlist control in the setChecked(), also doesnt loop through the GetListCheckBoxTest().
Anyone can plz help me.
Regards
The problem is that you are trying to find a control (in setChecked) without setting the Name property. You are using this:
CheckBoxList chkList = (CheckBoxList)Place.FindControl("ChkUser");
But where is this in AddControl?
chkList.Name = "ChkUser";
And in GetListCheckBoxText instead of...
if (c.GetType().Name == "CheckBoxList")
...use this:
if (c.GetType()== typeof(CheckBoxList))

Gathering Data: Dynamic Text Boxes

Edit: if someone could also suggest a more sensible way to make what I'm trying below to happen, that would also be very appreciated
I'm building an multiPage form that takes a quantity (of product) from a POST method, and displays a form sequence relying on that number. when the user goes to the next page, the form is supposed to collect this information and display it (for confirmation), which will then send this info to a service that will supply URL's to display.
Needless to say, I'm having problems making this work. Here is the relevant parts of my (anonymised) code:
public partial class foo : System.Web.UI.Page
{
Int quantityParam = 3;
ArrayList Users = new ArrayList();
//the information for each user is held in a hashtable the array list will be an array list of the user hashtables
protected void Page_Init(object sender, EventArgs e)
{
if(null != Request["quantity1"])
{
this.quantityParam = Request["quantity1"];
}
}
protected void Page_Load(object sender, EventArgs e)
{
int quantity = this.quantityParam;
if(quantity < 1){ mviewThankYou.SetActiveView(View4Error);}
else
{ //create a form for each user
mviewThankYou.SetActiveView(View1EnterUsers);
for(int user = 0;user < quantity; user++)
{
createUserForm(user);
}
}
}
protected void BtnNext1_Click(object sender, EventArgs e)
{
if(Page.IsValid)
{
for(int i = 0; i < quantity; i++)
{
String ctrlName = "txtUser" + i.ToString();
String ctrlEmail = "txtEmail" + i.ToString();
TextBox name = (TextBox)FindControl(ctrlName);
TextBox email = (TextBox)FindControl(ctrlEmail);
/*BONUS QUESTION: How can I add the Hashtables to the Users Array without them being destroyed when I leave the function scope?
this is where the failure occurs:
System.NullReferenceException: Object reference not set to an instance of an object. on: "tempUser.Add("name",name.Text);
*/
Hashtable tempUser = new Hashtable();
tempUser.Add("name",name.Text);
tempUser.Add("email",email.Text);
this.Users.Add(tempUser);
}
for(int i = 0; i < quantity; i++)
{
v2Content.Text +="<table><tr><td>Name: </td><td>"+
((Hashtable)Users[i])["name"]+
"</td></tr><tr><td>Email:</td><td>"+
((Hashtable)Users[i])["email"]+
"</td></tr></table>";
}
mviewThankYou.SetActiveView(View2Confirm);
}
}
private void createUserForm(int userNum){
DataTable objDT = new DataTable();
int rows = 2;
int cols = 2;
//create the title row..
TableRow title = new TableRow();
TableCell titleCell = new TableCell();
formTable.Rows.Add(title);
Label lblUser = new Label();
lblUser.Text = "<b>User "+ (userNum+1) + "</b>";
lblUser.ID = "lblTitle"+ userNum;
titleCell.Controls.Add(lblUser);
title.Cells.Add(titleCell);
for(int i = 0; i < rows; i++)
{
TableRow tRow = new TableRow();
formTable.Rows.Add(tRow);
for(int j = 0; j < cols; j++)
{
TableCell tCell = new TableCell();
if(j == 0){
Label lblTitle = new Label();
if(i == 0){
lblTitle.Text = "User Name:";
lblTitle.ID = "lblUser" + userNum;
}
else{
lblTitle.Text = "User Email:";
lblTitle.ID = "lblEmail" + userNum;
}
tCell.Controls.Add(lblTitle);
} else {
TextBox txt = new TextBox();
if(i==0){
txt.ID = "txtUser" + userNum;
}
else{
txt.ID = "txtEmail" + userNum;
}
RequiredFieldValidator val = new RequiredFieldValidator();
val.ID = txt.ID + "Validator";
val.ControlToValidate = txt.UniqueID;
val.ErrorMessage = "(required)";
tCell.Controls.Add(txt);
tCell.Controls.Add(val);
}
tRow.Cells.Add(tCell);
}//for(j)
}//for(i)
//create a blank row...
TableRow blank = new TableRow();
TableCell blankCell = new TableCell();
formTable.Rows.Add(blank);
Label blankLabel = new Label();
blankLabel.Text = " ";
blankLabel.ID = "blank" + userNum;
blankCell.Controls.Add(blankLabel);
blank.Cells.Add(blankCell);
}//CreateUserForm(int)
Sorry for the gnarly amount of (amateur code). What I suspect if failing is that FindControl() is not working, but I can't figure out why...
if any help can be given, I'd be very greatful.
Edit: showing the error might help:
Error (Line 112)
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 111: Hashtable tempUser = new Hashtable();
Line 112: tempUser.Add("name",name.Text);
Line 113: tempUser.Add("email",email.Text);
Line 114: this.Users.Add(tempUser);
You problem comes in the fact you are reloading the form every time in Page_Load. Make sure you only load the dynamic text boxes once and you will be able to find them when you need them for confirmation. As long as Page_Load rebuilds, you will not find the answer, and risk not finding anything.
I figured it out:
FindControl() works as a direct search of the children of the control it's called on.
when I was calling it, it was (automatically) Page.FindControl() I had nested the table creation inside a field and a Table control
when I called tableID.FindControl() it found the controls just as it should.
Thanks for the help, Gregory, and for all the comments everyone.
-Matt

Resources