Wednesday, May 31, 2006

Microsoft recently launched a new IIS website. The site is mainly dedicated to IIS 7. It has some pretty cool utilities and lets you try out IIS7 through your browser. It might be worth checking out when you have a chance.

http://www.iis.net/

ASP.NET | IIS
5/31/2006 7:27:29 AM (Eastern Standard Time, UTC-05:00)  #    Disclaimer  |  Comments [0]  |  Trackback
 Wednesday, May 24, 2006

Andy Hume posted an intersting article on bitesizestandards.com . It talks about positioning <divs> located on the source document in any order through css. Normally <divs> order themselves on the page according to the order they are placed on the page.

Here is a link to the article:

http://bitesizestandards.com/bites/understanding-any-order-columns

CSS
5/24/2006 9:34:20 AM (Eastern Standard Time, UTC-05:00)  #    Disclaimer  |  Comments [0]  |  Trackback
 Friday, April 21, 2006

Typical Problem: ASP.NET <asp:login> works fine when tested locally, but doesn't work when deployed.

If you are setting up an ASP.NET application for deployment it's best to explicitly define the applicationName property on your membership provider - otherwise when you go to deploy the application you might run into problems.

The applicationName property in the web.config has to match the application path or application name of the web application - the paths are generally going to be different between your test and production environments.

So, the best way to make sure web.config stays correct for test and production is to set the application name through IIS.

Note:

Another thing to look out for when using the LoginView is that the control does not use the membership provider - it uses FormsAuthentication (for login status) and the RoleProvider for specific roles. You need to put the ApplicationName in your role provider as well:


<roleManager enabled="true" defaultProvider="SqlRole">
  <providers>
    <add name="SqlRole"
    connectionStringName="YOUR_CONNECTION"
    applicationName="appName" />
  </providers>
</roleManager>

 

 

4/21/2006 2:24:00 PM (Eastern Standard Time, UTC-05:00)  #    Disclaimer  |  Comments [4]  |  Trackback
 Monday, April 10, 2006

ASP.NET Debugging with Trace.Warn

If you are trying to use Trace.Warn in your .NET application and you are getting an error that Trace is not declared than it probably means that you need to use the full object path declaration.

HttpContext.Current.Trace.Warn("Your String")

 

4/10/2006 7:45:17 AM (Eastern Standard Time, UTC-05:00)  #    Disclaimer  |  Comments [0]  |  Trackback
 Friday, April 07, 2006

Adding a Confirmation to a Command Button

I'm coming from a Classic ASP background, so I'm used to just using JavaScript to control the client side functionality of command buttons, so when it came time to take control of the Cancel button created by an ASP.NET DetailsView I was stumped. There's probably a better way to do it, but this worked for me.

The command buttons are created in the last row of the DetailsView during the ItemCreated event, so first check if the footer row has been created:

If Not (DetailsView1.FooterRow Is Nothing) Then

The command bar that contains your action buttons it the last element in the Rows collection. Once you have the command bar you can loop through all of its controls and find the one your looking for. I was looking for the Cancel button:

Dim commandRowIndex As Integer = DetailsView1.Rows.Count - 1
Dim commandRow As DetailsViewRow = DetailsView1.Rows(commandRowIndex)
Dim cell As DataControlFieldCell = CType(commandRow.Controls(0), DataControlFieldCell)
' Loop through controls to find Cancel button
For Each ctl As Control In cell.Controls
  If TypeOf ctl Is Button Then
  Dim btn As Button = CType(ctl, Button)
    If btn.CommandName = "Cancel" Then
      btn.ToolTip = "Click here to cancel"
      btn.CommandName = "Cancel"
      btn.OnClientClick = "if(!confirm('Do you really want to discard the changes?')){return false;}"
    End If
  End If
Next

I wanted to add a confirmation to the Cancel the action, but you could use this example to add functionality to any of the command buttons. The confirmation button is created by adding a client side script to the OnClientClick property:

btn.OnClientClick = "if(!confirm('Do you really want to discard the changes?')){return false;}"

Putting it together:

