Display dynamic data using GridView/DataList - asp.net

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.

Related

How to display the data based on page refresh in 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.

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()`
}

Need help removing this fudged code... Binding user control on postback

As per the title, I know the code I've posted below is utter poo, this is why I need your help!
I've put way too many hours into this, and it's either down to inexperience, a bug or I've screwed up somewhere.
I have a user control with a view properties that access the ViewState and two user controls within that display the properties.
Within the page_load of the user control, depending on the value of some of the properties, it will toggle the visibility of the controls within:
public partial class PatientStatus : System.Web.UI.UserControl
{
public string PatientName { get { return ViewState["PatientName"] as string; } set { ViewState["PatientName"] = value; } }
public bool ClinicianView { get { return Convert.ToBoolean(ViewState["ClinicianView"]); } set { ViewState["ClinicianView"] = value; } }
public string RangeTitle { get { return ViewState["RangeTitle"] as string; } set { ViewState["RangeTitle"] = value; } }
public int? RangeLimitNormSys { get { return ViewState["RangeLimitNormSys"] as int?; } set { ViewState["RangeLimitNormSys"] = value; } }
public int? RangeLimitNormDia { get { return ViewState["RangeLimitNormDia"] as int?; } set { ViewState["RangeLimitNormDia"] = value; } }
protected void Page_Load(object sender, EventArgs e)
{
bool ispostback = IsPostBack;
if (ispostback && ((System.Web.UI.WebControls.Repeater)(this.Parent.Parent)).DataSource != null)
{
object itm = ((RepeaterItem)this.Parent).DataItem;
if (itm is AppointmentRow)
{
AppointmentRow row = itm as AppointmentRow;
PatientName = row.Name;
RangeTitle = row.Range;
RangeLimitNormDia = row.RangeLimitNormDia;
RangeLimitNormSys = row.RangeLimitNormSys;
ispostback = false;
}
else if (itm is ReadingRow)
{
ReadingRow row = itm as ReadingRow;
PatientName = row.Name;
RangeTitle = row.Range;
RangeLimitNormDia = row.RangeLimitNormDia;
RangeLimitNormSys = row.RangeLimitNormSys;
ispostback = false;
}
else if (itm is PatientRow)
{
PatientRow row = itm as PatientRow;
PatientName = row.Name;
RangeTitle = row.Range;
RangeLimitNormDia = row.RangeLimitNormDia;
RangeLimitNormSys = row.RangeLimitNormSys;
ispostback = false;
}
}
if (!ispostback)
{
if (!string.IsNullOrWhiteSpace(RangeTitle))
{
placeHolder.Visible = true;
literalNA.Visible = false;
}
}
}
}
Previously the Page_Load event simply contained:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (!string.IsNullOrWhiteSpace(RangeTitle))
{
placeHolder.Visible = true;
literalNA.Visible = false;
}
}
}
But on postback, the RangeTitle property was always null, so even when removing the isPostBack statement, it didn't work property.
The only way to resolve it, was to really fudge it by the first block of code.
On the Page_Load of the parent page, and on !isPostBack I'm calling a method that gets and binds data to the repeater. This works fine. But on a postback, i'm calling the same method and for some reason, the user control isn't populating.
Any idea's?
Merry Christmas
Gav
Edit
In response to #jwiscarson
I have a table that is generated via an ASP:Repeater and within the ItemTemplate, I have added a User Control which contains a PlaceHolder and a Literal. Also within the User Control is a number of Properties.
Then on databind I pass across a number of values to the user control (I've tried both OnItemDataBound and inline using Eval). Then on the User Control's Page_Load event, as per the second block of code above, I first check if it's a postback, if not, I then check to see if the Property RangeTitle has a value.
If RangeTitle does not have a value, I then hide the placeholder that contains HTML that would display the RangeTitle and show a literal that displays N/A.
When loading the page for the first time, (!isPostBack) it works fine. But as soon as I create a postback, the User Controls within the repeater all revert to N/A even when their RangeTitle properties had a value.
On debugging, I set a breakpoint in the Page_Load of the User Control. When I first load the page, I can see that my properties have been populated correctly. Then on postback, Page_Load is called on the UserControl and the properties are populated correctly, then Page_Load is called again, but this time, the properties are empty.
i.e.
!isPostBack
UserControl::Page_Load < Correct data
isPostBack
UserControl::Page_Load < Correct data
myButton_Click (bind new data)
UserControl::Page_Load < No data
To make things even more confusing. The method called within myButton_Click to bind the data, is the exact same method called in the Page_Load of the Page to populate the repeater on !isPostBack
Thanks ;)
I would check to make sure that DataItem is accessible in this function. I think you need to listen to the ItemDataBound event and perform this work in that event.
Beyond that, it's difficult to suggest anything else concrete. I don't really understand why you're doing what you're doing (if you have this information in a Repeater, why does it also need to be in ViewState?). If you could explain your rationale for doing this, it might help me and anyone else who visits this question. You say that you're just trying to show/hide some specific items on the page. This is pretty complicated code without a lot of justification for that complication.
As an aside: you really, really need to break this code down and think about what you're trying to accomplish. Here are a few suggestions:
Separate the scopes inside your if and else if statements into functions that return the data you need.
Do not include lines like ((System.Web.UI.WebControls.Repeater)(this.Parent.Parent)).DataSource != null in an if statement. Perform this cast separately and store it in a variable, or write a small function that checks this.
Statements like this.Parent.Parent and other references to Parent controls are code smells, in my opinion. Even on a normal page, this would be a code smell, but what exactly is this.Parent going to reference when you include it in a UserControl?

ASP.Net Custom Control

I am developing a custom control that needs to display a dropdownlist as a composite control.
The drop down list gets populated from a Rest web service. The problem I am facing is that the dropdownlist only has DataTextField and DataValueField but I need a way of storing more values in the control i.e. I have a couple of other properties I need to access for the selected item.
What is the best way of going about this?
Here is the code I have so far:
[ValidationProperty("SelectedValue")]
public class SelectSurveyControl : Panel
{
private DropDownList ddlSurveys;
public string SelectedSurveyId
{
get
{
return SelectedValue;
}
}
public string SelectedSurveyJavascriptEmbedCode
{
get
{
return this.ddlSurveys.SelectedItem.Attributes[""];
}
}
public string SelectedValue
{
get
{
return ddlSurveys.SelectedValue;
}
set
{
if (ddlSurveys == null)
{
ddlSurveys = new DropDownList();
}
ddlSurveys.SelectedValue = value;
}
}
protected override void OnLoad(EventArgs e)
{
base.OnInit(e);
if (ddlSurveys == null)
{
ddlSurveys = new DropDownList();
}
IList<Survey> surveys = GetSurveys();
this.ddlSurveys.DataSource = surveys;
this.ddlSurveys.DataTextField = "title";
this.ddlSurveys.DataValueField = "id";
this.ddlSurveys.DataBind();
ddlSurveys.SelectedValue = this.SelectedValue;
ddlSurveys.CssClass = "umbEditorTextFieldMultiple charlimit";
ddlSurveys.Attributes.Add("SurveyId", SelectedSurveyId);
ddlSurveys.Attributes.Add("JavascriptEmbedingCode", SelectedSurveyId);
this.Controls.Add(ddlSurveys);
}
public IList<Survey> GetSurveys()
{
...
}
}
Try using a string join/split to store and retrieve the various values, then you don't have to customize your dropdown list very much.
For Example:
Text: Some Title
Value: 1|testing test|2/12/2010
This will let you store as many values as you want, so long as you choose an appropriate character to join and split on. I usually use the bar, as in my example above.
Side Note: I was looking at your selected value set handler and it needs some tweaking. You shouldn't check for a null drop down list, instead you should call EnsureChildControls() before each get and set instead. Make sure you override the CreateChildControls() method and create your controls there.
You could use a hidden field and iterate thru a copy of the returned Surveys like this:
foreach(Survey s in Surveys){
string val = s.id + ":" + s.<property1> + ":" + s.<property2>;
hiddenField.Value += val +",";
}
When you need to read from the hidden field, you use String.Split to separate the values into arrays using ',' as the separator and in each array, you split again using ':'.
In the first split Array1[0] who be the survey id and Array1[n!=0] would be the properties of the Survey with the id = Array1[0]. Array[n!=0] would then be split into Array2.
I would suggest handling empty property values with an empty string or something or else you might end up with unequal lengths especially if you specify StringSplitOptions.RemoveEmptyEntries.
Agricfowl

Editable GridView that stores changes in viewstate until save

I know how to have an editable GridView along with a SqlDataSource in which each edit (update/insert/delete) is immediately persisted to the database (using the SqlDataSource's UpdateCommand, Insertcommand, etc).
What I need now is to have an editable GridView that maintains all edits in viewstate until the user presses a "Save" button elsewhere on the form.
In other words:
On first load, populate the GridView from DB data
User makes various edits to the data, which are not persisted to the DB yet, but which survive through any number of postbacks.
User presses Save, and all the changes are persisted to the DB
I assume I'll need to write custom code to persist the data in step 3, but is there a straightforward, out-of-the-box approach to step 2?
You want to use a DataSet or DataTable and use the following:
myDataSet.AcceptChanges();
This commits the changes when you call that method on the DataSet, DataTable, or DataRow. Think of this almost like a SqlTransaction where you need to commit or rollback. Hope this helps!
see link: Accept Changes
I can suggest to do the following:
1) create a custom list object that stores your data. Upload DB data to that object and save it to session state.
Object:
public class InvestorClaim
{
public InvestorClaim()
{
}
private int? _record_id;
private int? _ic_record_id;
private Int64? _lh_record_id;
public int? record_id
{
get { return _record_id; }
set { _record_id = value; }
}
public int? ic_record_id
{
get { return _ic_record_id; }
set { _ic_record_id = value; }
}
public Int64? lh_record_id
{
get { return _lh_record_id; }
set { _lh_record_id = value; }
}
}
Upload data to the list :
List<InvestorClaim> inv_claim = new List<InvestorClaim>();
inv_clai= dataFromDB
Save to session :
HttpContext.Current.Session[ "InvestorClaimsObject" ] = inv_claim;
2) bind gridview to the object and manipulate data as you need.
protected void yourGridView_Bind()
{
inv_claim = HttpContext.Current.Session[ "InvestorClaimsObject" ] as List<InvestorClaim>;
yourGridView.DataSource = inv_claim;
BindData();
}
protected void yourGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
inv_claim = HttpContext.Current.Session[ "InvestorClaimsObject" ] as List<InvestorClaim>;
//Update the values.
GridViewRow row = yourGridView.Rows[e.RowIndex];
inv_claim[row.DataItemIndex].lh_record_id = ((TextBox)(row.Cells[1].Controls[0])).Text;
inv_claim[row.DataItemIndex].ic_record_id = ((TextBox)(row.Cells[2].Controls[0])).Text;
//Reset the edit index.
yourGridView.EditIndex = -1;
//Bind data to the GridView control.
yourGridView.DataSource = inv_claim;
BindData();
}
3) save data from session list object to DB when ready.

Resources