Building a status calendar - ASP.NET - asp.net

I need to build a status dashboard that reads Events (ID, Desc, Time) out of a database, and puts a marker on a day that drills down to detail, if there is an event - and just a checkmark if there is no event. Just like http://www.google.com/appsstatus.
I am a Jr. developer witha PHP background, I am using C# & ASP.NET. I chose to use the UpdatePanel/ScriptManager components for the grid even though there is some bandwidth costs.
My question is about the best way to build this table. In PHP, I would use a string and just keep adding HTML to it via nested iteration, like:
foreach($row in $rows){
MyHtml .= "<td>" . $variableFromDB[i] . "</td>";
i++
}
return MyHtml; //Output built HTML
In this case I need to query an array to see if an event exists for that day, if so, generate a link containing URL parameter (field1=value1&field2) so that I can display the corresponding detail information.
Since I'm using .NET, I'm wondering if there is a more elegant way to do this without querying for each cell to check for an event, and an easier way to build a table. I see .NET classes like "HtmlTableCell" & "HtmlTableRow", but I haven't seen any compelling examples that require a query in each cell.
Any guidance from you senior .NET folks would be great...

It sounds like you're wanting a grid or table layout to display your data. If that's the case, and you're using ASP.NET WebForms, consider using a GridView instead of building HTML manually. The advantage of ASP.NET's server controls is the ability to bind a collection of your business objects from your model to your controls.
Suggest you don't follow the HtmlTableCell and HtmlTableRow approach. You can get this job done much quicker by using server controls and data binding.
Here are a few steps in getting started with databinding.
On your .aspx markup page, write an <asp:GridView runat=server id=statusCal>.
Determine the columns that you want to show on your grid. Just like the HTML table you'd have built by hand, what data did you want to show? You'll end up with something like this:
<asp:GridView ID="statusCal" AutoGenerateColumns="false" runat="server">
<Columns>
<asp:BoundField DataField="Date" HeaderText="Status Date" />
<asp:BoundField DataField="Status" HeaderText="Status" />
<asp:BoundField DataField="UserName" HeaderText="User" />
<asp:BoundField DataField="Email" HeaderText="Email" />
</Columns>
</asp:GridView>
Is your data coming from a database? Somehow you'll want a class to hold your status calendar. Wherever you're getting the data from, you'll want to have a class to bind to your grid. Perhaps something like:
public class UserStatusCalendar
{
public string UserName {get;set;}
public string Email {get;set;}
public string Status {get;set;}
public string Date {get;set;}
}
Then it's a matter of:
Creating and populating an array or List<> of your UserStatusCalendar objects.
List<UserStatusCalendar> show = new List<UserStatusCalendar>();
show.Add(new UserStatusCalendar
{ UserName="foo", Email="foo#bar.com",
Status="Good", Date="Jan 1 2010 10:09"});
Bind your collection to the grid.
statusCal.DataSource = show;
statusCal.DataBind();
When navigating to the page, you'll see that your grid shows your data. You won't have had to mess with any lines of HTML markup. You can then dive into fancier tasks for your grid: creating <asp:HyperLink> fields instead of BoundFields, and all the rich easy editing support that the GridView allows you.
Here's a working test application to demonstrate the concepts. Name your page test.aspx.
C# code-behind: http://pastebin.com/DWaxRNmj
ASPX Markup: http://pastebin.com/XwrnGB0D
You'll end up with this grid:

Related

using postbackurl to pass variables without referencing ctl00$MainContent

