How to display the data based on page refresh in asp.net - asp.net

I am trying to build an application in asp.net which displays the real time data .Assume this application as application which you typicaly see in DMV where it dsiplays which person has to go for which counter and what is the status.
what i currently have ?
1) data displaying from database and getting the real time data
2) I am refreshing the page every 5 seconds
what i am trying to acheive?
1) Lest say i have 20 rows/persons . I want to display only 10 rows first and on page refresh next 10 rows and again on next page refresh go back to 1st 10 rows.
protected void Page_Load(object sender, EventArgs e)
{
BindQueueGrid();
GetScrollInfo();
gvwCourtRoomStatusInfo.Attributes.CssStyle.Add("font-size", "20px");
}
private void BindQueueGrid()
{
List<TestClass> mydata = qService.GetData();
Grid.DataSource = mydata;
Grid.DataBind();
}

You could use a ViewState variable and switch it on each page load. Load the first 10 results on the first load, then on the refresh check the variable and load the second set of results. Mix in a little linq to get your query working.
private void BindQueueGrid()
{
List<TestClass> mydata = qService.GetData();
List<TestClass> onlyTen = new List<TestClass>();
if(ViewState["check"] == null)
{
onlyTen = mydata.Take(10).ToList();
Grid.DataSource = onlyTen;
ViewState["check"] == true;
}
else
{
onlyTen = mydata.Skip(10).Take(10).ToList();
Grid.DataSource = onlyTen;
ViewState["check"] == null;
}
Grid.DataBind();
}
You may be able to do it without the second list but I have not tested the code.

Related

ASPxComboBox Filtering Large DataSet Using XpoDataSource (DevExpress)

