从级联下拉列表刷新更新面板

时间:2021-11-24 03:17:49

I am developing a webpage that will utilize two cascading drop down lists and an update panel that contains a nested gridview.

我正在开发一个网页,它将使用两个级联下拉列表和一个包含嵌套gridview的更新面板。

I have managed to get the cascading drop down lists to work correctly, and I have also managed to get my nested gridview working.

我已经设法让级联下拉列表正常工作,我也设法让我的嵌套gridview工作。

The next step is to populate the gridview based on the selection of the second cascading drop down list, which is where the update panel comes into play. I figured that once a selection has been made to the second drop down list, I could then refresh the update panel and load my nested gridviews. Looking through the documentation, I realized that I needed a trigger of some sort to force the update panel to reload, but I must not be doing something correctly...

下一步是根据第二个级联下拉列表的选择来填充gridview,这是更新面板发挥作用的地方。我想,一旦选择了第二个下拉列表,我就可以刷新更新面板并加载我的嵌套网格视图。浏览文档,我意识到我需要某种触发器来强制更新面板重新加载,但我不能正确地做某事......

Within my update panel, I use the trigger parameter, where cddMachine is the name of the second drop down list.

在我的更新面板中,我使用trigger参数,其中cddMachine是第二个下拉列表的名称。

<Triggers>
    <asp:AsyncPortBackTrigger ControlID="cddMachine" EventName="SelectedIndexChanged" />
</Triggers>

Upon running my code, the following error is thrown:

运行我的代码时,会抛出以下错误:

Control with ID 'cddMachine' being registered through 
RegisterAsyncPostBackControl or RegisterPostBackControl must implement either
INamingContainer, IPostBackDataHandler, or IPostBackEventHandler

After searching for a while, I have been unable to find any clear information as to how one would go about refreshing an update panel by means of a cascading drop down list. I'm sure I'm overlooking something simple, but I am not sure what it is that I need to look for.

搜索了一段时间后,我一直无法找到任何关于如何通过级联下拉列表刷新更新面板的明确信息。我确定我忽略了一些简单的事情,但我不确定我需要寻找什么。

EDIT - Included code

编辑 - 包含的代码

lockout.aspx

lockout.aspx

<%@ Page Title="" Language="vb" AutoEventWireup="true" MasterPageFile="~/Site.Master" CodeBehind="Lockout.aspx.vb" Inherits="LockoutTagout.Lockout" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>


<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<asp:ScriptManager ID="asm" runat="server" />
<div>
    Complex:
    <asp:DropDownList ID="ComplexList" runat="server" />
    &nbsp;&nbsp; Machine:
    <asp:DropDownList ID="MachineList" runat="server" AutoPostBack="true" />

    <asp:CascadingDropDown ID="cddComplex" runat="server"
        ServicePath="~/lockoutService.asmx" ServiceMethod="GetComplex"
        TargetControlID="ComplexList" Category="Complex"
        PromptText="Select Complex" />

    <asp:CascadingDropDown ID="cddMachine" runat="server"
        ServicePath="~/lockoutService.asmx" ServiceMethod="GetMachine"
        TargetControlID="MachineList" ParentControlID="ComplexList"
        Category="Machine" PromptText="Select Machine" />
</div>

<div>
    <asp:UpdatePanel runat="server">
        <ContentTemplate>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server"
                ConnectionString="<%$ ConnectionStrings:LOTOConnectionString %>"
                SelectCommand="SELECT * FROM [DEVICES] WHERE machine_id = @machine_id">
                <SelectParameters>
                    <asp:ControlParameter Name="machine_id" ControlID="cddMachine" PropertyName="SelectedValue" />
                </SelectParameters>
            </asp:SqlDataSource>
            <asp:GridView ID="GridView1" GridLines="None" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="devices_id" DataSourceID="SqlDataSource1" AlternatingRowStyle-BackColor="White">
                <Columns>
                    <asp:BoundField DataField="devices_id" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="devices_id" />
                    <asp:BoundField ItemStyle-HorizontalAlign="Center" DataField="description" HeaderText="Description" SortExpression="description" />
                    <asp:BoundField ItemStyle-HorizontalAlign="Center" DataField="location" HeaderText="Location" SortExpression="location" />
                    <asp:BoundField ItemStyle-HorizontalAlign="Center" DataField="verification" HeaderText="Verification" SortExpression="verification" />
                    <asp:BoundField ItemStyle-HorizontalAlign="Center" DataField="rack_num" HeaderText="Rack_#" SortExpression="rack_num" />
                    <asp:BoundField ItemStyle-HorizontalAlign="Center" DataField="section_num" HeaderText="Section_#" SortExpression="section_num" />
                    <asp:BoundField ItemStyle-HorizontalAlign="Center" DataField="color_or_number" HeaderText="Key_Identifier" SortExpression="color_or_number" />
                    <asp:BoundField ItemStyle-HorizontalAlign="Center" DataField="normal_position" HeaderText="Normal_Position" SortExpression="normal_position" />
                    <asp:TemplateField HeaderText="P&amp;P Numbers" ControlStyle-Width="100px">
                        <ItemTemplate>
                            <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:LOTOConnectionString %>" SelectCommand="SELECT * FROM [PNP] WHERE ([devices_id] = @devices_id)">
                                <SelectParameters>
                                    <asp:Parameter Name="devices_id" Type="Int32" />
                                </SelectParameters>
                            </asp:SqlDataSource>
                            <asp:GridView ID="GridView2" ShowHeader="false" GridLines="None" runat="server" AutoGenerateColumns="False" DataKeyNames="pnp_id" DataSourceID="SqlDataSource2">
                                <Columns>
                                    <asp:BoundField ItemStyle-HorizontalAlign="Center" DataField="pnp_num" HeaderText="pnp_num" SortExpression="pnp_num" />
                                </Columns>
                            </asp:GridView>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <HeaderStyle BackColor="#a52138" ForeColor="White" Font-Size="Medium" />
            </asp:GridView>
        </ContentTemplate>
    </asp:UpdatePanel>