I'm hoping there's a cleaner way of doing this. My source page markup has some simple inputs and a submit button:
<asp:TextBox runat="server" ID="TBPostDateFrom" placeholder="From" />
<asp:TextBox runat="server" ID="TBPostDateTo" placeholder="Present" />
...
<asp:Button ID="BtnDetailedResults" PostBackUrl="~/Auth/ResultsDetail.aspx" runat="server" Text="View Detailed Results" />
On my target page, I'm trying to reference those controls and use them as datasource select parameters. So far the only way I've found to do that is to use the long asp generated names "ctl00$MainContent$TBPostDateFrom" and "ctl00$MainContent$TBPostDateTo":
SDSDetailedResults.SelectParameters.Add("PDFrom", Request.Form["ctl00$MainContent$TBPostDateFrom"]);
SDSDetailedResults.SelectParameters.Add("PDTo", Request.Form["ctl00$MainContent$TBPostDateTo"]);
Is there a way I can reference those controls without using the long ct100$...? Or a way to reference the controls directly? I'm guessing if sometime down the road I change my master page, or content controls, these references would get messed up.
I've tried adding using adding the ClientIDMode=Static to the inputs like:
<asp:TextBox runat="server" ID="TBPostDateFrom" placeholder="From" ClientIDMode="Static" />
But that appears to only change the ID. On my target page, I'm still unable to reference it without using the ct100$....
I've also tried using the Page.PreviousPage method, but the objects end up empty:
if (Page.PreviousPage != null)
{
//post date
TextBox PostDateFrom = (TextBox)Page.PreviousPage.FindControl("TBPostDateFrom");
TextBox PostDateTo = (TextBox)Page.PreviousPage.FindControl("TBPostDateTo");
//at this point both PostDateFrom and PostDateTo are empty, if I do this:
SDSDetailedResults.SelectParameters.Add("PostDateFrom", PostDateFrom.Text);
SDSDetailedResults.SelectParameters.Add("PostDateTo", PostDateTo.Text);
// I get an IIS error saying the object references dont' exist, or are null
}
}
Thanks in advance, any help or guidance is much appreciated!
For a search page, I would recommend using the QueryString to pass information to your later page, rather than trying to reference the controls from the previous page.
This will be especially useful if you want to use this functionality from different technologies. You won't have to worry about where the request came from.
SearchPage.aspx:
//Button Click:
var page = "ResultsDetail.aspx";
var url = String.Format("{0}?TBPostDateFrom={1}&TBPostDateTo={2}", page, TBPostDateFrom.Text, TBPostDateTo.Text);
Response.Redirect(url);
ResultDetails.aspx:
var from = DateTime.Parse(Request.QueryString["TBPostDateFrom"]);
var to = DateTime.Parse(Request.QueryString["TBPostDateTo"]);
//Do search based on parameters
For more information: MSDN - How to: Pass Values Between ASP.NET Web Pages

turn sql rows into links on a vb.net page?

