在asp gridview中级联下拉列表,而不是获取.asmx方法

时间:2013-12-11 21:22:02

标签: c# asp.net gridview asmx

我一直按照此页面上的说明操作:
http://csharpdotnetfreak.blogspot.com/2009/02/ajax-cascading-dropdownlist-database.html 在我的gridview中设置2个级联下拉列表,但没有来自.asmx页面的数据。

这是我的.asmx页面:

public class SorSubsystemsCascade : System.Web.Services.WebService
{
    public SorSubsystemsCascade ()
    {
        //InitializeComponents();
    }

    [WebMethod]
    public CascadingDropDownNameValue[] GetSubsystems(string knowCategoryValue, string category)
    {
        // get a list of subsystems
        string url = @"http://osw-hml3mes.novelis.biz:3020/Preheat/Downtime/Subsystems";
        List<SorSubsystem> subsystems = new List<SorSubsystem>();
        subsystems = JsonConvert.DeserializeObject<List<SorSubsystem>>(new WebClient().DownloadString(url));

        // create a list of drop downs
        List<CascadingDropDownNameValue> subsystemsList = new List<CascadingDropDownNameValue>();

        foreach(SorSubsystem sub in subsystems)
        {
            subsystemsList.Add(new CascadingDropDownNameValue(sub.Description, sub.SubSystemCode));
        }

        return subsystemsList.ToArray();
    }

    [WebMethod]
    public CascadingDropDownNameValue[] GetReasons(string knownCategoryValues, string category)
    {
        List<CascadingDropDownNameValue> reasons = new List<CascadingDropDownNameValue>();
        string url = @"http://osw-hml3mes.novelis.biz:3020/Preheat/Downtime/Reasons";

        List<SorReason> loadReason = new List<SorReason>();
        loadReason = JsonConvert.DeserializeObject<List<SorReason>>(new WebClient().DownloadString(url));

        foreach (SorReason res in loadReason)
        {
            if(res.SubSystemCode == "A")
                reasons.Add(new CascadingDropDownNameValue(res.Description, res.SubSystemCode));
        }

        return reasons.ToArray();
    }
}

对于数据,我从页面获取JSON数据并将其反序列化为对象列表。

这些是我的对象: SorReason:

public class SorReason {
        public string Code { get; set; }
        public string ShortDescription { get; set; }    
        public string Description { get; set; }    
        public string SubSystemCode { get; set; }   
        public string DisplayReason { get { return Code + " - " + ShortDescription; } }                
 }

SorSubsystem:

public class SorSubsystem {
        public string SubSystemCode { get; set; }
        public string Description { get; set; }
}

dropdownlist使用子系统下拉列表作为其父级的原因。由于某种原因,子系统下拉列表未正确填充。看起来没有调用.asmx服务。

我确实把它放在我的ScriptManager标签

<Services>
     <asp:ServiceReference Path="SorSubsystemCascade.asmx" />
</Services>

