2012-09-26 122 views
0

該網頁包含的LinkBut​​ton及其方法:動態創建按鈕不起作用

protected void LinkButton_Click(Object sender, EventArgs e) 
    { 
     TableRow tr = new TableRow(); 
     TableCell tc = new TableCell(); 
     Button b = new Button(); 
     b.Text = "x"; 
     b.CommandArgument = "someargument"; 
     b.Click +=new EventHandler(this.b_Click); 
     tc.Controls.Add(b); 
     tc.Width = 30; 
     tr.Controls.Add(tc); 
     Table.Rows.Add(tr); 
    } 

和方法:

protected void b_Click(object sender, EventArgs e) 
    { 
     ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "UpdateCom",  "alert('ok');return false;", true); 
     string id = (sender as Button).CommandArgument; 
     // other operations... 

    } 

首先通過點擊我的LinkBut​​ton表裏面創建按鈕,然後當我點擊按鈕b_Click方法不起作用。我什至沒有看到JavaScript警報,而只是頁面更新發生。

什麼是問題?

需要注意的是,如果我在頁面加載方法裏面做LinkBut​​tonClick操作,一切都OK,創建按鈕,點擊該按鈕時b_click也適用。

+0

它回發時,當你點擊動態添加按鈕? – ecco88

+0

是發生回傳 – Nurlan

+1

我對OL的頁面生命週期並不是很滿意,但一眼就可以看到按鈕的點擊事件是在您返回響應的時候處理的。但是,當用戶單擊按鈕時,除非正確重新構建頁面(並在回發中設置按鈕單擊處理程序),否則click事件將不再有處理程序(處理該按鈕的前一個實例的頁面的前一個實例,與之前的響應/結束請求一起死亡)。希望這是有道理的? – Smudge202

回答

1

爲什麼你需要動態地構建上的按鈕,單擊表?你可以在頁面上加載並隱藏它嗎?當按鈕被點擊時顯示它 - 它可以顯示在客戶端或服務器端;

我的意思是我甚至會保持它在客戶端上 - 我不喜歡asp.net的東西實在是太多了回發。從字面上看是發佈形式及其在隱藏字段數據,如視圖狀態和重新描繪整個頁面。似乎ineffecient我 - 你的web服務器可以做其他事情一樣服務頁面請求,並推遲這個UX處理到客戶端瀏覽器。

<table id="testTable" style="display: none"> 
    <tr> 
    <td width="30px"> 
     <input id="b" name="b" type="button" value="someargument" text="Click Me!" onclick='alert(this.value);document.getElementById("testTable").style.display="none";' /> 
    </td> 
    </tr> 
</table> 
在你的鏈接按鈕

您可以添加這個屬性。

onclientclick='document.getElementById("testTable").style.display="inline";' /> // notice I used the single quote on the outside of the onclick. This would display the table.. 

,如果你仍然需要做的popup2按鈕一些服務器端操作 - 你可以添加RUNAT =「服務器」,改變的onclick到的OnClientClick ...

其已一段時間,因爲我用的ASP。淨網絡形式,所以讓我知道,如果這對你有用....