在代码后面的gridview事件处理程序中切换DIV可见性

时间:2013-06-19 09:21:58

标签: c# javascript asp.net webforms lifecycle

我有以下gridview和“loadingbar”div:

<asp:GridView ID="importLogGV" 
    AutoGenerateColumns="false" 
    DataKeyNames="id" 
    OnRowDeleting="DeleteStuff" 
    runat="server" 
    CssClass="searchListGridView">
<Columns>
<asp:BoundField HeaderText="ID" DataField="id">
    [...]
<asp:TemplateField ItemStyle-Width="120">
    <ItemTemplate>
        <asp:LinkButton ID="lnkDel" CommandName="Delete" Text='Delete' runat="server" 
         OnClientClick="return confirm('Really delete?');" />
    </ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

<div id="deleting" style="text-align: center" Visible="False" runat="server" 
 ClientIDMode="Static">Delete in progress...<br />
<asp:Image runat="server" ID="Image1" ImageUrl="~/Images/ajax-loadingbar.gif"/>
</div>

虽然在后面的代码中运行事件处理程序“DeleteStuff”(它可以执行大量数据库操作,但最多可能需要10秒),但我需要显示“删除”div,并且当事件处理程序完成时需要再次隐藏div

这是我在codebehind中删除的事件处理程序:

protected void DeleteStuff(object sender, GridViewDeleteEventArgs e)
{
    deleting.Visible = true;

    System.Threading.Thread.Sleep(5000);

    deleting.Visible = false;
}

我尝试使用style:display而不是visible属性,我尝试使用asp:panel而不是div,但无论我做什么,eventhandler都会运行,但div在运行时从不显示。< / p>

我想在用户点击删除按钮时用javascript显示div,但是在DeleteStuff过程完成后我怎么能再次隐藏它?

2 个答案:

答案 0 :(得分:2)

每当您单击要删除的按钮时,都会向服务器发送提交请求,并且您的页面将开始刷新。在服务器上的方法完成执行后,将重新呈现该页面。所以无论你在方法中做什么,最后的效果都将保留。如果你想在浏览器上显示繁忙的文字,那就是ajax;你将不得不使用更新面板。试试这个:

javascript:

<script type="text/javascript">
    function showDeleteConfirm()
    {
        var isSure = confirm('Really delete?');
        if(isSure)
        {
            document.getElementById("deleting").style['display'] = 'block';
        }
    }
</script>

aspx标记:

<asp:UpdatePanel id="up1" runat="server">
    <ContentTemplate>
    <asp:GridView ID="importLogGV" 
        AutoGenerateColumns="false" 
        DataKeyNames="id" 
        OnRowDeleting="DeleteStuff" 
        runat="server" 
        CssClass="searchListGridView">
    <Columns>
    <asp:BoundField HeaderText="ID" DataField="id">
        [...]
    <asp:TemplateField ItemStyle-Width="120">
        <ItemTemplate>
            <asp:LinkButton ID="lnkDel" CommandName="Delete" Text='Delete' runat="server" 
             OnClientClick="return showDeleteConfirm();" />
        </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>

    <div id="deleting" style="text-align: center" style="display:none" runat="server" 
     ClientIDMode="Static">Delete in progress...<br />
    <asp:Image runat="server" ID="Image1" ImageUrl="~/Images/ajax-loadingbar.gif"/>
    </div>
    </ContentTemplate>
</asp:UpdatePanel>

代码背后:

protected void DeleteStuff(object sender, GridViewDeleteEventArgs e)
{
    //... Real task here
    System.Threading.Thread.Sleep(5000);
    deleteing.Style['display'] = 'none';
}

顺便说一句,您需要在页面上添加ScriptManager

答案 1 :(得分:0)

你试过了吗?

protected void DeleteStuff(object sender, GridViewDeleteEventArgs e)
{
    if (CheckAccess())
    {
        deleting.Visible = true;
        System.Threading.Thread.Sleep(5000);   
        deleting.Visible = false;
    }
    else
    {
        Dispatcher.BeginInvoke((Action) (() => DeleteStuff(sender, e)));
    }
}
不过,你应该使用Timer而不是Thread.Sleep(5000)。