views:

351

answers:

3

I am overriding a Grid, adding some customer features. One of the features is a drop-down to adjust page size. I am extending the grid using a customer server control, which works great for what I've done so far. Now, however I am having a bit of trouble getting the dynamically added control to do a postback. The javascript to initiate the postback is not present.

Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
    Dim pageSizePanel As New Panel
    ...
    Dim countList As List(Of String) = GetCountList()
    Dim pageSizeDropdown As New DropDownList()
    pageSizeDropdown.ID = "pageSizeDropdown"
    pageSizeDropdown.DataSource = countList
    pageSizeDropdown.DataBind()

    AddHandler pageSizeDropdown.SelectedIndexChanged, _
               AddressOf HandlePageSizeChange

    pageSizePanel.Controls.Add(pageSizeDropdown)
    ...
    MyBase.Controls.AddAt(0, pageSizePanel)
    MyBase.OnPreRender(e)
End Sub

The HTML is

<select name="tab$grid1Tab$RadGrid1$pageSizeDropdown" 
    id="tab_grid1Tab_RadGrid1_pageSizeDropdown">
     <option selected="selected" value="10">10</option>
     <option value="20">20</option>
     <option value="40">40</option>
     <option value="80">80</option>
     <option value="All">All</option>

    </select>

So, does this have to do with when I'm 'injecting' the controls? Does it have to do with dynamic addition of the controls?

+1  A: 

You need to set "AutoPostBack" to true for a dropdown list to postback. Otherwise, another control will have to post the form back (however, the SelectedIndexChanged event will fire when that does happen).

FlySwat
+2  A: 

The first thing I noticed was you'd be missing this:

pageSizeDropdown.AutoPostBack = true

but I'm not sure if that's all you need for it to work

John
You are correct about that not being all. There is some ViewState stuff out of whack.
Greg Ogle
the 2nd part of the issue might be due to once the postback hits the server, it might not necessarily know what to do with it if that control doesn't exist when the page is rebuilt (i.e. this code that builds your datagrid hasn't been ran yet). i'd try building the grid in the page init event
John
+1  A: 

I think the control pageSizeDropdown would need to be created and the event hooked up earlier in the page lifecycle, see http://msdn.microsoft.com/en-us/library/ms178472.aspx. The dynamically added control needs to be created before the pages LoadComplete event so that its control event can fire.

PhilHoy