用静态方法中的数据重新加载ASP.Net页

时间:2019-05-24 21:15:28

标签: javascript c# asp.net webforms

我在ASP.Net页面上有一个静态方法,在该方法的结尾,我要重新加载页面。我的问题是这是一个搜索页面,而我的静态方法是从数据库的页面搜索结果中删除一个项目。因此,刷新后该项目应从页面上的GridView中消失。

为什么这是静态方法?因为这是从页面上的javascript调用的WebMethod。

下面是我的代码。首先是javascript:

<script type="text/javascript">
    function delItem(assetID) {
        if (confirm('Are you sure you want to delete this?'))
            PageMethods.DelItem(assetID);
    }
</script>

GridView的标记:

<table><tr><td><asp:GridView runat="server" AutoGenerateColumns="false" ID="gvResults" CssClass="table table-hover table-striped" GridLines="None" AllowSorting="true" 
                 AllowPaging="true" OnSorting="gvResults_Sorting" OnPageIndexChanging="gvResults_PageIndexChanging" OnRowDataBound="gvResults_RowDataBound" DataKeyNames="AssetID" 
                EmptyDataText="No Results!" EmptyDataRowStyle-Font-Bold="true" EmptyDataRowStyle-BackColor="AliceBlue" EmptyDataRowStyle-Font-Size="XX-Large" EmptyDataRowStyle-Font-Names="Calibri">
                        <RowStyle HorizontalAlign="Center"  />
                        <HeaderStyle Font-Size="Large" />
                        <PagerStyle HorizontalAlign="Right" Font-Size="Medium" CssClass="pagination-ys"  />
            <Columns>
                <asp:BoundField DataField="AssetName" SortExpression="AssetName" HeaderText="Name" />
                <asp:BoundField DataField="AssetType" SortExpression="AssetType" HeaderText="Asset Type" />
                <asp:BoundField DataField="ManName" SortExpression="ManName" HeaderText="Manufacturer" />
                <asp:BoundField DataField="ResponsiblePerson" SortExpression="ResponsiblePerson" HeaderText="Primary Support" />
                <asp:BoundField DataField="IPAddress" SortExpression="IPAddress" HeaderText="IP Address" />
                <asp:BoundField DataField="OSName" SortExpression="OSName" HeaderText="OS" />
                <asp:BoundField DataField="Office" SortExpression="Office" HeaderText="Location" />
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:HyperLink ID="hlEdit" runat="server" ImageUrl="~/images/pencil-img.png" Target="_parent" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:HyperLink ID="hlDelete" runat="server" ImageUrl="~/images/trash-img.png" NavigateUrl='<%# Eval("AssetID", "javascript:delItem({0});") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
                   </asp:GridView></td></tr></table>

请注意,最后一个HyperLink调用javascript方法delItem。

最后,隐藏代码:

 [System.Web.Services.WebMethod()]
    public static void DelItem(int assetID)
    {
        ServerContext context = new ServerContext();
        string paramNames = @"@AssetID ";
        string storedProc = "EXEC delAsset " + paramNames;
        object[] sqlParam = new object[]
        {
            new SqlParameter("@AssetID", assetID)
        };

        try
        {
            context.Database.ExecuteSqlCommand(storedProc, sqlParam);
        }
        catch (SqlException sqlex)
        {
            string err;
            if (sqlex.InnerException != null)
                err = sqlex.InnerException.Message;
        }
        catch (Exception ex)
        {
            string err;
            if (ex.InnerException != null)
                err = ex.InnerException.Message;
        }
    }

在try块之后,我尝试调用javascript重新加载页面:

ScriptManager.RegisterClientScriptBlock((Page)(HttpContext.Current.Handler), typeof(Page), "", "document.location.reload(true);", true);

我也尝试过window.location.reload。尽管有许多链接声称这是可行的方法,但是任何一个都没有发生。

我还尝试了以下方法来代替javascript:

DisplayApplicationSummary das = new DisplayApplicationSummary();
das.Search_Click();

其中DisplayApplicationSummary是页面上的类,但是由于它是在静态方法中调用的,因此当das.Search_Click()被调用时,它看不到页面上已输入的任何搜索参数。 / p>

这是单击“搜索”按钮时的缩写形式:

public Hashtable SearchParams { get; set; }

protected void Search_Click(object sender, EventArgs e)
    {
        string name_search = tbName.Text;
        int type_search = Convert.ToInt32(ddlType.SelectedValue);
        int office_search = Convert.ToInt32(ddlOffice.SelectedValue);
        string respperson_search = ddlRespPerson.SelectedValue;
        int manufacturer_search = Convert.ToInt32(ddlManufacturer.SelectedValue);
        int OS_search = Convert.ToInt32(ddlOS.SelectedValue);
        int usergroup_search = Convert.ToInt32(ddlUserGroup.SelectedValue);
        string ipaddress_search = BasicSwitchIpAddress.Value;
        bool search_deleted = cbDeleted.Checked;

        Hashtable searchparams = new Hashtable();
        searchparams.Add("name", name_search);
        searchparams.Add("type", type_search);
        searchparams.Add("office", office_search);
        searchparams.Add("respperson", respperson_search);
        searchparams.Add("manufacturer", manufacturer_search);
        searchparams.Add("os", OS_search);
        searchparams.Add("usergroup", usergroup_search);
        searchparams.Add("ipaddress", ipaddress_search);
        searchparams.Add("searchdeleted", search_deleted);

        SearchParams = searchparams;

        Search_Click();
    }

public void Search_Click()
    {
        string name_search;
        int type_search;
        int office_search;
        string respperson_search;
        int manufacturer_search;
        int OS_search;
        int usergroup_search;
        string ipaddress_search;
        bool search_deleted;

        if (SearchParams == null)
        {
            name_search = tbName.Text;
            type_search = Convert.ToInt32(ddlType.SelectedValue);
            office_search = Convert.ToInt32(ddlOffice.SelectedValue);
            respperson_search = ddlRespPerson.SelectedValue;
            manufacturer_search = Convert.ToInt32(ddlManufacturer.SelectedValue);
            OS_search = Convert.ToInt32(ddlOS.SelectedValue);
            usergroup_search = Convert.ToInt32(ddlUserGroup.SelectedValue);
            ipaddress_search = BasicSwitchIpAddress.Value;
            search_deleted = cbDeleted.Checked;
        }
        else
        {
            name_search = SearchParams["name"].ToString();
            type_search = Convert.ToInt32(SearchParams["type"]);
            office_search = Convert.ToInt32(SearchParams["office"]);
            respperson_search = SearchParams["respperson"].ToString();
            manufacturer_search = Convert.ToInt32(SearchParams["manufacturer"]);
            OS_search = Convert.ToInt32(SearchParams["os"]);
            usergroup_search = Convert.ToInt32(SearchParams["usergroup"]);
            ipaddress_search = SearchParams["ipaddress"].ToString();
            search_deleted = Convert.ToBoolean(SearchParams["searchdeleted"]);
        }
...
}

如您在上面看到的,我尝试创建一个名为SearchParams的公共Hashtable变量,并在首次单击搜索按钮时将其填充为搜索参数,但是当Search_Click()为时,静态方法当然看不到Hashtable再次打来电话。

所以我很困惑。总之,我想刷新搜索结果页面以反映一项已被删除的事实。我该怎么办?

0 个答案:

没有答案