Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Custom Self-Configurable User Control in GridView Template Column

Status
Not open for further replies.

bitseeker

Programmer
Nov 27, 2005
60
US
I'm working on a design the involves multiple record types in the same GridView. I can merge all the fields from all the record types into one record layout in the GridView datasource, if needed, and separate the record types after. The remaining trick is presenting different formats for different records.

I've gotten a basic understanding of how to use the Databinder.Eval method in such a scenario to test the record type and show/hide controls base on the result.

I would like to know if it would be possible to extend this approach to build a custom user control that:

1) could be inserted into the Template within the GridView Column
2) would still be able to access and take action based on the record type, via DataBinder.Eval
3) would be able to run javascript (if it came to that) prior to render, in order to customize itself (position and visibility of fields) based on record type.

Particularly helpful would be brief descriptions of key techniques by which 2) and 3) would be implemented.

I can probably implement the features of my app without going to these lengths, and I respect that the approach above could generate performance issues. However, knowing that the approach above is possible, and knowing the general path for implementing it (steps 2 and 3), would open up both my understanding how the pieces in ASP.NET work together, and also the design possibilities.

Thanks!
 
First, add an @Reference at the top of the ASPX file so you can reference the control by type.

Then, in the RowDataBound event of the grid in the code-beside you should be able to use:
Code:
MyUserControl control = (MyUserControl)e.Row.FindControl("myControl");
...and manipulate the control from there. So, for instance, if you have a public method in the user control that configured stuff, you could do something like:
Code:
control.ConfigureStuff( /*your arguments here*/ );
 
Thanks for input. That will get me started. For further clarification (due to my newbie-ness in this area): how would the javascript in the custom user control be initiated?

I'm just learning about the whole page processing cycle;
(useful link here = My limited model tells me the control will act like a mini-page...but then through what event(explicit or implicit) would the javascript in that control/mini-page notice that the control had been loaded, and then execute to manipulate the page elements(doing callbacks or whatever in the process)?

I'll be digging into my Dummies book on javascript...any jumpstarts on the question above, to help fill out the conceptual framework, would be appreciated.

Thanks!
 
You'd do it through Page.ClientScript (type that, and intellisense will guide you the rest of the way). Remember to reference your control names by control.ClientId if you're going to run scripts on them (and be careful not to register a script with duplicate keys thus overwriting the old one). That part will make more sense when you explore it more.

Anyway, as for user controls, think of them as a way to encapsulate a part of the larger page. For instance, if you have a user control on your web form, it will be delivered like this to the browser:

Code:
<html>
   <body>
      <h2>This is the larger page.</h2>

      [b]<!-- if your user control appears after the above
           text on your web form, it will get rendered
           like you see below.
        -->
      <p>This is where the contents of the user control
         get rendered to your page.</p>
      <!-- end of user control -->[/b]


      <p>This would be the rest of the page that appears
         after your user control in the web form.</p>
   </body>
</html>

So in other words be aware that your script is going to interact with the entire html page, because when the user control gets delivered to the browser, it IS part of the html page.

On a sidenote, that's also why Microsoft tells you not to place <body> or <html> tags in the user controls (because when they get sent to the client, you'd end up with embedded body or html elements which is an error).
 
Ok. I'll play with it and see what other questions I come up with. Thanks for the starting points.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top