这是我页面的整个标记:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SorGrid.aspx.cs" Inherits="SorDowntimeWebApp.SorGrid" EnableEventValidation="false" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
<%@ Register TagPrefix="asp" Namespace="AjaxControlToolkit" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <meta charset="utf-8" />
        <title>Preheat SOR</title>
        <link rel="Stylesheet" href="Common/JqueryUI/css/custom-theme/jquery-ui-1.10.3.custom.min.css" />
        <style type="text/css">
            body { font-family: Museo, Arial; }
            h1 { background: #8F1626; color: #BFC5C5; width: 50%; }
            #dataGridWrapper { height: 400px; width: 100%; }
            #lblError { color: Red; font-size: 12pt; }
        </style>
        <%--<script type="text/javascript" src="Common/jQueryUI/js/jquery-1.9.1.js"></script>--%>
        <script type="text/javascript" src="Common/jquery-1.10.2.min.js"></script>
        <script type="text/javascript" src="Common/jQueryUI/js/jquery-ui-1.10.3.custom.min.js"></script>
        <script type="text/javascript">
            $(function () {
                // maxDate: 0 sets the max date allowable to today (users can't pick tomorrow as an option)
                $("#txtStartDate").datepicker({
                    showOn: "button",
                    buttonImage: "Common/Images/calendar.gif",
                    buttonImageOnly: true,
                    buttonText: "",
                    maxDate: 0
                });
                $("#txtEndDate").datepicker({
                    showOn: "button",
                    buttonImage: "Common/Images/calendar.gif",
                    buttonImageOnly: true,
                    buttonText: "",
                    maxDate: 0
                });
            });
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:ScriptManager ID="mainScriptManager" runat="server"></asp:ScriptManager>
            <ajaxToolkit:ToolkitScriptManager ID="toolkitScriptManager" runat="server" EnablePartialRendering="true" />
            <div id="formWrapper">
                <div id="toolbarWrapper">
                    <div id="dateSearchBar">
                        <h1>Preheat SOR<asp:Label ID="lblError" runat="server" Text=""></asp:Label></h1>
                        &nbsp;&nbsp;&nbsp;<asp:Label ID="lblStart" runat="server">Start Date:&nbsp;&nbsp;</asp:Label><asp:TextBox ID="txtStartDate" runat="server"></asp:TextBox>
                        &nbsp;&nbsp;&nbsp;<asp:Label ID="lblEnd" runat="server">End Date:&nbsp;&nbsp;</asp:Label><asp:TextBox ID="txtEndDate" runat="server"></asp:TextBox>
                        &nbsp;&nbsp;&nbsp;<asp:Button ID="btnSearch" runat="server" Text="Search" onclick="btnSearch_Click" />
                        &nbsp;&nbsp;&nbsp;<asp:Button ID="btnRefresh" runat="server" Text="Refresh" onclick="btnRefresh_Click" />
                    </div>
                    <div id="toolbar">
                        &nbsp;&nbsp;&nbsp;<asp:Button ID="btnExport" runat="server" Text="Export to Excel" onclick="btnExport_Click" />
                        &nbsp;&nbsp;&nbsp;<input type="button" value="Pareto" onclick="window.open('SorPareto.aspx', 'Pareto')" />
                    </div>
                </div>
                <div id="dataGridWrapper">
                    <asp:GridView AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" CellPadding="4" ForeColor="Black" 
                            ID="GridView1" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDataBound="GridView1_RowDataBound" OnRowEditing="GridView1_RowEditing" 
                            OnRowUpdating="GridView1_RowUpdating" runat="server" style="margin: 2px 5px 5px 15px;">
                        <Columns>
                            <asp:TemplateField HeaderText="Edit">
                                <ItemTemplate>
                                    <asp:LinkButton ID="btnEdit" runat="server" Text="Edit" commandName="Edit" />
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:linkbutton id="btnUpdate" runat="server" commandname="Update" text="Update" />
                                    <asp:linkbutton id="btnCancel" runat="server" commandname="Cancel" text="Cancel" />
                                </EditItemTemplate>
                            </asp:TemplateField>
                            <asp:BoundField DataField="StartTime" HeaderText="Start Time" SortExpression="StartTime" ReadOnly="true">
                                <ItemStyle Width="170px" />
                            </asp:BoundField>
                            <asp:BoundField DataField="EndTime" HeaderText="End Time" SortExpression="EndTime" ReadOnly="true">
                                <ItemStyle Width="170px" />
                            </asp:BoundField>
                            <asp:BoundField DataField="Duration" HeaderText="Duration" SortExpression="Duration" ConvertEmptyStringToNull="False" ReadOnly="true">
                                <ItemStyle Width="60px" />
                            </asp:BoundField>
                            <asp:BoundField DataField="Furnace" HeaderText="Furnace" SortExpression="Furnace" ReadOnly="true">
                                <ItemStyle Width="40px" />
                                </asp:BoundField>
                            <asp:TemplateField HeaderText="Subsystem" SortExpression="Subsystem">
                                <EditItemTemplate>
                                    <asp:DropDownList ID="ddEditSubsystem" runat="server" Width="150px" AutoPostBack="true" OnSelectedIndexChanged="ddEditSubsystem_SelectedIndexChanged">
                                    </asp:DropDownList>
                                    <ajaxToolkit:CascadingDropDown ID="cddSubsystem" runat="server" Category="Subsystem" TargetControlID="ddEditSubsystem" 
                                            ServiceMethod="GetSubsystems" PromptText="Subsystem">
                                    </ajaxToolkit:CascadingDropDown>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblSubsystem" runat="server" Text='<%# Bind("Subsystem") %>'></asp:Label>
                                </ItemTemplate>
                                <ItemStyle Width="210px" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Reason" SortExpression="Reason">
                                <EditItemTemplate>
                                    <asp:DropDownList ID="ddEditReason" runat="server" Width="100px" OnSelectedIndexChanged="ddEditReason_SelectedIndexChanged" AutoPostBack="true">
                                    </asp:DropDownList>
                                    <ajaxToolkit:CascadingDropDown ID="cddReason" runat="server" Category="Reasons" TargetControlID="ddEditReason" ParentControlID="ddEditSubsystem" 
                                            ServiceMethod="GetReasons" PromptText="Reason">
                                    </ajaxToolkit:CascadingDropDown>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblReason" runat="server" Text='<%# Bind("Reason") %>'></asp:Label>
                                </ItemTemplate>
                                <ItemStyle Width="300px" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="F/S" SortExpression="ForceScheduleFlag">
                                <EditItemTemplate>
                                    <asp:DropDownList ID="ddEditForcedSched" runat="server" Width="40px">
                                        <asp:ListItem>F</asp:ListItem>
                                        <asp:ListItem>S</asp:ListItem>
                                    </asp:DropDownList>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblForcedSched" runat="server" Text='<%# Bind("ForceScheduleFlag") %>'></asp:Label>
                                </ItemTemplate>
                                <ItemStyle Width="25px" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Operator Comments" SortExpression="OperatorComments">
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtEditOperatorComments" runat="server" Text='<%# Bind("OperatorComments") %>' Width="300px"></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblOperatorComments" runat="server" Text='<%# Bind("OperatorComments") %>'></asp:Label>
                                </ItemTemplate>
                                <ItemStyle Width="300px" />
                            </asp:TemplateField>
                        </Columns>
                        <FooterStyle BackColor="#CCCC99" ForeColor="Black" />
                        <HeaderStyle BackColor="#DF7E26" Font-Bold="True" ForeColor="#000000" Height="30px" HorizontalAlign="Center"/>
                        <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
                        <SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
                        <SortedAscendingCellStyle BackColor="#F7F7F7" />
                        <SortedAscendingHeaderStyle BackColor="#4B4B4B" />
                        <SortedDescendingCellStyle BackColor="#E5E5E5" />
                        <SortedDescendingHeaderStyle BackColor="#242121" />
                        <RowStyle HorizontalAlign="Center" />
                    </asp:GridView>
                </div>
            </div>
        </form>
    </body>
</html>

1 个答案:

答案 0 :(得分:1)

在网格页面中,您可以创建名为ASP.NET AJAX页面方法的static方法。它们本质上是页面托管的Web服务方法,如下所示:

[WebMethod]
public static CascadingDropDownNameValue[] GetSubsystems(string knowCategoryValue, string category)
{
    // get a list of subsystems
    string url = @"http://osw-hml3mes.novelis.biz:3020/Preheat/Downtime/Subsystems";
    List<SorSubsystem> subsystems = new List<SorSubsystem>();
    subsystems = JsonConvert.DeserializeObject<List<SorSubsystem>>(new WebClient().DownloadString(url));

    // create a list of drop downs
    List<CascadingDropDownNameValue> subsystemsList = new List<CascadingDropDownNameValue>();

    foreach(SorSubsystem sub in subsystems)
    {
        subsystemsList.Add(new CascadingDropDownNameValue(sub.Description, sub.SubSystemCode));
    }

    return subsystemsList.ToArray();
}

[WebMethod]
public static CascadingDropDownNameValue[] GetReasons(string knownCategoryValues, string category)
{
    List<CascadingDropDownNameValue> reasons = new List<CascadingDropDownNameValue>();
    string url = @"http://osw-hml3mes.novelis.biz:3020/Preheat/Downtime/Reasons";

    List<SorReason> loadReason = new List<SorReason>();
    loadReason = JsonConvert.DeserializeObject<List<SorReason>>(new WebClient().DownloadString(url));

    foreach (SorReason res in loadReason)
    {
        if(res.SubSystemCode == "A")
            reasons.Add(new CascadingDropDownNameValue(res.Description, res.SubSystemCode));
    }

    return reasons.ToArray();
}

  

注意:逻辑与.asmx Web服务版本相同,但在方法本身中添加了static修饰符。


现在在您的标记中,您只需要引用方法名称并省略ServicePath属性,因为Web方法现在是带有网格的页面的本地方法,如下所示:

<ajaxToolkit:CascadingDropDown ID="cddSubsystem" runat="server" 
    Category="Subsystem" TargetControlID="ddEditSubsystem"     
    ServiceMethod="GetSubsystems" PromptText="Subsystem">
</ajaxToolkit:CascadingDropDown>