动态添加控件不执行回发

时间:2021-04-09 00:03:20

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.

我正在覆盖网格,添加一些客户功能。其中一项功能是调整页面大小的下拉菜单。我正在使用客户服务器控件扩展网格,这对我迄今为止所做的工作非常有用。现在,我在动态添加控件进行回发时遇到了一些麻烦。启动回发的javascript不存在。

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

HTML是

<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?

那么,这与我注射'控件'时有关吗?是否与动态添加控件有关?

3 个解决方案

#1


2  

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

但我不确定这是否是您需要的全部工作

#2


1  

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).

您需要将“AutoPostBack”设置为true,以便下拉列表进行回发。否则,另一个控件必须将表单发回(但是,当确实发生时,将触发SelectedIndexChanged事件)。

#3


1  

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.

我认为需要创建控件pageSizeDropdown并在页面生命周期的早期连接事件,请参阅http://msdn.microsoft.com/en-us/library/ms178472.aspx。需要在页面LoadComplete事件之前创建动态添加的控件,以便可以触发其控件事件。

#1


2  

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

但我不确定这是否是您需要的全部工作

#2


1  

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).

您需要将“AutoPostBack”设置为true,以便下拉列表进行回发。否则,另一个控件必须将表单发回(但是,当确实发生时,将触发SelectedIndexChanged事件)。

#3


1  

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.

我认为需要创建控件pageSizeDropdown并在页面生命周期的早期连接事件,请参阅http://msdn.microsoft.com/en-us/library/ms178472.aspx。需要在页面LoadComplete事件之前创建动态添加的控件,以便可以触发其控件事件。