ICodeFactory Labs

Workaround for removing broken VSTO bookmark object after manual removal from the document

by Janko 12. July 2010 23:14

Bookmarks are very useful in developing and working with documents. Besides content controls, they are the main tool for our Word add-in project and are used as placeholders to mark items or locations in the Word document. Although developers would like bookmarks to have more options and operations like content controls, they are just not as flexible. Some options that could be supported by VSTO 3 but, unfortunately are not, could make our lives as developers easier:

  • before delete and after add events,
  • locking bookmark to prevent deleting,
  • locking bookmark and its content,
  • deleting bookmark with its content…

All these options are supported for content controls.

 

This post is about a problem that exists when user deletes a bookmark from the document manually. There is no event triggered when this happens. Deleting bookmarks manually causes its Interop.Word.Bookmark object to be removed, but Tools.Word.Bookmark object remains in the documents controls collection. We can access this remaining object but access to its properties and methods will throw exception with message "Object has been deleted". In this state it can not even be removed from the document.Controls collection. So if user removes the bookmark manually, our application will be unaware of that and will break when trying to access its properties, methods or even to remove it.

 

Fortunately, there is a way to “repair” this broken Tools.Word.Bookmark object enough for us be able to remove it from the collection of document’s VSTO objects. Then we are able to recover our application from this unwanted user action. Every time we want to get any bookmarks we use the following method:

 

Public Function GetBookmarkIfExists(ByVal bookmarkName As String) _ 
                                    As Tools.Word.Bookmark
        Dim returningControl As Tools.Word.Bookmark = Nothing
       
        ' If bookmark exists in word document, get it:
        If Me.Controls.Contains(bookmarkName) Then
            returningControl = Me.Controls(bookmarkName)

            ' If user has deleted bookmark manually, VSTO object exists but interop
            ' not. To recover from this unwanted state it is needed to remove VSTO
            ' object too:
            If (Not (Globals.ThisDocument.Bookmarks.Exists(bookmarkName))) Then
                Globals.ThisDocument.Bookmarks.Add(bookmarkName, Me.Range(0, 0))

                ' Remove all bindings from the bookmark and delete it:
                returningControl.DataBindings.Clear()
                returningControl.Delete()
              
                returningControl = Nothing
            End If
        End If
        Return returningControl 
End Function 

The method is in the document class.

 

This method checks if the corresponding Interop object exists for the VSTO object. If not, the VSTO object it broken. By adding interop object with the same name, VSTO object will be “repaired”. Range of repaired bookmark will not be as it was before removal but it is deletable now. Deleting bookmark will not delete its content.

 

Of course this method needs to be adapted to your code and application.

Currently rated 1.6 by 148 people

  • Currently 1.554054/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , ,

VSTO | .NET

Workaround for VSTO bug: Content control Exiting event handler

by Janko 21. May 2010 02:04

If you have ever worked on some Word add-in where you use content controls, you maybe have encountered problems for which you have no explanations. This post is about one of the known VSTO issues and offers a workaround for it.

 

We were working on Word add-in that uses content controls. Also we needed to process data on entering and exiting a content control.

 

Good thing is that VSTO offers events Entering and Exiting.

Bad thing is that Exiting event has a bug. If you are doing some "simple" process in Exiting event handler it could work fine, but you never know what will happen. Using this event could produce very strange problems, problems which look like they should not have any connections with the event.

 

