Tech Tidbits - Ruby, Ruby On Rails, Merb, .Net, Javascript, jQuery, Ajax, CSS...and other random bits and pieces.

Wednesday, November 21, 2007

ASP.NET Calendar Control - Misc

How to: Display Selected Dates from a Database in the Calendar Control

Hide days that aren't in current month:

protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
if (e.Day.Date.Month != Calendar1.VisibleDate.Month)
e.Cell.Text = "";
}


Disable link for particular day (the 23rd, in this case):

protected void cal_DayRender(object source, DayRenderEventArgs e)
{
if (e.Day.Date.Day == 23)
{
e.Cell.Controls.Clear();
e.Cell.Text = e.Day.DayNumberText;
e.Cell.BackColor = System.Drawing.Color.Gainsboro;
}
}

Tuesday, November 20, 2007

ASP.NET HyperLink Control: mailto link


<asp:HyperLink ID="EmailLink" text='<%# Eval("UserEmail") %>' NavigateUrl='<%# Eval("UserEmail","mailto:{0}")%>' runat="server" target="_blank" />


With a "Subject"


<asp:HyperLink ID="EmailLink" text='<%# Eval("UserEmail") %>' NavigateUrl='<%# Eval("UserEmail","mailto:{0}?Subject=YourQuestion")%>' runat="server" target="_blank" />

Saturday, November 17, 2007

Identify Button Clicked in GridView

I had a GridView with a ButtonField used to Select a record to display a User's details in a DetailsView. I wanted a second button to display different form for changing a Users's password, since I wanted that to be a separate option.

GridView (usersGrid):

<asp:ButtonField CommandName="Select" Text="Select" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton runat="server" ID="changePasswordButton" Text="Change Password" OnClick="usersGrid_SelectRow" />
</ItemTemplate>
</asp:TemplateFild>


Code Behind:

protected void usersGrid_SelectRow(object sender, EventArgs e)
{
GridViewRow row = (GridViewRow)((LinkButton)sender).Parent.Parent;
usersGrid.SelectedIndex = row.RowIndex;

MyDatabase.DBUtil = new MyDatabase.DBUtil();
changePasswordDetails.DataSource = DB.GetUser(Int16.Parse(usersGrid.SelectedValue.ToString());
changePasswordDetails.ChangeMode(DetailsViewMode.ReadOnly);
userDeatils.Visible = false;
changePasswordDetails.Visible = true;
changePasswordDetails.DataKeyNames = new string[] { "UserID" };
changePasswordDetails.DataBind();
}

Friday, November 9, 2007

.NET Smart Delete (Details View Client-side delete confirmation)

I was working on a database-driven web site and I needed a way to make sure that the admin couldn't delete all the admin user accounts (that was simple enough), but I also wanted a javascript confirmation popup when an admin did delete accounts.

I found exactly what I was looking for on Dino Esposito's WebLog on his post ASP.NET Today: Smart Delete:


DetailsView and FormView controls support delete operations and delegate the execution to the underlying data source control. If the data source control is configured to execute the delete operation, all works fine, otherwise an exception is thrown.

The DetailsView generates command buttons automatically and doesn't expose them directly to page code. How can you add a bit of Javascript code to ask for confirmation? Here's the code.


protected void DetailsView1_ItemCreated(object sender, EventArgs e)
{
// Test FooterRow to make sure all rows have been created
if (DetailsView1.FooterRow != null)
{
// The command bar is the last element in the Rows collection
int commandRowIndex = DetailsView1.Rows.Count-1;
DetailsViewRow commandRow = DetailsView1.Rows[commandRowIndex];

// Look for the DELETE button
DataControlFieldCell cell = (DataControlFieldCell) commandRow.Controls[0];
foreach(Control ctl in cell.Controls)
{
LinkButton link = ctl as LinkButton;
if (link != null)
{
if (link.CommandName == "Delete")
{
link.ToolTip = "Click here to delete";
link.OnClientClick = "return confirm('Do you really want to delete this record?');";
}
}
}
}


The ItemCreated event doesn't provide any information about the row being created. However, it can be figured out that the footer row is always created and is always the last row to be created. If the FooterRow object is not null, you can conclude that all rows have been created, including the command bar. The command bar is the first row after the data rows and is stored in the Rows collection--it's the last element in the collection. The command bar is a table row (type is DetailsViewRow) and contains a cell (type is DataControlFieldCell). The cell contains as many link buttons (type is DataControlLinkButton) as there are commands. Delete, Edit, New, Update, Cancel are the command names used and useful to identify the right button.

The FormView is fully templated and lets you manually define appearance and behavior of command buttons. If you place a custom button, or want to use a custom command name for a standard button (Edit, New, Delete), here's how to detect the click on the button. You start by adding a handler for ItemCommand. The code below shows how to deal with a custom Delete button that checks if the bound data source control is enabled for deletion before proceeding.


protected void FormView1_ItemCommand(object sender, FormViewCommandEventArgs e)
{
if (e.CommandName == "SmartDelete")
{
IDataSource obj = (IDataSource) FindControl(FormView1.DataSourceID);
DataSourceView view = obj.GetView("DefaultView");
if (!view.CanDelete) {
Response.Write("Sorry, you can't delete");
return;
}
else
FormView1.DeleteItem();
}
}

Tuesday, November 6, 2007

Formatting Date Field in Grid View

Due to a bug fix in .NET 2.0, HtmlEncode must be set to False for date formatting to give expected results:


<asp:BoundField DataField="AgendaDate" HeaderText="Agenda Date" SortExpression="AgendaDate" DataFormatString="{0:yyyy-MM-dd}" HtmlEncode=False />


In a Details View using TemplateFields:


<ItemTemplate>
  <asp:Label ID="agendaDateLabel" runat="server" Text='<%# Eval("AgendaDate", "{0:yyyy-MM-dd}") %>'></asp:Label>
</ItemTemplate>

Saturday, November 3, 2007

SQL Server 2005 Check Constraint

I wanted to use something like the MySQL Enum constraint in SQL Server to limit the values allowed in a column...

I ended up using a separate table and a foreign key for this, but never the less, here's how to use the Check constraint, in my case, to limit RecordStatus to contain only 'Active' or 'Inactive':


CREATE TABLE NewsItems (
NewsItemID int IDENTITY NOT NULL PRIMARY KEY,
RecordStatus nvarchar(10) NOT NULL DEFAULT 'ACTIVE' CHECK (RecordStatus IN ('Active', 'Inactive'))
);

About Me

My photo
Developer (Ruby on Rails, iOS), musician/composer, Buddhist, HSP, Vegan, Aspie.

Labels