简体   繁体   English

如何使用jQuery在C#中的代码后面获取单击事件

[英]How to get click event in code behind in c# using jquery

Hi Please check i want to get click event in code behind as i am using master page concept and i have one child form of it in this page i have ContentPlaceHolder, My button "btnSubmit" this is a linkbutton which is under GridView. 您好,请检查我想在代码后面获取点击事件,因为我正在使用母版页概念,并且在此页中有一个子窗体,我具有ContentPlaceHolder,我的按钮“ btnSubmit”是位于GridView下的linkbutton。 i want loading image when i will click on btnSubmit button. 我想在单击btnSubmit按钮时加载图像。 please check and help. 请检查并提供帮助。

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
function ShowProgress() {
    setTimeout(function () {
        var modal = $('<div />');
        modal.addClass("modal");
        $('body').append(modal);
        var loading = $(".loading");
        loading.show();
        var top = Math.max($(window).height() / 2 - loading[0].offsetHeight / 2, 0);
        var left = Math.max($(window).width() / 2 - loading[0].offsetWidth / 2, 0);
        loading.css({ top: top, left: left });
    }, 200);
}
$('form').live("submit", function () {
    ShowProgress();
});

aspx page.. aspx页面..

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
    <asp:BoundField DataField="Journal" HeaderText="Customer Id" />
    <asp:BoundField DataField="ISBN" HeaderText="Contact Name" />
    <asp:BoundField DataField="Status" HeaderText="City" />
    <asp:TemplateField HeaderText="Btn">
        <ItemTemplate>
            <asp:Button ID="btnSubmit" runat="server" Text="Load Customers"
OnClick="btnSubmit_Click"  />
        </ItemTemplate>
    </asp:TemplateField>
</Columns>
</asp:GridView>
<div class="loading" align="center">
Downloading Files. Please wait<br />
<br />
<img src="loader.gif" alt="" />
</div>
</asp:Content>

In cs Page. 在cs页面中。

 protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        string script = "$(document).ready(function () { $('[id*=btnSubmit]').click(); });";
        ClientScript.RegisterStartupScript(this.GetType(), "load", script, true);
    }       
}

try this way 尝试这种方式

Script AS 脚本AS

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
    function ShowProgress() {
        setTimeout(function () {
            var modal = $('<div />');
            modal.addClass("modal");
            $('body').append(modal);
            var loading = $(".loading");
            loading.show();
            var top = Math.max($(window).height() / 2 - loading[0].offsetHeight / 2, 0);
            var left = Math.max($(window).width() / 2 - loading[0].offsetWidth / 2, 0);
            loading.css({ top: top, left: left });
        }, 200);
    }
    $('form').live("submit", function () {
        ShowProgress();
    });
</script>

And Add event onrowdatabound in grid 并在网格中添加事件onrowdatabound

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
 onrowdatabound="GridView1_RowDataBound">

And CS page 和CS页面

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType == DataControlRowType.DataRow)
    {    
         Button btnSubmit = e.Row.FindControl("btnSubmit") as Button;
         btnSubmit.Attributes.Add("OnClick", "ShowProgress();");
    }
}

也使用按钮的OnClientClick事件在此事件中调用javascript函数,该函数将进度条的css更改为可见。

OnClientClick="ShowProgress();return true;"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM