简体   繁体   English

转发器按钮发回到page_load而不是我的方法?

[英]Repeater button posting back to page_load instead of my method?

In an aspx page I have the following repeater: 在aspx页面中,我有以下转发器:

<asp:Repeater runat="server" ID="r" OnItemCommand="paper_ItemCommand">
    <ItemTemplate>
        <div style="padding-bottom:20px">
            <asp:HiddenField runat="server" ID="questionID" Value='<%# Eval("ID") %>'/>
            <asp:Label runat="server" ID="questionNumber" Text='<%# Eval("Number") %>'/><br />
            <asp:Label runat="server" ID="question1" Text='<%# Eval("Question1") %>' /><br />
            [ <asp:Label runat="server" ID="questionMark" Text='<%# Eval("Mark") %>'/> ]<br />
            <asp:Button ID="View_Conversations" runat="server" Text="View Conversations" CommandName="ViewConversationsCommand" />
        </div>
    </ItemTemplate>
    <SeparatorTemplate>
        <hr />
    </SeparatorTemplate>
</asp:Repeater>

And in the code behind file I have the following methods: 在文件后面的代码中,我有以下方法:

    protected void Page_Load(object sender, EventArgs e)
    {
        var paperID = Session["paperID"].ToString();
        QuestionPaper = repository.GetPaper(Int32.Parse(paperID));
        r.DataSource = QuestionPaper.Questions;
        r.DataBind();
    }

    protected void paper_ItemCommand(object sender, RepeaterCommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "ViewConversationsCommand":
                var hidden = e.Item.FindControl("questionID") as HiddenField;
                var myquestionID = hidden.Value;
                break;
        }
    }

The problem I have is when I click the View Conversation button on the repeater, the code hits the page_load method instead of my paper_ItemCommand method. 我遇到的问题是,当我单击转发器上的“查看对话”按钮时,代码击中了page_load方法而不是我的paper_ItemCommand方法。 What am I getting wrong here? 我这是怎么了?

The page load occurs because each time when you hits the button the postback occur to the page it self and its the whole page lifecycle get run. 之所以会发生页面加载,是因为每次您按下按钮时,回发都会发生在页面本身上,并且整个页面生命周期都会运行。 If you want to prevent not to execute the page load stuff on button click you can try as follows: 如果要防止不执行按钮上的页面加载操作,可以尝试如下操作:

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack){
      // Your existing stuff
    }
}

Good Luck !! 祝好运 !!

Every time a page is loaded, including when you post back to the page (say, by clicking a Button ), the Page_Load event handler will be hit. 每次加载页面时,包括当您发布回页面时(例如,通过单击Button ),都会命中Page_Load事件处理程序。 The key is to check the Page object's IsPostBack property: 关键是检查Page对象的IsPostBack属性:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack) 
    {
        // do things that only should be done on the first page load
        var paperID = Session["paperID"].ToString();
        QuestionPaper = repository.GetPaper(Int32.Parse(paperID));
        r.DataSource = QuestionPaper.Questions;
        r.DataBind();
    }
}

page_load always run first http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbconwebformspageprocessingstages.asp test if not postback set your initialization page_load始终首先运行http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbconwebformspageprocessingstages.asp测试如果不回发,请设置您的初始化

if(!Page.IsPostback)
{
 var paperID = Session["paperID"].ToString(); 
        QuestionPaper = repository.GetPaper(Int32.Parse(paperID)); 
        r.DataSource = QuestionPaper.Questions; 
        r.DataBind(); 
}

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

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