Several strange problems have occurred for which we were unable to find a solution. So we created different POC projects to reproduce and isolate the bugs and posted them on the Microsoft forum. Shortly the issues were confirmed - one of them as a new VSTO bug (https://connect.microsoft.com/VisualStudio/feedback/details/556456/programatically-deselecting-a-content-control-results-in-a-com-exception?wa=wsignin1.0) and another got recognized as a notorious VSTO bug. After using proposed workaround for notorious bug, both bugs were resolved.

 

The notorious problem manifests itself in following way:

In the document there are two building block content controls:

  1.  POCControl1 and
  2.  POCControl2

On the document startup we are adding Entering and Exiting event handlers to these controls.

-On Entering the control TrackChanges is set to TRUE and old value is remembered.

-On Exiting all changes are confirmed and TrackChanges is returned to the previous version.

Problem occurs in following scenario:

  1. Select POCControl1
  2. Entering event occurs.
  3. Unselect POCControl1.
  4. Exiting event occurs.
  5. PROBLEM: on line
    Me.TrackRevisions = Me._oldTrackRevisionsStatus
    Entering event for POCControl1 occurs (which should not happen). After it finishes, Exiting event continues to run from the specified line.

Even stranger behavior occurs when selecting POCControl1 and then selecting POCControl2. Here is the whole POC code:

 

Public Class ThisDocument

    Private _oldTrackRevisionsStatus As Boolean

    Private Sub ThisDocument_Startup(ByVal sender As Object, _
                                     ByVal e As System.EventArgs) Handles Me.Startup
        AddHandler POCControl1.Entering, AddressOf TextItemSelected
        AddHandler POCControl2.Entering, AddressOf TextItemSelected
        AddHandler POCControl1.Exiting, AddressOf TextItemUnselected
        AddHandler POCControl2.Exiting, AddressOf TextItemUnselected

        POCControl1.Tag = "UPPER control"
        POCControl2.Tag = "LOWER control"
    End Sub

    Public Sub TextItemSelected(ByVal sender As BuildingBlockGalleryContentControl, _
                                ByVal e As ContentControlEnteringEventArgs)
        MsgBox("Select " & sender.Tag & " beggining")

        ' Set track changes:
        _oldTrackRevisionsStatus = Me.TrackRevisions
        Me.TrackRevisions = True

        MsgBox("Select " & sender.Tag & " ending")
    End Sub

    Public Sub TextItemUnselected(ByVal sender As BuildingBlockGalleryContentControl, _
                           ByVal cevent As Tools.Word.ContentControlExitingEventArgs)
        MsgBox("Unselect " & sender.Tag & " beggining")

        If sender.Range.Revisions.Count > 0 Then
            sender.Range.Revisions.AcceptAll()
        End If

        Me.TrackRevisions = Me._oldTrackRevisionsStatus

        MsgBox("Unselect " & sender.Tag & " ending")
    End Sub
End Class

 

Proposed solution:

Do not use Exiting event at all. For example, use Application.WindowSelectionChange event instead, which we did. This event is triggered on every selection change in the active document window, whenever it is manually or programmatically caused. In its handler we are tracking "exiting" from the control.

 

So we removed Exiting event handler and related code from our POC project and added WindowSelectionChange event handler:

 

Private _oldTrackRevisionsStatus As Boolean
    Private selectedControl As Tools.Word.BuildingBlockGalleryContentControl

    Private Sub ThisDocument_Startup(ByVal sender As Object, _
                                     ByVal e As System.EventArgs) Handles Me.Startup
        AddHandler POCControl1.Entering, AddressOf TextItemSelected
        AddHandler POCControl2.Entering, AddressOf TextItemSelected
        
        POCControl1.Tag = "UPPERControl"
        POCControl2.Tag = "LOWERControl"
    End Sub

    Public Sub TextItemSelected(ByVal sender As BuildingBlockGalleryContentControl, _
                                ByVal e As ContentControlEnteringEventArgs)
        MsgBox("Select " & sender.Tag & " beggining")

        selectedControl = sender

        ' Set track changes:
        _oldTrackRevisionsStatus = Me.TrackRevisions
        Me.TrackRevisions = True

        MsgBox("Select " & sender.Tag & " ending")
    End Sub

    
    Public Sub WindowSelectionChange_Handler(ByVal sel As Interop.Word.Selection) _
                                      Handles ThisApplication.WindowSelectionChange
        Dim control As Interop.Word.ContentControl = sel.Range.ParentContentControl
        If selectedControl IsNot Nothing _
         AndAlso (control Is Nothing OrElse control.Tag <> selectedControl.Tag) Then
            MsgBox("Unselect " & selectedControl.Tag & " beggining")

            If selectedControl.Range.Revisions.Count > 0 Then
                selectedControl.Range.Revisions.AcceptAll()
            End If

            Me.TrackRevisions = Me._oldTrackRevisionsStatus

            selectedControl = Nothing

            MsgBox("Unselect ending")
        End If
    End Sub

 

If you have issues with content controls and you are using Exiting event, it is possible that it is the root of your problems. Use WindowSelectionChange event and adapt its handler for your own code. It helped us and might potentially help you get rid of some annoying bugs and unwanted behavior.

Currently rated 1.6 by 56 people

  • Currently 1.642859/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , ,

ICF.Labs | .NET | VSTO

Dynamically adding ASP.NET validators and ASP.NET ajax validation callout extenders using Javascript

by Lacio 13. May 2010 01:33

Have you ever encountered a problem where you generated some content dynamically without the help of the server and server side code, but still needed to perform some basic validation on those contents?

That is exactly what this post deals with – validating content which is taken from dynamically generated input fields and adding the validation callout extender to it. 

Since I did not find any suitable solutions online, I decided to do it myself. Turned out the solution is not so hard at all, and after a small analysis on the page source of pages with regular validation, I got an idea on how to do it.

The first thing to notice is that after the aspx page is parsed, the output of a validator control (of any type) is a SPAN tag. Sort of. The tag has some unconventional attributes, off course depending on the type of the validator used, but we will get to that later. Besides this, a reference to the span is kept in an array called Page_Validators, which is an array consisting of all the validators present on the page.

So let’s go step by step.

First create the span tag and place it in the DOM where you would place the validator control. Lets say we have an input field and we place a RequiredFieldValidator next to it, like in the code below:

 

               // Get the table:
                var tblEditItems = $get('tblEditItems');
                // Get the number of rows currently in the table:
                var rowsCount = tblEditItems.rows.length;
                // Insert a new row at the end of the table:
                var row = tblEditItems.insertRow(rowsCount);

                // Insert a table cell:
                var cellName = row.insertCell(0);
                // Create an input field for the name:
                var elName = document.createElement('input');
                elName.type = 'text';
                elName.size = 10;
                elName.id = 'txtProductName' + globalControlCounter;
                cellName.appendChild(elName);

                // Add the span that will represent the validotor
                // (in this case the required field validator):
                var elNameValidator = document.createElement('span');
                elNameValidator.style.color = "Red";
                elNameValidator.style.display = "none";
                elNameValidator.id = elName.id + "Validator";
                elNameValidator.controltovalidate = elName.id;
                elNameValidator.errormessage = "<b>Field is incorrect</b>
                                     <br /> <span>Name is a required field.</span>";
                elNameValidator.validationGroup = "EditItems";
                elNameValidator.initialvalue = "";
                elNameValidator.evaluationfunction =
                                              RequiredFieldValidatorEvaluateIsValid;

 

The next step is to add the validator to the array of existing validators:

 

                // Push the new validator inside the page validators array:
                Page_Validators.push(elNameValidator);

 

After this is done, we can add the validator callout extender control, that will add some nice pop out effects displaying the error message itself:

 

                // Now lets bind the validator callout that will popup
                // up when the field is not valid:
                $create(AjaxControlToolkit.ValidatorCalloutBehavior, {
                    "closeImageUrl": "/image/close.png",
                    "highlightCssClass": "highlight",
                    "id": elNameValidator.id + "ValidatorCalloutExtender",
                    "warningIconImageUrl": "../images/attention.png"
                }, null, null, $get(elNameValidator.id));

 

The last step that needs to be taken and is essential for this to work is to add some dummy validator and dummy validator callout extender to the page. This is needed, as otherwise the necessary javascript libraries will not be included on the page (either this, or including the libraries by hand, whichever solution you prefer).

 

Now, the triggering of the validators is up to you – I used triggering when the user clicks some submit action. Since the action does not cause a postback, I needed some client side validation check. If you have the same problem, you can check out this simple solution located on this post.

 

As you can see, it is really easy to create the validation and add the validator callout extenders, with just a bit of knowledge in javascript. I recommend experimenting with different types of validators, as they all attach different attributes to the DOM element. If you have any questions or suggestions, feel free to post them to the comments section, I will try my best to help.

Currently rated 1.5 by 92 people

  • Currently 1.532607/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , , ,

.NET | ASP.NET | ICF.Labs

ASP.NET AJAX 4.0 Templates Simple Demo - Step by Step

by Sergio 27. November 2009 02:59

This quick step by step tutorial is made with Visual Studio 2010 beta2
and ASP.NET AJAX 4.0 Preview 6.
It is possible that some properties and/or methods will be changed in future versions, but so far this is the way to use client side templates in ASP.NET AJAX 4.0.

 

Downloads

First you should download appropriate software:

  1. Microsoft .NET Framework 4.0
  2. Microsoft Visual Studio 2010
  3. ASP.NET Ajax Library

Project

For your first project you should use web site or web project visual studio templates, create new project and add appropriate scripts to a project.

For this example I’ve created a new web site and added all the AJAX scripts we need to a folder named “MicrosoftAJAX” (look at the picture).

 

WebSiteProjectFiles

 

Also I have created a simple demo xml web service to be used from client side to read data for our template.

 

   1:  <%@ WebService Language="C#" Class="DemoService" %>
   2:   
   3:  using System;
   4:  using System.Web;
   5:  using System.Web.Services;
   6:  using System.Web.Services.Protocols;
   7:   
   8:  [WebService(Namespace = "http://icodefactory.com/", 
   9:     Description="Demo web service used by client side templates")]
  10:  [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  11:  // To allow this Web Service to be called from script, 
  12:  // using ASP.NET AJAX, uncomment the following line. 
  13:  [System.Web.Script.Services.ScriptService]
  14:  public class DemoService  : System.Web.Services.WebService {
  15:   
  16:      [WebMethod(Description="Returns Demo Contacts data")]
  17:      public Contact[] GetContacts() {
  18:          Contact[] contacts = new Contact[3] {
  19:              new Contact(){ Id=1, Name = "John", Lastname="Doe" },
  20:              new Contact(){ Id=2, Name = "Joan", Lastname="Smith" },
  21:              new Contact(){ Id=3, Name = "Brad", Lastname="Forbs" }
  22:          };
  23:          return contacts;
  24:      }
  25:      
  26:  }

 

Pay attention that you have to set the ScriptService attribute so your web service can be used from JavaScript.

 

Finally we have to add some simple html (yes, pure html) file and import appropriate scripts:

  • MicrosoftAjax.js
  • MicrosoftAjaxTemplates.js

for AJAX client side libraries and

  • DemoService.asmx/jsdebug

for access to the xml web service.

 

   1:  <script src="MicrosoftAJAX/MicrosoftAjax.debug.js" 
   2:          type="text/javascript"></script>
   3:  <script src="MicrosoftAJAX/MicrosoftAjaxTemplates.debug.js"
   4:          type="text/javascript"></script>
   5:   
   6:  <script src="DemoService.asmx/jsdebug" 
   7:          type="text/javascript"></script>

 

We also need some styling on the page. Class ‘sys-template’ is used to hide empty table before data is bound from the service by using AJAX DataView client side control.

 

   1:  <style type="text/css">
   2:          /*Used to hide empty table*/
   3:          .sys-template
   4:          {
   5:              display: none;
   6:          }
   7:          /* Style of table that shows data*/
   8:          .table_style
   9:          {
  10:              background-color: Olive;
  11:              padding: 5px 5px 5px 5px;
  12:          }
  13:      </style>

 

Now, time for JavaScript that should load the data from the xml web service, fill the DataView object and bind the data to the html template.

 

<script type="text/javascript">
        // ASP.NET Ajax client side control - DataView
        var contactsView;
        
        // Triggers on page load
        function pageLoad() {
            // Create Contacts DataView
            contactsView = 
                      $create(Sys.UI.DataView, {}, {}, {}, $get("contactsBody"));

            // Load Contacts from web service
            DemoService.GetContacts(ContactsLoaded);
        }
        
        function ContactsLoaded(contactsData) {
            // Display data in Contacts DataView
            contactsView.set_data(contactsData);
        }
    </script>

 

The pageLoad function creates the DataView client side object and calls the xml web service to populate the object with data.

On successful service call the data is bound to the html with a simple call to the set_data() function.

 

This is how your template should look:

 

   1:  <table class="table_style" border="0" cellpadding="4" cellspacing="4">
   2:          <thead>
   3:              <tr>
   4:                  <th>
   5:                      Name
   6:                  </th>
   7:                  <th>
   8:                      Lastname
   9:                  </th>
  10:              </tr>
  11:          </thead>
  12:          <!-- tbody tag id is used to identify tag in DOM that are going to be
  13:               used by the DataView. Class is set to 'sys-template' in order to
  14:               hide empty table -->
  15:          <tbody id="contactsBody" class="sys-template">
  16:              <tr>
  17:                  <td>
  18:                      {{ Name }}
  19:                  </td>
  20:                  <td>
  21:                      {{ Lastname }}
  22:                  </td>
  23:              </tr>
  24:          </tbody>
  25:      </table>

 

<tbody> tag is used to become a template for the data in the DataView object.

DataView will change HTML with appropriate tags and generate table rows for every data row from the xml web service method call.

{{ Name }} and {{ Lastname }} parts will be replaced with actual data.

 

At the end you have your data.

 

DemoSite_Result

 

Hope this will help you start with the new templates. Web development is moving to client side and everyone should embrace the client side because of its many benefits, like responsiveness, bandwidth and UI patterns.

 

Demo Solution may be downloaded from our public download section.

Currently rated 1.8 by 31 people

  • Currently 1.838711/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , ,

ASP.NET

How to trigger an ASP.NET validator from JavaScript?

by Lacio 7. October 2009 04:40

Have you ever been in a situation when You were not able to perform server side validation and were not able to trigger the validators in the standardized way. By this I mean that you had a button that was doing a submit, but without causing any postback, and doing it all through JavaScript. In my case, I had a pagemethod that was doing a save for some entity we needed. So because of the nature of this operation, I needed to do a validation – and off course I used different kinds of validators – from range to required field and custom validators. So what is the problem then – the problem is that the JavaScript hides the form for the insert and the validation messages get lost, and along the way either a false insert or an exception is thrown – some fields might even be potential security breaches without proper validation. So I wandered around trying to find a way to perform regular validation, the way it is supposed to be done. I found a nice, elegant way to do it and even make it reusable on all the pages that need JavaScript validation and have the same problems as stated above.

 

An example of how validation works:

 

blogpic

 

So the solution is to write a small JavaScript function that will invoke all the validators that belong to a certain group. Below is the JavaScript function.

 

function ValidateEntry(validationGroup) {

  var isValidEntry = true;

  if (typeof (Page_Validators) != 'undefined') {

    for (var i = 0; i < Page_Validators.length; i++) {

      if (Page_Validators[i].validationGroup == validationGroup) {

        // call validator function

        var func = Page_Validators[i].evaluationfunction;

        Page_Validators[i].isvalid = func(Page_Validators[i]);

        if (!Page_Validators[i].isvalid) {

          isValidEntry = false;

          Page_Validators[i].style.visibility = '';

        }

      }

    }

  }

  return isValidEntry

}

 

So how does this work? The function returns a boolean value that suggests whether the validation is correct. The function goes through all the page validators, and selects the ones which belong to the specified validation group – this way we ensure only validators needed to be invoked are actually checked.

 

Later, we just call the ValidateEntry JavaScript function before calling the method that does the persisting – in my example, before calling a pagemethod that calls some server side method that does an insert in the db. The example is below:

 

First we have the controls that have the validators on them:

<td>

  <asp:TextBox MaxLength="50" ID="txtNewName" runat="server"

    Width="495px" ValidationGroup="NewEntityValidationGroup">

  </asp:TextBox>

</td>

<td>

  <asp:RequiredFieldValidator ID="valNewName" Text="*" ValidationGroup="NewEntityValidationGroup" runat="server" ControlToValidate="txtNewName">

  </asp:RequiredFieldValidator>

</td>

<td>

  <asp:TextBox ID="txtNewDescription" TextMode="multiLine" runat="server" Height="100px" Width="495px" ValidationGroup="NewEntityValidationGroup">

  </asp:TextBox>

</td>

<td>

  <asp:RequiredFieldValidator ID="valNewDescription" Text="*" ValidationGroup="NewEntityValidationGroup" runat="server" ControlToValidate="txtNewDescription">

  </asp:RequiredFieldValidator>

</td>

Then we have the pagemethod call with the call to the validation function (ValidateEntry):

 

function AddNewEntity() {

  if (ValidateEntry("NewEntityValidationGroup")) {

    var entityName = $get('<%=txtNewName.ClientID %>');

    var entityDescription=get('<%=txtNewDescription.ClientID %>');

    PageMethods.AddEntity(entityName.value, entityDescription.value, AddEntitySuccess, OnFailure);

  }

}

 

Lets analyze the function above – before we do any processing, we check all the validators, and only if the function returns true, we continue with the field value retrieval and call the pagemethod.

 

The best practice to use this type of validation would be to extract it to a new file and include the JavaScript file wherever it is needed.

 

If you want to incorporate this into your solution, feel free to go ahead and use the JavaScript function above, it is easy to integrate –just copy it to your solution, either to the page itself or to a JavaScript file and then just call it.

Currently rated 3.5 by 11 people

  • Currently 3.454545/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

FCKEditor with ASP.NET - Fix “The server did not send back a proper XML response error”.

by Sergio 7. July 2009 10:47

I like FCKEditor. Our company is using it on several projects, and it mainly works fine, it has a number of different functionalities and it integrates well.

Recently we integrated FCKEditor with an asp.net 3.5 web application. We wanted to allow the client to update some contents on the web site and upload/manipulate images, but we faced a strange exception that stated:

The server didn’t send back a proper XML response. Please contact your system administrator.
XML request error: Ok (200)

 

xmlrequesterror_image1

Response text looked scrambled and messy.

Our first reaction was to search for the same bug on net, but after a few days I did not find anything barely useful!

That’s about when I realized I’ll have to investigate it by myself.

First of all I supposed I should blame some response mime type or format, so I dived into the FCKEditor project source code and found a class named XmlResponseHandler.

This class is used to clear the response object, set the content encoding and content type.

It all looked well. It was time to put some break points and investigate what is going on there.

I monitored two methods: SetupResponse and SendResponse.

private static void SetupResponse( HttpResponse response )

{

// Cleans the response buffer.

response.ClearHeaders();

response.Clear();

// Prevent the browser from caching the result.

response.CacheControl = "no-cache";

// Set the response format.

response.ContentEncoding = System.Text.UTF8Encoding.UTF8;

response.Charset = "utf-8";

response.ContentType = "text/xml";

}

public void SendResponse()

{

SetupResponse();

Response.Write( Xml.OuterXml );

Response.End();

}

Let’s put some break points and check what is going on while looking for the xml response from the server.

code_with_break_points_image2

My attention was drawn to data.

xmldata_image3

SendResponse method has a well formed xml. Let’s go further through the code.

Oops!

The server didn’t send back a proper XML response. Please contact your system administrator.

XML request error: Ok (200)

 

The same error again. Good. That means it is not about the data. Response looks like it is scrambled or packed when it comes to the client side!

That is a clue. Let’s investigate an interesting property of the Response object. It is the Filter property.

Response.Filter is a stream that is used to process data before it is sent to browsers, so if you, for example like to convert all cases to upper this is the place for your custom stream.

I investigated that property and found its value interesting.

deflatestream_image4

There was a DeflateStream object. This means response is compressed by server and sent to client as a compressed stream. This is why I got and strange encoded characters as error.

Fix was easy. I added one line of code:

Response.Filter = null;

private static void SetupResponse( HttpResponse response )

{

// Cleans the response buffer.

response.ClearHeaders();

response.Clear();

response.Filter = null;

// Prevent the browser from caching the result.

response.CacheControl = "no-cache";

// Set the response format.

response.ContentEncoding = System.Text.UTF8Encoding.UTF8;

response.Charset = "utf-8";

response.ContentType = "text/xml";

}

Build the solution and run it again. It works.

It was simple fix, but a very hard one to find. What is most amazing for me is that it was not found by other members of the community so far. I hope this article will help. Does it?

Currently rated 4.0 by 11 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

ASP.NET

ASP.NET 3.5 ListView control - Is it perhaps too early?

by Lacio 3. July 2009 04:50

The ListView control was introduced with asp.net 3.5 as an alternative to the existing data bound controls. It offers much more possibilities than the other controls, plus it generates clean html code, that will display just the way you tell it to. It offers all the possibilities of the other controls combined - selection, sorting etc. Despite all of this sounding quite nicely, it still has some unresolved issues that might hinder its usage in the early stages.

 

Being accustomed to using drag&drop and free naming techniques, the first strange thing that popped to my mind the first time I used the control was the need to name the itemplaceholder exactly "itemPlaceholder". That was strange at first, but only later  have I realised that this was a clever method to transform any control running on client side into a potential place holder for the item templates defined by the ListView control. However, I still found it strange that the name has to be hard coded, otherwise the control won't work. Why couldn't they add a property that holds the name of the itemplaceholder, instead of explicitly being forced to name it like that.

 

After coping with this annoying issue all in the name of being able to use a great new control that is still better then any of the other ones - it enables selected item template - which beats the repeater - and it generates the html you tell it to generate - unlike the datalist control, which is really messed up here, I found another really strange issue. I am not sure if anyone has encountered this problem, I did some short googling on the issue, and haven't found any spot-on fixes. Either people are not using the control, or I have a very specific version of the .net framework and it just won't obey commands from my keyboard :).

 

The issue happens when raising the SelectedIndexChanged event - first of all, it requires the SelectedIndexChanging event to even work. If you don't specify the indexChanging event, the compiler will report an issue. If you manage to get it started however, you are about to hit another unpleasent surprise. If you expect it to run normally, it won't - when having the select command the index will always be lagging by one step - The selected index will always be the next to last item you selected. The way to fix this is by set the selected index in the method raised by the SelectedIndexChanging event. This way, when you get to the selectedIndexChanged event, you will have the correct index and it will show it selected on the page.

 

Doing some research has revealed that it is the least used control of them all. For me personally, once you learn how to tame it, it becomes a very powerfull tool to display data. I believe that with the coming of the new version of asp.net the control will mature a bit
and become a regular feature on sites worldwide.

Currently rated 1.9 by 21 people

  • Currently 1.857143/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , ,

.NET | ICF.Labs | State of Mind

UAC Revealed ~ .elevation of rights from .NET as commonly misunderstood.

by Sergio 8. June 2009 09:19

Recently we had a project that involves impersonification of windows users.

Idea was to create a tool that will allow non admin domain user to start single executable that requires administrative rights without need to add domain user to administrators group.
So, we were planning to create command prompt tool that may process parameters, impersonate some other user (administrator) and starts another process with new user’s credentials.

We made a small proof of concept and it all worked well. Nice, I sad, let’s test it with Vista’s User Access Control. Whoops!

Newly started process, starts as admin user, but without elevated rights. That means user is administrator, but run with rights of regular user!

So we got “this process needs elevation” error in console window.

After some investigation on internet we found ProcessStartInfo.Verb property as point of interest in this case.
According to number of articles, you just need to set this property’s value to ‘runas’ and new process will be started with elevated rights. As if.
This approach would not help you in this case. Actually there is no solution for UAC!

We contacted Microsoft’s forums, and after number of posts and responses we discovered the awful truth.
There is no way to start another process from .net code, impersonate another user to execute that code and elevate rights for that user.
Actually there is no way to do it with UAC enabled. It is constraint of UAC itself. UAC will not allow you to do this.
Microsoft claims that this is security measure, but in my opinion it does not have too much sense since my code already knows administrator user’s credentials!
So, what kind of protection is this anyway?
Next thing that was really strange to me is why so many articles claim opposite?
(Take a look at: http://victorhurdugaci.com/using-uac-with-c-part-1/ for example.)

Problem is that there are some administrative users that are not controlled by UAC!
Built in local administrator user or build in domain administrator user are not constrained by UAC.

This means that you may start another process from your .net code, impersonate built in administrator user and new process will start elevated!
So this code snippet:

ProcessStartInfo processInfo = new ProcessStartInfo();
SecureString securePassword = new SecureString();
foreach (char ch in “testpass”)
{
securePassword.AppendChar(ch);
} // foreach
processInfo.UserName = “administrator”;
processInfo.Password = securePassword;
processInfo.Verb = “runas”;
processInfo.FileName = @”c:\windows\system32\cmd.exe”;
processInfo.UseShellExecute = false;
Process.Start(processInfo);

Will run cmd.exe elevated on Vista, because this user is not run as regular user, but as full rights administrator!
But, if you try to use any other member of administrators group as as new process user it will not work!

Unfortunately it has been an ‘show stopper’ for our project. Too bad.

By my first investigation it will  be possible by default on Windows 7! Now, that is interesting.

I wonder did we found a bug in UAC design or .NET implementation? Maybe. The World will know;-)

 

Currently rated 4.6 by 11 people

  • Currently 4.636363/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

.NET

Test Driven Development ~ Yes or No

by Sergio 24. May 2009 18:24

Currently as being in Germany and working on client's site I met an interesting project.

Product that our team is working on with the client is developed on TDD practices.

NUnit, CruiseControl.NET and NCover are used to support development.

I now wonder why TDD is not used more on other projects. There are a number of common reasons people exposes.

“It takes too much time to write tests. I code faster without it.”

This is I guess number one complaint voiced by developers. It is untrue. And it is simple.

Many people view testing of any sort as something that happens at the end of a project. And it is somehow true, but not completely.

It is more related to acceptance tests for example. If you try to add unit test at the end of project, you will fail,

but if you use "pay as you go" logic and start with tests at the beginning, you will get solid, tested code, and will definitely have less bugs.

This means that you will not produce 2 lines of code daily at the end of project just because you are searching for an tricky bug.

This is especially true for "heavy to debugg" issues in nearly finished projects when deadline is so close that you feel dizzy.

There are a number of other excuses for not using TDD, but this one is somehow first one to beat. 

Currently rated 3.5 by 4 people

  • Currently 3.5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , ,

State of Mind

Welcome to ICF labs

by Administrator 28. June 2008 18:00

This section is still under development, but when finished, you will be able to see interesting guides, hands-on experiences, helpfull tips and a host of other nice articles.

Currently rated 4.0 by 5 people

  • Currently 4.04/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

ICF.Labs


Contact

Development and Sales
ICodeFactory d.o.o.
Trg Marije Trandafil 24/2
21000 Novi Sad
Serbia, Europe
Phone: +381 (0)21 41 77 08
info[at]icodefactory[dot]com

Headquarter
T.C. Bagljaš, Lok. 11
23000 Zrenjanin
Serbia, Europe

Working hours
Monday - Friday
8am - 4pm (GMT+1)