ModalPopup Extender automatic close when PostBack

Guys, I found a trick to solve the AjaxControlToolkit's ModalPopup Extender automatic close when PostBack.

  1. Dont use the "TargetControlID" to call the ModalPopup, but "TargetControlID" cannot be empty, you must set it.

  2. Add a new HiddenField control, e.g: "HiddenField1"

  3. Set the "TargetControlID" to "HiddenField1"

  4. but without set "TargetControlID", how to call it?
    the easy way is use "Show()" method. e.g:



    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
                ModalPopupExtender1.Show()
    End Sub



  5. "OkControlID" and "CancelControlID" also do not set it.
    Use the "Hide" method.


    Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
                ModalPopupExtender1.Hide()
    End Sub




You are almost solve the problem, but if you still have problem. You can try this:
  1. Use the ViewState keep track the Popup Window is display or not

    Public Property PopupIsDisplay() As Boolean
                Get
                    Return ViewState("PopupIsDisplay")
                End Get
                Set(ByVal value As Boolean)
                    ViewState("PopupIsDisplay") = value
                End Set
    End Property

  2. When you click the button to show popup window, update the ViewState

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
                ModalPopupExtender1.Show()
                PopupIsDisplay=True
    End Sub
     
  3. When hide the popup window,

    Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
                ModalPopupExtender1.Hide()
               
    PopupIsDisplay=False
    End Sub

  4. Finally, In "Form_Load", double check the popup window is display or not, if not, show again.

                If PopupIsDisplay Then
                    ModalPopupExtender1.Show()
                Else
                    ModalPopupExtender1.Hide()
                End If


Sorting in Generic List(T)

  1. I will create Customer object



    Class Customer
    Private _Name As String
    Private _Age As Integer

    Public Property Name() As String
    Get
    Return _Name
    End Get
    Set(ByVal value As String)
    _Name = value
    End Set
    End Property
    Public Property Age() As Integer
    Get
    Return _Age
    End Get
    Set(ByVal value As Integer)
    _Age = value
    End Set
    End Property
    End Class



  2. Add two compare methods , one for compare by name, and one for compare by age



    Class Customer
    Private _Name As String
    Private _Age As Integer

    Public Property Name() As String
    Get
    Return _Name
    End Get
    Set(ByVal value As String)
    _Name = value
    End Set
    End Property
    Public Property Age() As Integer
    Get
    Return _Age
    End Get
    Set(ByVal value As Integer)
    _Age = value
    End Set
    End Property

    Public Shared Function CompareByName(ByVal c1 As Customer, _
    ByVal c2 As Customer) As Integer
    Return c1.Name.CompareTo(c2.Name)
    End Function
    Public Shared Function CompareByAge(ByVal c1 As Customer, _
    ByVal c2 As Customer) As Integer
    Return c1.Age.CompareTo(c2.Age)
    End Function

    End Class



  3. Create a Collection with generic List(T)



    Dim CustList As New List(Of Customer)

    cus1 = New Customer()
    cus1.Name = "B"
    cus1.Age = 23
    CustList.Add(cus1)

    cus2 = New Customer()
    cus2.Name = "A"
    cus2.Age = 24
    CustList.Add(cus2)

    cus3 = New Customer()
    cus3.Name = "C"
    cus3.Age = 27
    CustList.Add(cus3)



  4. To use the List.Sort(Generic Comparison) create a instance of Comparison delegate and register it with CompareByName and CompareByAge method.



    Dim comp1 As New Comparison(Of Customer)(Customer.CompareByName)
    CustList.Sort(comp1)


    For Each cus As Customer In CustList
    Console.WriteLine((cus.Name & " ") + cus.Age)
    Next

    Dim comp2 As New Comparison(Of Customer)(Customer.CompareByAge)
    CustList.Sort(comp2)

    For Each cus As Customer In CustList
    Console.WriteLine((cus.Name & " ") + cus.Age)
    Next



RowUpdating not firing!!!

I create a simple function to update some data in the GridView.

And I add a Edit button, when the edit button is clicked in a GridView control, the row is redisplayed in edit mode:

<asp:CommandField ButtonType="Image"
EditImageUrl="~/images/i_edit.gif"
ShowEditButton="True" />


In edit mode, with the data available in editable controls such as TextBox and CheckBox controls. The Edit button becomes an Update or Save button, and when users click it, the RowUpdating event will be firing.

when I finish my code, when I click the "Update link", the RowUpdating event is not firing!!!
I try to debug it, yes, it is totally not firing the RowUpdating event. I dont know what happen?!?!

Finally, I found the problem, Which is because I set the ButtonType="Image", I want used an icon become a button, and I also set EditImageUrl="~/images/i_edit.gif", but I leave the UpdateImageUrl blank!?!?! so that why it cannot call the RowUpdating event.

<asp:CommandField ButtonType="Image"
EditImageUrl="~/images/i_edit.gif"
UpdateImageUrl="~/images/i_save.gif"
CancelImageUrl="~/images/i_cancel.png"
ShowEditButton="True" />

In DataGridView, Change the cell value use my own date format.

I have a datagridview with a column populated from a datetime field. I would like to change the date format to "31/Dec/2009" (no follow the server date format).

I used the following code to display the date, it is work when I test in my PC.

<asp:boundfield headertext="Join Date" datafield="joindate" dataformatstring="{0:dd/MMM/yyyy}" >

but after I upload to the web server, it cannot work!

After research, a setting in the grid columns - HtmlEncode="false" and it should be used when the formats as specifified in the dataformat string.

<asp:boundfield headertext="Join Date" datafield="joindate" dataformatstring="{0:dd/MMM/yyyy}" htmlencode="False"> </asp:boundfield>

How to use ReadXml? (direct read from a string xml)

Normally, we used the ReadXml to read a xml file.
but if I already have a xml in a string, ReadXml cannot directly read the xml into dataset/datatable!!!

The following example first creates a simple dataset by a xml string:

1. we has a xml string


sXml+="<root>"
sXml+=" <func>"
sXml+=" <function>Testing</function>"
sXml+=" </func>"
sXml+="</root>"



2. We convert the string into byte, and write into memory stream.

Dim myEncoder As New System.Text.ASCIIEncoding
Dim bytes As Byte() = myEncoder.GetBytes(sXml.ToString)
Dim ms As System.IO.MemoryStream = New System.IO.MemoryStream(bytes)


3. We declare the dataset first.

Dim dsRoot As New DataSet("root")
Dim dtNew As DataTable = New DataTable("func")
dtNew.Columns.Add(New DataColumn("function", GetType(String)))
dsRoot.Tables.Add(dtNew)


4. Now, we can read the xml from the memory stream into the dataset.

dsRoot.ReadXml(ms)