Protected Sub DetailsView1_ItemCreated(ByVal sender As Object, ByVal e As EventArgs) Handles DetailsView1.ItemCreated

' Check that footerRow has been created
If Not (DetailsView1.FooterRow Is Nothing) Then
' Find Command Bar
Dim commandRowIndex As Integer = DetailsView1.Rows.Count - 1
Dim commandRow As DetailsViewRow = DetailsView1.Rows(commandRowIndex)
Dim cell As DataControlFieldCell = CType(commandRow.Controls(0), DataControlFieldCell)
' Loop through controls to find Cancel button
For Each ctl As Control In cell.Controls
  If TypeOf ctl Is Button Then
  Dim btn As Button = CType(ctl, Button)
    If btn.CommandName = "Cancel" Then
     btn.ToolTip = "Click here to cancel"
     btn.CommandName = "Cancel"
     btn.OnClientClick = "if(!confirm('Do you really want to discard the changes?')){return false;}"
    End If
  End If
Next
End If
End Sub

Redirecting User on Command Click

The next thing I wanted to do was redirect the user to a different page if they confirmed the cancel action. First I created a sub called "Get_Command" and then I called it from the OnItemCommand of the DetailsView.

Here's an example:

<asp:DetailsView ID="DetailsView1" runat="server"
AutoGenerateRows="False"
DataSourceID="ObjectDataSource1"
OnItemCommand = "Get_Command">

And this code goes in your Code Behind file:

Sub Get_Command(ByVal Src As Object, ByVal Args As DetailsViewCommandEventArgs)

If Args.CommandName = "Cancel" Then

Response.Redirect("~/yourPage.aspx:")

End If

End Sub

Conclusion

I hope somebody finds this post and it saves them a little time. If somebody knows a better way to do this - I'd love to see it.

4/7/2006 8:28:56 AM (Eastern Standard Time, UTC-05:00)  #    Disclaimer  |  Comments [6]  |  Trackback

It's really easy to set the display size of a multi-line textbox in .NET once you know the correct properties to set.  The multi-line textbox is equivalent to an <HTML> textarea.

To control the number of lines that display to the user you can set the "rows" property and to control the display width you can set the "width" property. You can also set the CssStyle of the textbox to control the width.

Here's an example:

<asp:TextBox ID="txtYourTextBox" runat="server" rows="6" TextMode="MultiLine" Width="450px" Text="Your text"></asp:TextBox>

4/7/2006 7:45:10 AM (Eastern Standard Time, UTC-05:00)  #    Disclaimer  |  Comments [0]  |  Trackback
 Thursday, April 06, 2006

Disable Controls Based on Role

Most web applications need the ability to disable or hide certain contols based on what security role a user in logged in with - Admins will have more privelages then a general user.

It's pretty easy to do with .NET because they have built in the functionality to get a user's roles:

If Roles.IsUserInRole("admin") Then
'Execute this
Else
'Execture something else
End If

Finding Controls in a FormView

What I wanted to do was disable the Delete button on a Formview when the user wasn't logged in as an admin. The first thing I needed to do was actually find the control. The problem is that ASP.NET doesn't know what type of control the formView control is, so you can't directly access the properties of the control. You have to tell it (using cast) what kind of control it is.

I was looking for my Delete button:

DirectCast(FormView1.FindControl("DeleteButton"), Button)

Once you use DirectCast to tell .NET the type of control you have access to all of it's properties. This works with all of the controls - not just buttons.

Putting it Together

Another thing to note when adjusting properties of a FormView control is that you want to do it during the ItemCreated event. It won't work during the PageLoad or PreRender events. So, if you want to disable a button in a FormView based on the users role:

Protected Sub FormView1_ItemCreated(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.ItemCreated

If Roles.IsUserInRole("admin") Then
  DirectCast(FormView1.FindControl("DeleteButton"), Button).Enabled = "True"
Else
  DirectCast(FormView1.FindControl("DeleteButton"), Button).Enabled = "False"
End If

End Sub

 

 

4/6/2006 8:56:13 AM (Eastern Standard Time, UTC-05:00)  #    Disclaimer  |  Comments [0]  |  Trackback