</div>

lockout.aspx.vb

lockout.aspx.vb

Public Class Lockout
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load, MachineList.SelectedIndexChanged

End Sub

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim s As SqlDataSource = CType(e.Row.FindControl("SqlDataSource2"), SqlDataSource)
        s.SelectParameters(0).DefaultValue = e.Row.Cells(0).Text
    End If
End Sub
End Class

lockoutService.asmx.vb

lockoutService.asmx.vb

Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports AjaxControlToolkit
Imports System.Data.SqlClient

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
<System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class lockoutService
    Inherits System.Web.Services.WebService

    <WebMethod()> _
    Public Function GetComplex(ByVal knownCategoryValues As String, ByVal category As String) As CascadingDropDownNameValue()
    Dim conn As New SqlConnection("Data Source=WIWRMS-SQLD2\SQLD2;Initial Catalog=LOTO;Persist Security Info=True;User ID=LOTO_ADMIN;Password=lotoadmin")
    conn.Open()
    Dim comm As New SqlCommand( _
        "SELECT * FROM complex", conn)
    Dim dr As SqlDataReader = comm.ExecuteReader()
    Dim l As New List(Of CascadingDropDownNameValue)
    While (dr.Read())
        l.Add(New CascadingDropDownNameValue(dr("complex_name").ToString(), dr("complex_id").ToString()))
    End While
    conn.Close()
    Return l.ToArray()
End Function

<WebMethod()> _
Public Function GetMachine(ByVal knownCategoryValues As String, ByVal category As String) As CascadingDropDownNameValue()
    Dim complex_id As Integer
    Dim kv As StringDictionary = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues)

    If Not kv.ContainsKey("complex") Or Not Int32.TryParse(kv("complex"), complex_id) Then
        Throw New ArgumentException("Couldn't find complex.")
    End If

    Dim conn As New SqlConnection("Data Source=WIWRMS-SQLD2\SQLD2;Initial Catalog=LOTO;Persist Security Info=True;User ID=LOTO_ADMIN;Password=lotoadmin")
    conn.Open()
    Dim comm As New SqlCommand( _
        "SELECT * FROM machine WHERE complex_id = @complex_id", conn)
    comm.Parameters.AddWithValue("@complex_id", complex_id)
    Dim dr As SqlDataReader = comm.ExecuteReader()
    Dim l As New List(Of CascadingDropDownNameValue)
    While (dr.Read())
        l.Add(New CascadingDropDownNameValue(dr("machine_name").ToString(), dr("machine_id").ToString()))
    End While
    conn.Close()
    Return l.ToArray()
End Function
End Class

1 个解决方案

#1


1  

A CascadingDropDown is really just an extender on a DropDownList, so you should be able to remove the trigger, set your final DropDownList's AutoPostBack property to true, then handle its SelectedIndexChanged event. That should automatically trigger the PartialPostBack so that you can update your GridView accordingly.

CascadingDropDown实际上只是DropDownList的扩展器,因此您应该能够移除触发器,将最终的DropDownList的AutoPostBack属性设置为true,然后处理其SelectedIndexChanged事件。这应该会自动触发PartialPostBack,以便您可以相应地更新GridView。

Additional details for fix:

修复的其他细节:

I'm thinking you may want to change the following in your DataSource markup: <asp:ControlParameter Name="machine_id" ControlID="cddMachine" PropertyName="SelectedValue" /> change ControlID to MachineList.

我想你可能想在你的DataSource标记中更改以下内容: 将ControlID更改为MachineList。 :controlparameter>

#1


1  

A CascadingDropDown is really just an extender on a DropDownList, so you should be able to remove the trigger, set your final DropDownList's AutoPostBack property to true, then handle its SelectedIndexChanged event. That should automatically trigger the PartialPostBack so that you can update your GridView accordingly.

CascadingDropDown实际上只是DropDownList的扩展器,因此您应该能够移除触发器,将最终的DropDownList的AutoPostBack属性设置为true,然后处理其SelectedIndexChanged事件。这应该会自动触发PartialPostBack,以便您可以相应地更新GridView。

Additional details for fix:

修复的其他细节:

I'm thinking you may want to change the following in your DataSource markup: <asp:ControlParameter Name="machine_id" ControlID="cddMachine" PropertyName="SelectedValue" /> change ControlID to MachineList.

我想你可能想在你的DataSource标记中更改以下内容: 将ControlID更改为MachineList。 :controlparameter>