I tried to Filter an ASPxComboBox that gets data using an XpoDataSource, note that restoring and filtering data from a small data set works fine , the issue start when I try to filter large dataset - about 70000 records- from datasource the ComboBox loading becomes very slow since XpoDataSource gets all data from database table .
So I created a criteria for the XpoDataSource to reduce number of records restored ,then the ComboBox Keeps repeating the top 10 records while scrolling down the ComboBox, I don't know where the problem is.
I realized that what I need is similar to the example in the following link
But using an XpoDataSource instead of SqlDataSource1 .
I don't know how to write a similar code for an XpoDataSource .
this is my code :
protected void cmbServices_OnItemRequestedByValue_SQL(object source, DevExpress.Web.ASPxEditors.ListEditItemRequestedByValueEventArgs e)
{
try
{
string criteria = "";
if (string.IsNullOrEmpty(e.Value.ToString()) || e.Value.ToString().Length < 3)
{
criteria = "1 = 2";
}
else
{
criteria =
string.Format("(( Code like '{0}%' OR ProductName like '{0}%') AND CustomerId = {1})", e.Value.ToString(), (cmbServicesActivities != null && cmbServicesActivities.Value != null) ? cmbServicesActivities.Value.ToString() : "0");
}
dsServices.Session = LookupsSession;
dsServices.Criteria = criteria;
cmbServicesDescription.DataSource = dsServices;
cmbServicesDescription.DataBind();
}
catch (Exception exc)
{
Debug.WriteLine(exc.Message);
}
}
The Following Example demonstrates the answer of my question
public partial class _Default : System.Web.UI.Page {
Session session = XpoHelper.GetNewSession();
protected void cmb_ItemRequestedByValue(object source, DevExpress.Web.ASPxEditors.ListEditItemRequestedByValueEventArgs e) {
MyObject obj = session.GetObjectByKey<MyObject>(e.Value);
if (obj != null) {
cmb.DataSource = new MyObject[] { obj };
cmb.DataBindItems();
}
}
protected void cmb_ItemsRequestedByFilterCondition(object source, DevExpress.Web.ASPxEditors.ListEditItemsRequestedByFilterConditionEventArgs e) {
XPCollection<MyObject> collection = new XPCollection<MyObject>(session);
collection.SkipReturnedObjects = e.BeginIndex;
collection.TopReturnedObjects = e.EndIndex - e.BeginIndex + 1;
collection.Criteria = new BinaryOperator("Title", String.Format("%{0}%", e.Filter), BinaryOperatorType.Like);
collection.Sorting.Add(new SortProperty("Oid", DevExpress.Xpo.DB.SortingDirection.Ascending));
cmb.DataSource = collection;
cmb.DataBindItems();
}

Create a newEventHandler for a button. But Data fields are empty for some reason

I create a new event Handler to handle two different events. One is for saving a new document. The other is for saving an edit.
I added this in my Page_load:
if (Request.QueryString["ExhibitID"] != null)//new
{
if (!IsPostBack)
{
ddlCaseFiles.DataSourceID = "dsCaseFiles";
ddlCaseFiles.DataTextField = "Display";
ddlCaseFiles.DataValueField = "FileID";
rbByFileID.Checked = true;
rbMyFiles.Checked = false;
ddlCaseFiles.DataBind();
editExhibit(int.Parse(Request.QueryString["ExhibitID"]));//new
exhibitHeader.InnerText = "Edit Exhibit";
}
hidSavedExhibitID.Value = Request.QueryString["ExhibitID"];
saveExhibitBtn.Click += new EventHandler(this.btnUpdateExhibit_Click);
}
else
{
saveExhibitBtn.Click += new EventHandler(this.saveExhibitBtn_Click);
}
my save method for some reason keeps looping then crashing because the second time it goes through, there is no data since I reset it after the first save. I have no idea why it is running my save method twice.
this is my save method :
protected void saveExhibitBtn_Click(object sender, EventArgs e)
{
hidSavedExhibitID.Value = null;
int newExhibitID = saveExhibit();
int propertyID = autoCreateProperty(newExhibitID);
linkExhibitAndProperty(newExhibitID, propertyID);
SaveInfoIntoSessionVariables();
ClearFormFields();
}
the "saveExhibit()" method is where I actually access the DB and store everything. It works fine.
Because you re bind your datas in your Page_Load.
You must persist your datas with ViewState, EnableViewState="true"
You bind your datas just one time, in the ! IsPostBack. in order to not erase the selected values
If(! IsPostBack)
{
//Bind your datas with `DataBind()`
}

Selected value in asp.net dropdown in a repeater not returned

I've currently go a very strange problem. I'm using an asp.net wizard to upload some files. The files are uploaded using plupload. After the files have uploaded I have a list of the upload files stored in a session variable. I use the session variable to create a table showing the upload files. The user now has option to set a file category using a dropdown in the table. When the user clicks 'finish' button the code reads the list of files and the category from the table. The odd thing is this code works fine on my development machine and on several servers but on a particular clients server the drop down value always returns as null. Here is the relevent code:
protected void Page_Init(object sender, EventArgs e)
{
bindRepeater();
}
private void bindRepeater()
{
ArrayList sessionFiles = (ArrayList)Session["PLUploadFiles"];
IList<document> files = new List<document>();
foreach (string fileName in sessionFiles)
{
document doc = new document();
doc.FileName = fileName;
doc.Description = fileName.Split('.').First();
files.Add(doc);
}
TableRepeater.DataSource = files;
TableRepeater.DataBind();
}
protected void SaveButton_Click(object sender, EventArgs e)
{
foreach (RepeaterItem item in TableRepeater.Items)
{
Label descriptionLabel = (Label) item.FindControl("DescriptionLabel");
String description = descriptionLabel.Text;
Label fileNameLabel = (Label)item.FindControl("FileNameLabel");
String fileName = fileNameLabel.Text;
DropDownList categoryDropDown = (DropDownList) item.FindControl("CategoryDropDownList");
string category = categoryDropDown.SelectedValue;
if(SaveClicked != null)
{
SaveEventArgs s = new SaveEventArgs();
s.FileName = fileName;
s.Category = category;
s.Description = description;
SaveClicked(this, s);
}
}
Response.Redirect(RedirectURL);
}
Note that the entire wizard lives on a usercontrol. Has anybody got any idea why this code works fine on most machines but fails on one particular server?
Looks like I've fixed it. For some reason I was seeing a double postback. This was calling my code to reset the session variables in Page_Load at the wrong time. The work around is to reset the session variables in the page that links to the upload page so they are reset before the page is loaded. I've no idea why I'm seeing the double postback.

Can't submit changes to database or page controls

I've got a pretty simple page, consisting of two DropDownLists populated from the database, and a button. The purpose of the page is pretty simply to allow users to delete an entry from the database. When the button is clicked then a simple LINQ query is executed to delete the intended target, and remove the entry from the dropdownlists, but it doesn't work unless the response is redirected within that function, even if SubmitChanges() was called. Why would this happen?
Edit: Code
protected void Page_Init(object sender, EventArgs e)
{
var result = Database.DB.Data.GetTable<Database.tbl_module_>().Where(module => module.deptCode == ((User)Session["user"]).deptCode);
foreach (var row in result)
{
this.listModuleCode.Items.Add(new System.Web.UI.WebControls.ListItem(row.code));
this.listModuleTitle.Items.Add(new System.Web.UI.WebControls.ListItem(row.title));
}
}
protected void Delete_Click(object sender, EventArgs e)
{
var DB = Database.DB.Data;
var table = DB.GetTable<Database.tbl_module_>();
var result = table.Where(module => module.deptCode == ((User)Session["user"]).deptCode && module.code == listModuleCode.SelectedItem.Text);
listModuleCode.Items.Remove(listModuleCode.SelectedItem);
listModuleTitle.Items.Remove(listModuleTitle.SelectedItem);
table.DeleteAllOnSubmit(result);
DB.SubmitChanges();
Response.Redirect("deletemodule.aspx"); // redirect to this page
}
We need to see your code to help more probably. However:
You need to make sure it knows to delete on submit:
var q = db.Customers.Where(c => c.CustomerID == 2).Single();
db.Customers.DeleteOnSubmit(q);
db.SubmitChanges();
Don't forget you can pass straight SQL to the object:
db.ExecuteCommand("DELETE FROM Customers WHERE ID = 2");
Which you might think is easier.

Display dynamic data using GridView/DataList

I need to build a form that shows data is the following format:
M(1 Nov), T(4 Nov), S(7 Nov) etc
user 1 yes/no yes/no yes/no yes/no
user 2 yes/no yes/no yes/no yes/no
The yes/no will be represented by a check box and there will be Monday, Thursday and Sunday columns for each occurrence within the month - dynamically worked out. I was planning on using Anonymous Type's as explained here to represent the data. Is this a sound approach? My problem is how to display this using a GridView or DataList control. If the data structure is dynamic, how can I configure the GridView/DataList control to bind to the data?
Is there a better approach to what I'm trying?
Use a DataTable stored in the ViewState to carry the data and bind to a GridView / DataList.
public DataTable MyDynamicDataTable {
get { return (DataTable)ViewState["MyDynamicDataTable"]; }
set { ViewState["MyDynamicDataTable"] = value; }
}
...
public void Page_Load(object sender, EventArgs e) {
if (!Page.IsPostBack) {
MyDynamicDataTable = BuildMyDynamicDataTable();
MyGridView1.DataSource = MyDynamicDataTable;
MyGridView1.DataBind();
}
}
...
public DataTable BuildMyDynamicDataTable() {
DataTable dt = new DataTable();
foreach (obj in myDayCollection) {
dt.Columns.Add(...);
}
foreach (obj in myCollection) {
dt.Rows.Add(...);
}
return dt;
}
When the user makes changes, it should persist to the DataTable structure. Once the changes are complete and successfully validated, you can simply foreach (DataRow in MyDynamicDataTable) to process the selections that the user made and persist them to database, or whatever.

Resources