触发问题,更新面板不起作用?

时间:2010-03-04 21:38:44

标签: asp.net triggers updatepanel

我创建了动态LinkBut​​ton,并将LinkBut​​ton的Click Trigger添加到UpdatePanel。

现在,当我第一次点击任何一个链接按钮触发器运行良好并显示我的选择whitout POSTBACK。之后,我点击其他LinkBut​​ton所有页面加载和POSTBACK运行,因此触发器不起作用! 问题是什么 ?请帮助!

protected void Page_Load(object sender, EventArgs e)
{

        ShowAllCar();

}

public void ShowAllCar()
{

    dsAraclarTableAdapters.tblAraclarTableAdapter _t=new dsAraclarTableAdapters.tblAraclarTableAdapter();
    dsAraclar.tblAraclarDataTable _m =_t.GetData();
    int i=0;
    UpdatePanel1.Triggers.Clear();
    pnlAraclar.Controls.Clear();
    foreach (DataRow _row in _m.Rows)
    { 
        LinkButton _linkbutton =new LinkButton();
        i++;
        _linkbutton.ID ="Option" + i.ToString();
        _linkbutton.Text = "<img src='" + _row["Resim"].ToString() + "' border='0'/> <b>" + _row["Marka"].ToString() + " " + _row["Model"].ToString() + "</b><br/>" + _row["Ozellikler"].ToString() + " : " + _row["KisFiyat"].ToString() + ":" + _row["YazFiyat"].ToString();
        _linkbutton.CssClass="ContextMenuItem";
        _linkbutton.PostBackUrl = "";
        _linkbutton.Click +=new EventHandler(OnCarSelect);
        pnlAraclar.Controls.Add(_linkbutton);

        AsyncPostBackTrigger _trigger = new AsyncPostBackTrigger();
        _trigger.ControlID = _linkbutton.ID;
        _trigger.EventName = "Click";
        UpdatePanel1.Triggers.Add(_trigger);
    }
} 

protected void OnCarSelect(object sender, EventArgs e)
{
    lblSelection.Text = "You selected <b>" + ((LinkButton)sender).Text + "</b>.";
}

1 个答案:

答案 0 :(得分:2)

您需要在页面生命周期的早期将控件添加回控件树:

  

PreInit
使用此活动   以下内容:
      ...
      *创建或重新创建动态控件       ...

ASP.NET Page Life Cycle Overview