Got more questions? Find advice on: SQL | XML | Regular Expressions | Windows
in Search
Welcome to AspAdvice Sign in | Join | Help

Justin Lovell's Blog

Mood: The Grumpy Coder

DataGrid Dilema Solved... will it be used?

Scott Mitchell is on a mission and he is looking for good method to tackle his problem with the easiest method. He has appealed to the community on how to tackle his problem (which will be published in an article):

http://scottonwriting.net/sowblog/posts/544.aspx

And I wanted to have a shot at solving his dilema with the DataGrid. I already posted a suggestion that was a “maybe it will work out“, which went on the lines of:

I was thinking that you (Scott) should go on something along the lines of what your (Scott's) initially thought. It is something that I like to call "Better late than never". You (Scott) go about this by allowing the DataGrid to do its processing, then override the OnPreRender method. You will allow the actual DataGrid to do its PreRender event processing and then slap the button controls at the top of the DataGrid.

protected override void OnPreRender(EventArgs e) {
   base.OnPreRender(e);
   // slap the controls at the top of the DataGrid... example:
   Controls.AddAt(0, new Button());
}

And the “maybe” idea was shot down when I learned from Scott that the PrepareControlHierarchy method will called during the Render part of the life cycle. However, I was still determined to find the answer. Instead of adding controls to the control collection, why not render straight to the HTTP response (and revert back to the IPostBackEventHandler interface for the interaction of the events)?

public class EnhancedDataGrid : DataGrid, IPostBackEventHandler {
   private const string EditBatchUpdateText = "Edit Batch Update";

   public event EventHandler EditBatchUpdate;

   protected virtual void OnEditBatchUpdate(EventArgs e) {
      if (EditBatchUpdate != null)
         EditBatchUpdate(this, e);
   }

   public void RaisePostBackEvent(string argument) {
      switch (argument) {
         case BatchUpdateText :
            OnEditBatchUpdate(EventArgs.Empty);
            break;
         // and so on
      }
   }

   private void WriteButton(HtmlTextWriter writer, string text) {
      Button button = new Button();
      button.Text = text;
      button.Attributes.Add("OnClick",
      
 Page.GetPostBackClientEvent(this, text));
      button.RenderControl(writer);
   }

   protected override void Render(HtmlTextWriter writer) {
      WriteButton(writer, EditBatchUpdateText);
      // and so on
      base.Render(writer);
   }
}

And with some further modification to the article, I am sure he can go for the idea that “Batch updates” buttons can be placed anywhere on the page. Good luck to you Scott and I hope this was in some aid to your dilema.

Sponsor
Published Saturday, January 10, 2004 10:59 AM by jlovell
Filed under:

Comments

 

jlovell said:

...It certainly will be used !

I've been tearing my hair out trying to get this to work. Like Scott I'd got as far as playing about with the controls collection and overriding most of the DataGrid methods, but this does the trick and is far less messy.

Thankyou, Thankyou Sam-I-am
September 29, 2004 6:48 PM
Anonymous comments are disabled