星期三, 6月 04, 2008

解決updatepanel抓不到control

有時候我們在使用updatepanel時..會習慣用ui去選我們要trigger的物件..可是如果ui選不到ㄟ..那要怎麼辦..當然就是產生錯誤畫面..

A control with ID 'LinkButton2' could not be found for the trigger in UpdatePanel 'UpdatePanel1'.

解決辦法..就是用程式去加入..這樣就可以解決了..看下面例子..

這是一個.aspx..他是用masterpage..裡面有兩個contentplaceholder

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Panel ID="Panel1" runat="server" GroupingText="ContentPlaceHolder1" Width="200px">
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" runat="Server">
<br />
<asp:Panel ID="Panel2" runat="server" GroupingText="ContentPlaceHolder2" Width="200px">
<asp:LinkButton ID="LinkButton2" runat="server">LinkButton1</asp:LinkButton>
</asp:Panel>
</asp:Content>

.cs

public partial class test_updatePanel_Default : System.Web.UI.Page
{
void Page_Init(object sender, EventArgs e)
{
//動態加入trigger
AsyncPostBackTrigger t = new AsyncPostBackTrigger();
UpdatePanel1.Triggers.Add(t);
t.ControlID = LinkButton2.UniqueID;
}

protected void Page_Load(object sender, EventArgs e)
{
//抓出時間
Label1.Text = DateTime.Now.ToString();
}
}

ddf

這樣就可以抓到trigger的值了..不過注意一下..要給asyncpostbacktrigger的id是uniqueid喔..Smile..
解決收工..

參考:
http://forums.asp.net/t/1042891.aspx

沒有留言: