.NET Discussion

.NET Issues, Problems, Code Samples, and Fixes

ASP.NET and AJAX: UpdatePanelPopupExtender Is An Awesome Tool

Supposing that your site is set up for ASP.NET and AJAX, adding a popup panel to any control is a snap, thanks to Raj Kaimal’s UpdatePanelPopupExtender (UPPE).  Normally I am not one for taking other people’s code or user controls, but I was in a bind and needed something like this very quickly, and it worked amazingly well.  Essentially, the UPPE acts as a method to hide and show a details or “extra info needed” panel using AJAX.  You can even easily attach it to command buttons in GridViews!

It is very easy to install: simply drop the DLL in your /bin/ directory and reference it, and then add it to your toolbox (second section). Then drop it where you want it, usually at the bottom of the page because during runtime its page location will be determined dynamically.  Here’s code that I used to make it work on the .aspx page:

<cc1:updatepanelpopupextender id="uppeDetails" runat="server" targetcontrolid="upDetails" offsetx="0" offsety="0" CalloutColor="#7C6F57" CalloutBorderColor="Black" AutoPostBack="True">
 <div style="display:none;">
  <asp:updatepanel id="upDetails" runat="server">
  <div class="popup-details">
  <h3>Inventory Notes: </h3>
  <asp:textbox id="txtInventoryNotes" runat="server" textmode="multiLine" rows="5"></asp:textbox>
  <p><asp:button runat="server" id="btnSaveNotes" text="Save and Close" onclick=btnSaveNotes_Click> <asp:button runat="server" id="btnCancelNotes" text="Cancel"></p>
<!-- END -->

The necessary parts of the above code are the <div style="display: none;">, which ensures that the panel is hidden from the get-go, the autopostback="true" property in the control declaration, and the fact that it is referencing an update panel within your hidden div. Anything else is pretty much optional, and you can put anything in there, too.

Once you have that set up, you have a few steps in your code-behind to set up (all control names reference the controls from the above example). First, import the library (Imports Flan.Controls). Then in your Page_Load event, you need to reference the “close” button for your UpdatePanel, like so: UpdatePanelPopupExtender.RegisterCloseControl(btnCancelNotes, upDetails)

Then the last step is to have the control show up where you want it to, usually next to the button that says “Edit” or “See Details” or whatever, and to do that, just put the following code in that control’s Click event: uppeDetails.ShowAt(yourControl). And that’s it!

If you want to wire it to a control in a GridView, it is a little trickier, but not because of the UPPE. Rather, you need to dynamically locate the control to wire the UPPE to. Here’s how you do it using the “SelectedIndexChanging” event of a GridView:

Protected Sub gvExisting_SelectedIndexChanging(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewSelectEventArgs)
		Dim ctl As Control = CType(gvExisting.Rows(e.NewSelectedIndex).Cells(10).Controls(0), Control)	'Cells(10) is the location of my CommandField column
		gvExisting.SelectedIndex = e.NewSelectedIndex
		txtInventoryNotes.Text = gvExisting.DataKeys(e.NewSelectedIndex).Values(1)
	End Sub

What I did in the above example is figure out exactly which cell was clicked, then pull the datakey from the GridView (which happens to contain the info I want) and then setting the textbox in my UPPE to that information, then showing the UPPE.

It’s all very easy to do, and extremely helpful for UI. You can have entire detail panels just pop up for editing, take care of business, then disappear again. No more need for separate detail pages, just use an UpdatePanelPopupExtender!

Here’s the download location for the UpdatePanelPopupExtender and sample code.

kick it on DotNetKicks.com

October 31, 2007 Posted by | AJAX, AJAX Toolkit, ASP.NET, GridView, Tips & Tricks | | Leave a comment

ASP.NET Nested MasterPages: ID Mangling

After I implemented the ASP.NET PayPal solution for the order submission part of my site, I noticed that my color picker (which relies heavily on Javascript) wasn’t working anymore.  I did a bit of digging in my code and eventually discovered the source of the problem: ID mangling. While I was aware that ASP.NET mangled control IDs, I was not aware that each time you nest a MasterPage, you change the way the ID is mangled.

For example, my original post-mangling ID was “ctl00_cphMain_txtBackgroundColor”, and after I nested my MasterPages, the ID changed to “ctl00_ctl00_cphMain_cphMain_txtBackgroundColor”.  So, when using the document.getElementByID() function, it couldn’t locate the control, and thus threw me a yellow triangle. 

This really shouldn’t affect anyone who doesn’t include much Javascript in their code, but should be a good FYI for those who do.  I guess the silver lining is that at least the IDs are mangled predictably 🙂

kick it on DotNetKicks.com

October 19, 2007 Posted by | ASP.NET, Javascript, MasterPages, Tips & Tricks | 2 Comments

ASP.NET: Rethinking Inline Scripting

I came across an article called Crazy Talk: Inline Scripting and Code-Behind and it really made me rethink the way I program. The article discusses how the “taboo” of inline scripting shouldn’t really be taboo anymore because, if done responsibly, it can be extremely effective in cutting down code. As a sort of experiment/form-reuse, I took a working form from one project to implement in a new project I’m working on and tried using inline scripting where it was required.

I was basically blown away by how much simpler it was to just use <%= %> for implementing little things, such as name, address, etc. rather than having to deal with literals on the code behind. The codebehind was cut from 58 lines to two—four if you count the declaration of objects to use for binding the UI—and the same exact result is achieved. I don’t think my codebehind was messy either, just a lot of string concatenation and setting literal .Text properties.

