Nicholas Head

Filter by APML

Making jQuery UI’s Dialog Work With ASP.Net

by Nicholas Head 25. June 2009 22:06

You might have come across this problem in your travels, I know I have. Basically if you’re using jQuery UI’s Dialog and have a <form runat=”server”> inside of that dialog, your postback is not going to work. What’s going on?

jQuery UI is taking the dialog DIV content and moving it outside of the <form runat=”server”> tag. This makes your postback no longer function as expected.

Luckily we can fix this fairly easily with jQuery itself.

var dlg = $('#divMyDialog').dialog({
                    autoOpen: false,
                    modal: true,
                    width: 400,
                    resizable: false,
                    overlay: {
                        background: "black",
                        opacity: 0.5    
                    }
                });
dlg.parent().appendTo(jQuery('form:first'));

The special logic lies in the last line—using the reference to our created dialog, we get the parent container and move it back to the first <form> on the page. After that, all is well.

Bookmark and Share

Tags: ,

asp.net | jQuery

ASP.Net Trick: Getting rid of viewstate on DropDownLists

by Nicholas Head 22. February 2009 22:11

If you’ve used ASP.Net for any amount of time, you quickly realize how much it loves to dump into the viewstate. Now, I’m not claiming I’m a saint in this area, but I have been learning as I go to recognize what operations will fill the viewstate and work to prevent them.

One trick I learned recently is to re-bind your DropDownList on every postback, inside of the DropDownList’s Init event handler. You can also pull in the postback value from the Request.Form collection and set the DropDownList’s SelectedValue to what it should be. Observe the following example:

Protected Sub ddlMyDropDownList_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlMyDropDownList.Init
    Dim db = New MyDataContext()

    Dim q = From s In db.States _
            Order By s.StateName _
            Select New ListItem(s.StateName, s.StateAbbrev)

    ddlMyDropDownList.Items.AddRange(q.ToArray())

    Dim r = Request(ddlMyDropDownList.UniqueID)
    If r IsNot Nothing Then ddlMyDropDownList.SelectedValue = r
End Sub

Don’t forget to set “EnableViewState=False” on your DropDownList.

Bookmark and Share

Tags:

asp.net | programming

ASP.Net: Remember, the .Visible property also checks parent’s visibility!

by Nicholas Head 11. January 2009 20:44

new_dotnet_logo I’ve been hit by this a few times, and it’s darn confusing at first, but it makes sense (kinda.)

In ASP.Net, if you reference a control’s .Visible property, it not only checks if it’s been set to Visible = True, but also if it’s parent control is Visible = True. Only then will it return a “True” value back to you.

For example, let’s say you have a PlaceHolder, and inside the PlaceHolder you add a Label control:


   1: <asp:PlaceHolder ID="phParent" runat="server">
   2:     <asp:Label ID="lblChild" runat="server" />
   3: </asp:PlaceHolder>

And then in your code, you write:

   1: phParent.Visible = False
   2: lblChild.Visible = True
   3: Response.Write(lblChild.Visible)

Your response is going to be “False” because the Label’s parent control is not visible.

Just thought I’d share this as a warning to anyone else who might get tripped up on it.

Bookmark and Share

Tags:

asp.net | Microsoft | programming

Are you moving or looking to move soon? Don't hurt your back shifting boxes around-- use HireAHelper to Book Movers Online!

Log in

Powered by BlogEngine.NET 1.6.1.0