2009-06-15 123 views
0

我有一個包含10個動態生成的LinkBut​​ton元素的ASP.NET頁面。當用戶點擊其中一個LinkBut​​ton元素時,我想在模態對話框中顯示其文本。用戶可以通過在TextBox中輸入值來更改文本。我給這家代碼如下所示:ASP.NET - 使用ModalPopupExtender阻止頁面刷新

<asp:ScriptManager ID="theScriptManager" runat="server" /> 
<asp:UpdatePanel ID="myUpdatePanel" runat="server"> 
    <ContentTemplate> 
     <asp:Table ID="myTable" runat="server" OnInit="myTable_Init" CellPadding="10" CellSpacing="10" /> 
    </ContentTemplate> 
</asp:UpdatePanel> 

<asp:LinkButton ID="testLinkButton" runat="server" /> 
<cc1:ModalPopupExtender ID="myPopupExtender" runat="server" TargetControlID="testLinkButton" 
    OkControlID="okButton" PopupControlID="myPanel" /> 

<asp:Panel ID="myPanel" runat="server" Style="display: none;"> 
    <table border="1" cellpadding="0" cellspacing="0"><tr><td> 
     <table border="0" cellpadding="0" cellspacing="0" style="width: 300px;"> 
      <tr><td colspan="2" style="background-color: Blue; font-weight: bold; color: White;"> 
       &nbsp;Test 
      </td></tr> 
      <tr> 
       <td>You clicked <asp:TextBox ID="numTextBox" runat="server" MaxLength="3" />.</td> 
       <td align="right" style="padding-top: 5px; padding-bottom: 5px;"> 
        <asp:Button ID="okButton" runat="server" Text="OK" OnClick="okButton_Click" />&nbsp; 
       </td> 
      </tr> 
     </table> 
    </td></tr></table> 
</asp:Panel> 

我的代碼隱藏這個ASP.NET代碼如下所示:

private LinkButton selectedLinkButton = null; 

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

protected void myTable_Init(object sender, EventArgs e) 
{ 
     TableRow row = new TableRow(); 
     for (int i = 1; i < 11; i++) 
     { 
      LinkButton linkButton = new LinkButton(); 
      linkButton.Text = i.ToString(); 
      linkButton.Click += new EventHandler(linkButton_Click); 
      linkButton.CommandArgument = i.ToString(); 

      AddLinkButtonToRow(linkButton, row); 
     } 
     myTable.Rows.Add(row); 

} 

protected void linkButton_Click(object sender, EventArgs e) 
{ 
    selectedLinkButton = (LinkButton)(sender); 
    numTextBox.Text = selectedLinkButton.CommandArgument; 

    myPopupExtender.Show(); 
} 

protected void okButton_Click(object sender, EventArgs e) 
{ 
    if (selectedLinkButton != null) 
    { 
     selectedLinkButton.Text = numTextBox.Text.Trim(); 
    } 
} 

private void AddLinkButtonToRow(LinkButton linkButton, TableRow row) 
{ 
    TableCell cell = new TableCell(); 
    cell.Controls.Add(linkButton); 
    row.Cells.Add(cell); 
} 

我的問題是,我想減少回發的數量。爲了做到這一點,我決定使用ASP.NET AJAX工具箱。不幸的是,一旦用戶點擊對話框中的「確定」,我沒有更新LinkBut​​ton文本。另外,我似乎仍然收到回傳。我如何正確使用它?

謝謝

回答

2

默認情況下,在UpdatePanel只會觸發刷新時,其內部的對象有自己的發射各種活動。

您需要在UpdatePanel中移動ModalPopupExtender和代碼,或者在模態彈出窗口中爲您的Ok按鈕指定一個Update Trigger。

如果您還沒有得到正確的刷新,您可能需要添加myUpdatePanel.Update()命令到您的OK按鈕代碼,以便在爲新的LinkBut​​ton提供內容後刷新面板。

0

爲我的Close按鈕添加一個UpdatePanel觸發器有助於防止頁面發生完整的回發。所以爲你的OK按鈕添加一個觸發器應該做同樣的事情。