如何使用__doPostBack函数asp.net [英] how to use __doPostBack function in asp.net

查看:110
本文介绍了如何使用__doPostBack函数asp.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用 __ doPostBack 功能,这样我就可以迫使我的网页做包装后的页面加载,但我有一些困难的认识。

i am trying to use __doPostBack function so i can force my page to do post pack on the page load but i am having some difficulties understanding.

。在按一下按钮,我想做回发,但不知道如何完成在code中的code后面。

when i was looking at the examples online. On button click, i want to do the post back but not sure how to complete the code in the code behind.

下面是我到目前为止有:

Here is what i have so far:

<script type="text/javascript">
        function DoPostBack() {
            __doPostBack('btnNew', 'MyArgument');
        }
    </script>

下面是我的按钮

 <asp:Button ID="btnNew" runat="server" CausesValidation="False" CommandName="New" OnClick="DoPostBack()" Text="Create" />

我似乎并不明白在code用MyArgument的背后。我需要什么,仅次于所以它回来后在页面加载code呢?在此先感谢了帮助。

I don't seem to understand to use "MyArgument" in the code behind. What do i need to do in the code behind so it does post back on the page load? thanks in advance for the assistance.

推荐答案

情景1

如果你想使用你的JavaScript 功能触发回发,则需要使用的OnClientClick更换的OnClick 。修改这样的按钮定义(我已经假设它嵌套是一个的UpdatePanel

If you want to use your JavaScript function to trigger the postback, you need to replace the OnClick with OnClientClick. Modify the button definition like this (I'm already assuming that it's nested inside an UpdatePanel):

<asp:Button ID="btnNew" 
    runat="server" 
    CausesValidation="False" 
    CommandName="New" 
    OnClientClick="DoPostBack();" 
    Text="Create" />

在code后面,在的Page_Load 方法,阅读请求[__ EVENTTARGET] 请求[__ EVENTARGUMENT]

In the code behind, in the Page_Load method, read the Request["__EVENTTARGET"] and the Request["__EVENTARGUMENT"]:

protected void Page_Load(object sender, EventArgs e)
{
    if (Page.IsPostBack)
    {
        if (Request["__EVENTTARGET"] == "btnNew" && 
            Request["__EVENTARGUMENT"] == "MyArgument")
        {
            //do something
        }
    }
}

情景2

如果您不一定要使用JavaScript ,修改按钮的定义是这样的:

If you don't necessarily want to use JavaScript, modify the button's definition like this:

<asp:Button ID="btnNew"
    runat="server"
    CausesValidation="False"
    CommandName="New"
    OnClick="DoPostback"
    CommandArgument="MyArgument"
    Text="Create" />

然后,在后面的code添加下面的方法:

Then, add the following method in the code behind:

protected void DoPostback(object sender, EventArgs e)
{
    var target = ((Button)(sender)).ClientID; //"btnNew"
    var argument = ((Button)(sender)).CommandArgument; //"MyArgument"
    if (target == "btnNew" &&
        argument == "MyArgument")
    {
        //do something
    }
}

这篇关于如何使用__doPostBack函数asp.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