I am trying to create a specific aspx page where I display clickable links based on information in a sql database. For example one column could be the anchor tag another column could be the path to the link itself etc. In the past I would pull this information from sql and put it into a non-visible Label (say linkLabel1). Then in the page itself I would insert <%linkLabel1.text%> to insert the link path from the database to the appropriate area.
I hope I'm not confusing things too much here and that makes sense how I explained it.
What I would like to do is set up a way that I could simply enter a new row into a SQL table with link information and a web page automatically displays the new link for me.
I guess I am mostly looking for insight, opinions, or directions of what approach to consider. I can elaborate if I was unclear (wouldn't be awfully surprising if I was not).
Thanks in advance for anyone's time on this matter.
Since you are displaying this in a table, you could use a GridView for this. The columns that will display the link could be defined as hyperlink columns as so:
<Columns>
<asp:HyperLinkField
HeaderText="Header text"
DataNavigateUrlFields="PropertyContainingTheHRefForTheAnchor"
DataTextField="PropertyContainingTheTextForTheAnchor"
/>
</Columns>
So for example, if you return a record set containing these columns:
TextProperty PathProperty
See Details Assets/SomeOther/
Click me Products/AnotherPath/
Your grid will render these as:
See Details
Click me
If you define the column as:
<Columns>
<asp:HyperLinkField
HeaderText="Header text"
DataNavigateUrlFields="PathProperty"
DataTextField="TextProperty"
/>
</Columns>

ASP.NET 4 GridView - Pulling a Hyperlink out of a Database

I have a GridView bound to a SqlDataSource.
I'm pulling hyperlinks which point to Job Descriptions stored in a separate web space, out of a database and placing them in the GridView.
These are full Hyperlinks such as "Http://stackoverflow.com/"
Originally the GridView column was a simple BoundField like this:
<asp:BoundField DataField="JobDescription" HeaderText="JobDescription"
SortExpression="JobDescription" />
So I started trying to convert it into a hyperlink field.
<asp:HyperLinkField DataNavigateUrlFields="JobDescription"
DataTextField="JobDescription"
HeaderText="JobDescription"
SortExpression="JobDescription"
Target="_blank"
NavigateUrl="{0}" />
This produced the desired result, but I can no longer edit that column in the GridView. When it was a BoundField I could edit the item, but could find no way to make it into a hyperlink.
Either way will work...
I either need the HyperLinkField to be updatable, or I need the BoundField to be formatted as a Hyperlink with what it pulls directly from the database.
I appreciate the help.
Use a Template Field. So your can define your normal view and editing view.
Grrr found the answer:
<asp:BoundField DataField="JobDescription" HeaderText="Job Description"
SortExpression="JobDescription"
DataFormatString="<a target='_blank' href='{0}'>Text</a>"
HtmlEncode="False" />
You don't need a template field. That HtmlEncode property must be set to false in order for html in DataFormatString to be rendered as html, otherwise it changes all of your characters into the equivalent of stuff like...
The Entity Numbers here: http://www.w3schools.com/tags/ref_entities.asp

ASP.Net master/detail w/Databinding to DataSource objects - multi-insert with all-or-nothing principals

My goal is to allow the user to enter order header and detail data and have it all save with a single button click, in one transaction so the save is all or nothing.
For example:
Tables:
OrderHeader:
Id,
CustomerId,
Comments,
OrderDate,
OrderLine
Id,
OrderId FK => OrderHeader
ProductId,
Quantity
Markup (abbreviated):
<asp:FormView ID="uxFormHeaderInfo" runat="server" DataSourceID="headerDataSource">
<InsertItemTemplate>
<%--Header Fields--%>
.....
<asp:GridView ID="uxDetails" runat="server" DataSourceID="detailsDataSource">
<%-- Markup --%>
.....
</asp:GridView>
<asp:Button ID="uxSave" runat="server" CommandName="Insert" />
</InsertItemTemplate>
</asp:FormView>
In the page i'd have FormView control that will contain the edit fields for CustomerId and Comments, also i'd have a nested gridview or listview that I could add line items to. The FormView and gridview controls would most likely be linked to independent datasource controls. What I want to happen is for the user to enter the header info and the line items and click save just once at the bottom of the screen.
Is this even possible using the LinqDataSource or ObjectDatasource objects? If so could you point me to a good example that shows how to do this in master/detail style insert that hits 2 or more tables with a single button click? Thanks in advance.
[Edit]
After reviewing feedback and searching around on this subject, I've concluded that datasource controls are just the wrong tool for the job. Since they can't really handle a case as pedestrian as this, it really does call into question their usefulness IMHO. So much for the new way...
I don't think this is going to work with a DataSource. At least not that i know of.
I would handle the Save operation myself. In the Click event of your save button read the values the user has entered, encapsulate it in a transaction and call your business functions.

Best way to custom edit records in ASP.NET?

I'm coming from a Rails background and doing some work on a ASP.NET project (not ASP MVC). Newbie question: what's the easiest way to make a custom editor for a table of records?
For example: I have a bunch of data rows and want to change the "category" field on each -- maybe a dropdown, maybe a link, maybe the user types it in.
In Rails, I'd iterate over the rows to build a table, and would have a form for each row. The form would have an input box or dropdown, and submit the data to a controller like "/item/edit/15?category=foo" where 15 was the itemID and the new category was "foo".
I'm new to the ASP.NET model and am not sure of the "right" way to do this -- just the simplest way to get back the new data & save it off. Would I make a custom control and append it to each row? Any help appreciated.
You can REALLY cheat nowadays and take a peek at the new Dynamic Data that comes with .NET 3.5 SP1. Scott Guthrie has a blog entry demoing on how quick and easy it'll flow for you here:
http://weblogs.asp.net/scottgu/archive/2007/12/14/new-asp-net-dynamic-data-support.aspx
Without getting THAT cutting edge, I'd use the XSD generator to generate a strongly typed DataSet that coincides with the table in question. This will also generate the TableAdapter you can use to do all your CRUD statements.
From there, bind it to a DataGrid and leverage all the standard templates/events involved with that, such as EditIndex, SelectedIndex, RowEditing, RowUpdated, etc.
I've been doing this since the early 1.0 days of .NET and this kind of functionality has only gotten more and more streamlined with every update of the Framework.
EDIT: I want to give a quick nod to the Matt Berseth blog as well. I've been following a lot of his stuff for a while now and it is great!
There are a few controls that will do this for you, with varying levels of complexity depending on their relative flexibility.
The traditional way to do this would be the DataGrid control, which gives you a table layout. If you want something with more flexibility in appearance, the DataList and ListView controls also have built-in support for editing, inserting or deleting fields as well.
Check out Matt Berseth's blog for some excellent examples of asp.net controls in action.
Thanks for the answers guys. It looks like customizing the DataGrid is the way to go. For any ASP.NET newbies, here's what I'm doing
<asp:DataGrid ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="RuleID" Visible="False" HeaderText="RuleID"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Category">
<ItemTemplate>
<!-- in case we want to display an image -->
<asp:Literal ID="litImage" runat="server">
</asp:Literal>
<asp:DropDownList ID="categoryListDropdown" runat="server"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
This creates a datagrid. We can then bind it to a data source (DataTable in my case) and use things like
foreach (DataGridItem item in this.GridView1.Items)
{
DropDownList categoryListDropdown = ((DropDownList)item.FindControl("categoryListDropdown"));
categoryListDropdown.Items.AddRange(listItems.ToArray());
}
to populate the intial dropdown in the data grid. You can also access item.Cells[0].text to get the RuleID in this case.
Notes for myself: The ASP.NET model does everything in the codebehind file. At a high level you can always iterate through GridView1.Items to get each row, and item.findControl("ControlID") to query the value stored at each item, such as after pressing an "Update" button.

Resources