动态事件没有被称为asp.net

时间:2011-07-19 17:35:54

标签: c# asp.net event-handling

我想我已经阅读了互联网上的所有内容,而我仍然无法掌握动态事件。我正在制作一个在线调度程序,您可以在线查看您的工作时间表。我有它所以你可以发布你的班次让别人去接。你工作的每一天都有一个岗位转换按钮。当您单击它时,您的班次将被发布到数据库,页面将被重新加载,并且后换班按钮将变为拉动班次按钮。问题是如果你发布你的班次然后在下一页加载拉动你的班次拉动班次事件不会被调用。但是,如果您重新加载页面并单击它,它就可以正常工作。这是一些基本代码。

protected void Page_Load(object sender, EventArgs e)
{
    setView();  
}

private void assignEvents()
{
   for (int i = 0; i < 7; i++)
   {
        foreach (ScheduleItem item in scheduleControls[i])
        {
            if (item.shiftPosted)
            {
                item.button.Click += (sender, e) => btnPullShift_Click(sender,e, item);
            }
        }
    }
}


private void setView()
{
    //make a call to the database to get your schedule

    //loop through your schedule and display each day
    //if the shift is posted then the button should pull shift else it should post the shift.
    //(loop)
    if (tempResult.isPosted)
    {
        tempButton.Text = "Pull Shift";
        tempItem.shiftPosted = true;
        tempButton.ID = "Button" + scheduleID;
        tempButton.AutoPostBack = true;
    }
    else
    {
        tempButton.ID = "Button" + scheduleID;
        tempItem.shiftPosted = false;
        tempButton.Text = "Post Shift";
        //posting does a client side event that shows another panel where you enter details about posting there is another button you click to actually posts the shift.
     }

     tempItem.button = tempButton;
     scheduleControls[i].Add(tempItem);
    //(end loop)
    assignEvents();
}

void btnPullShift_Click(object sender, EventArgs e, ScheduleItem item)
{
    //edits the database to pull the shift
    setView();
}

void btnPostShift_Click(object sender, EventArgs e, ScheduleItem item)
{
    //edits the database to post the shift
    setView();
}

按钮每次都会获得相同的ID。所以当你加载页面时,它会创建按钮并设置事件,你点击post,重新加载页面,创建完全相同的按钮,调用btnPostShift_Click事件,更新数据库然后再调用setView()用户可以看到班次已发布以及更新事件,然后当他点击拉动班次时,页面将重新加载,并且按钮将使用相同的ID重新创建,但事件永远不会被调用。有任何想法吗?感谢。

0 个答案:

没有答案
相关问题