Don’t take this as a license to completely rid yourself of codebehind pages and go nuts building everything into your .aspx page. Server side coding still has its purpose and is still more powerful than inline scripting, and many things are still easier to do on the server side. Just don’t think that it’s still “bad practice” to weave inline scripting into your source code. When used responsibly, it can make a lot of things a heckuva lot easier.

October 16, 2007 Posted by | ASP.NET, Tips & Tricks | Leave a comment

VB.NET and ASP.NET: How Do You Combine Them?

I’m trying to create an executable VB.NET program that will serve as an order-entry system on my local system so that I can export to Microsoft Excel and Word, but also draw from my existing database and use my existing classes. How do I do this in Visual Studio 2005? Do I simply add another “project” to my existing ASP.NET solution? I suppose I could try it to find out, but there is a lot of development to be done before I could even get to a testing point.  Also, if I were to succeed in this, would any changes to classes or codebehind require any sort of recompiling/redeployment for the project not involved?

To simplify: I have an ASP.NET project with a class, say Product.vb.  I create a new VB.NET project within that solution that wants to use Product.vb. Assuming I can even do this in the first place, would a change to Product.vb require a recompiling for both the ASP.NET project and a redeployment of my VB.NET project? If so, can anyone think of a simpler way to do this? I’d rather not create two similar Product.vb files and have to update both of them every time I want to add a feature or something.

EDIT (10/16/07): So upon re-reading this post, I noticed the solution is actually really easy. Sorry, yesterday was one of those days. If you’ve come here in search of the answer, just add a reference the project’s DLL.

October 15, 2007 Posted by | ASP.NET, VB.NET | Leave a comment

ASP.NET/MySQL Open Source Forum Software: Does It Exist?

I’ve been looking for a decent open source forum in ASP.NET that hooks up to a MySQL database, but it looks like there’s a serious dearth in the area of free ASP.NET forum software period!  So far I have found two.  

  • YetAnotherForum is an open-source app written in .NET (C#), but does not hook up to a MySQL server. I looked through the source code and started getting dizzy, as it’s very convoluted. Should I wish to customize it, it looks like I would have to take many hours to decipher what goes where and is called by what all over the place.  However, I’ve never really considered what goes into the making of forum software, so maybe this is just par for the course. I may end up sucking it up and going with this software, but I won’t like having two databases (MSSQL and MySQL).
  • The second .NET forum app I found was dnfBB. This is more of a plain-Jane-looking forum, but with some CSS, I’m sure I’d be able to fix it up.  It supposedly hooks up to MySQL (and Firebird and MSSQL). I say “supposedly” because I could not get this forum to install on my machine, and it’s not really open source.  I can’t really say much about it other than I couldn’t get it to work, as I received an error message during installation.

If I can’t find some good .NET forum software soon, I may have to lower myself to implementing some sort of…sigh…PHP forum software (of which there are many).  It would be a little weird running PHP and .NET simultaneously in the same site, but if I can’t come up with anything else, that might have to do. I certainly don’t have the time (nor the desire) to write forum software myself, although I probably could. Reinventing the wheel is not much fun.

Has anyone else “deigned” to mix PHP in their site with .NET simply because the right .NET software wasn’t out there? Am I killing my credibility for even suggesting this? 🙂

October 12, 2007 Posted by | AJAX, ASP.NET, Forums, MySQL | , | 2 Comments

ASP.NET: Tutorials for Every Standard Server Control

While trying to determine the best way to display certain data, I came across a veritable goldmine of information: Microsoft’s ASP.NET QuickStart Tutorials.  This resource has a tutorial on every standard control in the ASP.NET library, including working samples and source code.  I learned a whole lot about controls that I thought I already knew everything about and controls that I’ve never really had a desire to learn about because they looked too complex or like something I wouldn’t need. Even if you are an “expert” programmer, perusing these tutorials will teach you a thing or two.

Especially check out the section on the DataControls. I didn’t even know there was such a thing as a DataList control, and now I don’t know if I’ll use a GridView again!  Really great resource.

October 10, 2007 Posted by | ASP.NET, GridView, Resources, Tips & Tricks | 1 Comment

ASP.NET and AJAX Toolkit: Is Anyone Else Having Problems With the Accordion Control?

Ever since version 10301 of the AJAX Toolkit, the Accordion control has been acting a little wonky for me, but only in certain circumstances.  The recent release of the AJAX Toolkit still has not addressed this issue.  I’ve posted about it in the ASP.NET forums hoping that someone will get back to me or that they will at least address the issue.  I’m not the only one having problems, but apparently I’m the only one having this particular problem: basically, my Accordion control’s headers’ text is set at text-indent: -5000px to make the text not appear on the page so I can replace it with a background image. Pretty standard procedure. But when using the versions of the ASP.NET AJAX Toolkit later than 10301, when the user clicks on a header to get the content in the AccordionPane, all the background images for the headers disappear and the text returns. It’s a really weird bug that I don’t think they’ve quite figured out how to fix, but I am confident that they will. Check the link above for my full code.

When they figure out what’s wrong and fix it, you’ll see it here.

EDIT 10/15/07: I’ve been informed of a work-around for those who like workarounds. Click the link above to my post to the ASP.NET forums and read the thread.  It’s definitely a workaround and a bit kludgy, but it will work.

October 10, 2007 Posted by | Accordion, AJAX, AJAX Toolkit, ASP.NET, Bugs | Leave a comment