运行的Page_Load前的按钮事件处理函数 [英] Run the button event handler before page_load

查看:93
本文介绍了运行的Page_Load前的按钮事件处理函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我有我的数据库中的所有对象的表格。我加载它们在我的Page_Load函数。我有一个文本字段和一个按钮,点击该按钮时,我想的是,处理器点击把一个新的对象,写在文本字段在数据库中的名称。

现在,我想这是在点击后会发生什么情况是,在表中的新项目重新加载页面。问题是,该按钮事件处理程序在Page_Load功能之后运行。

解决的办法是使用的IsPostBack在Page_Load或使用pre加载功能。一个问题是,如果我将有三个不同的按钮,我会在它们之间相差有3个不同的方便的功能的,而不是有

这是没有这个问题的任何解决方案?

code:

 保护无效的Page_Load(对象发件人,EventArgs的发送)
        {
            如果(会话[用户id] == NULL)
                的Response.Redirect(的Login.aspx);            // LOAD DATA FROM DB
        }        保护无效的CreateObject(对象发件人,EventArgs的发送)
        {
            //保存新对象
        }


解决方案

也许你应该尝试preRender而不是加载过程中加载数据。

 保护无效的Page_Load(对象发件人,EventArgs的发送)
    {
        这一点。preRender + = Page_ preRender
        如果(会话[用户id] == NULL)
            的Response.Redirect(的Login.aspx);
    }    保护布尔reloadNeeded {搞定;组;}    保护无效的CreateObject(对象发件人,EventArgs的发送)
    {
        //保存新对象
        reloadNeeded = TRUE;
    }
    保护无效Page_ preRender(对象发件人,EventArgs的发送)
    {
        如果(reloadNeeded ||!的IsPostBack)
        // LOAD DATA FROM DB
    }

I have a table with all the objects I have in my db. I load them in my Page_Load function. I have a text field and a button that when clicking the button, I want the handler of that click to put a new object with the name written in the text field in the db.

Now, I want that what happens after the click is that the page loads again with the new item in the table. The problem is that the button event handler is run after the Page_Load function.

A solution to this would be to use IsPostBack in the Page_Load or use the pre load function. A problem is that if I would have 3 different buttons, I would have to differ between them there instead of having 3 different convenient functions.

Any solutions that don't have this problem?

Code:

protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["userId"] == null)
                Response.Redirect("Login.aspx");

            // LOAD DATA FROM DB
        }

        protected void CreateObject(object sender, EventArgs e)
        {
            // SAVE THE NEW OBJECT
        }

解决方案

Maybe you should try loading your data during PreRender instead of Load

    protected void Page_Load(object sender, EventArgs e)
    {
        this.PreRender += Page_PreRender
        if (Session["userId"] == null)
            Response.Redirect("Login.aspx");
    }

    protected bool reloadNeeded {get; set;}

    protected void CreateObject(object sender, EventArgs e)
    {
        // SAVE THE NEW OBJECT
        reloadNeeded = true;
    }


    protected void Page_PreRender(object sender, EventArgs e)
    {
        if(reloadNeeded || !IsPostBack)
        // LOAD DATA FROM DB
    }

这篇关于运行的Page_Load前的按钮事件处理函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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