C#中的页面加载

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/880003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 05:11:29  来源:igfitidea点击:

pageload in c#

c#asp.net

提问by pier

Here is a piece of code that I've written. The problem that I'm having is: When i click a button in the gridview "rowcommand" adds the item to an arraylist which works fine. After the user clicks the button the page loads again and it goes to "rowcommand" AGAIN! As a result the same value is added to the arraylist.

这是我写的一段代码。我遇到的问题是:当我单击 gridview“rowcommand”中的一个按钮时,将该项目添加到一个工作正常的数组列表中。用户单击按钮后,页面再次加载并再次转到“rowcommand”!结果相同的值被添加到数组列表中。

Is this something regarding postback? if it's I dont think I've understood it clearly enough! what seems to be wrong here?

这是关于回发的吗?如果是的话,我认为我已经理解得不够清楚了!这里似乎有什么问题?

//edit 2: entire code block

//编辑2:整个代码块

public partial class Action_k : System.Web.UI.Page
{
    ArrayList array;
    ArrayList tmpArrayList = new ArrayList();
    string itemIDs = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == false)
        {
            if (Session["username"] == null)
            {
                Session["anonuser"] = "anon";

                Label1.Text = "";
                userLabel.Text = "";
                ImageButton1.ImageUrl = "~/images/logink.gif";
                ImageButton1.PostBackUrl = "~/Login_k.aspx";
            }
            else
            {
                userLabel.Text = Session["username"].ToString();
                Label1.Text = "Your logged in as: ";
                ImageButton1.ImageUrl = "~/images/logoutk.gif";
            }

            if (Session["array"] == null)
            {
                array = new ArrayList();
                Session.Add("array", array);
            }

        }
        array = Session["array"] as ArrayList;
    }

    public void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
    {

        if (e.CommandName == "AddToCart")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            string items = GridView2.DataKeys[index].Value.ToString();
            array.Add(items);
            Response.Redirect("ShoppingCart_k.aspx?itemID=" + items);
        }

    }
}

Thanks,

谢谢,

采纳答案by pier

Well I found the answer for it, and it was simple. It seems to be a issue when ButtonType="Button".

好吧,我找到了答案,而且很简单。当 ButtonType="Button" 时,这似乎是一个问题。

The problem can be resolved by changing the ButtonType to "Link".

该问题可以通过将 ButtonType 更改为“Link”来解决。

Just incase if your interested here's the link that helped me out.

以防万一如果您感兴趣,这里是帮助我的链接。

http://forums.asp.net/p/1002747/1324414.aspx#1324414

回答by Joel Coehoorn

After the user clicks the button the page loads again and it goes to "rowcommand" AGAIN!

用户单击按钮后,页面再次加载并再次转到“rowcommand”!

I'm not seeing the problem. The user clicked button, so it handled the event. What did you expect it to do?

我没有看到问题。用户单击了按钮,因此它处理了事件。你期望它做什么?

Remember that every post back — and all events force a full post back — means running the entire page life cycle, including your page load event, in addition to any event that may have triggered the post back. Additionally, you are working with a new instance of your class for every post back.

请记住,每次回发(以及所有事件都强制进行完整回发)意味着运行整个页面生命周期,包括页面加载事件,以及可能触发回发的任何事件。此外,您正在为每次回发使用类的新实例。



So at the first button click you are expecting the page to be redirected and end up somewhere else?

那么在第一个按钮点击时,您希望页面被重定向并最终到达其他地方?

回答by kervin

It's doing what it's suppose to. First your page load event is handled, then your Row Command event is handled.

它正在做它应该做的事情。首先处理您的页面加载事件,然后处理您的 Row Command 事件。

What you're seeing is that two events are being handled, one after the other.

您看到的是正在处理两个事件,一个接一个。

Also checkout Callbacks versus Postbacks.

还要检查回调与回传。

Like Joel said, a Postback is going to invalidate and rebuild your page. A Callback uses Javascript/AJAX __doPostBack() and does not invalidate the entire page, just your callback component or container. A page load event is still called though, but you can test for the IsCallback property.

就像乔尔所说,回发将使您的页面无效并重建您的页面。回调使用 Javascript/AJAX __doPostBack() 并且不会使整个页面无效,只会使您的回调组件或容器无效。虽然仍会调用页面加载事件,但您可以测试 IsCallback 属性。

This link may help... http://msdn.microsoft.com/en-us/library/ms178141.aspx

此链接可能会有所帮助... http://msdn.microsoft.com/en-us/library/ms178141.aspx