2008-11-14 67 views
1

我有Datalist是在一個updatepanel內,它在面板中的modalpopupextender;關於Asp.Net DataList命令和EventValidation

我可以列出我想要的項目。我還爲Delete和AddBelow添加了2個按鈕。下面是標記:

<asp:DataList ID="SpeedDialsDL" runat="server"> 
       <ItemTemplate> 
        <table id="speedDialValueEditorTable" width="100%"> 
         <tr> 
          <td width="275px"> 
           <asp:Label ID="ValueLabel" runat="server" Text="Value"></asp:Label> 
          </td> 
          <td> 
           <asp:TextBox ID="ValueTextBox" runat="server" Text='<%# Eval("Value") %>' Width="340px" 
            Enabled="false"></asp:TextBox> 
          </td> 
         </tr>      
         <tr> 
          <td colspan="2"> 
           <asp:Button ID="Delete" runat="server" Text="Delete" CommandName="Delete" 
           CausesValidation="false" />&nbsp; 
           <asp:Button ID="AddNewButton" runat="server" Text="AddBelow" CommandName="AddBelow" 
            CausesValidation="false" /> 
          </td> 
         </tr> 
        </table> 
       </ItemTemplate> 
      </asp:DataList> 

並註冊evenst像以下內容:(我都用了ItemCommand和DeleteCommand,看看哪些工作:)

protected void Page_Load(object sender, EventArgs e) 
{ 
    SpeedDialsDL.ItemCommand += SpeedDialsDL_ItemCommand; 
    SpeedDialsDL.DeleteCommand += SpeedDialsDL_ItemCommand;    
}   

void SpeedDialsDL_ItemCommand(object source, DataListCommandEventArgs e) 
{ 
    switch (e.CommandName) 
    { 
     case "Delete": 
      this.DeleteFromList((string)e.CommandArgument); 
      break; 
     case "AddBelow": 
      break; 
    } 
} 

但是當我點擊刪除或AddBelow按鈕我獲得以下錯誤:

Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation. 

我頁的禁用eventvalidation但該事件不能被抓到......

我在這裏錯過了什麼?

回答

0

控件在呈現期間註冊它的事件,然後在回發或回調處理期間驗證事件。您正在Page_Load期間手動註冊事件。

嘗試重構代碼,以便在.aspx頁面中指定事件處理程序,將項目的Id綁定到按鈕的CommandArgument,然後在處理程序中獲取綁定項目Id。我還會爲不同的事件分別設置事件處理程序,它會更清潔一些。喜歡的東西:

<asp:DataList ID="SpeedDialsDL" runat="server">     
<ItemTemplate>      
    <table id="speedDialValueEditorTable" width="100%">       
     <tr>        
      <td width="275px">         
       <asp:Label ID="ValueLabel" runat="server" Text="Value"></asp:Label>        
      </td>        
      <td>         
       <asp:TextBox ID="ValueTextBox" runat="server" Text='<%# Eval("Value") %>' Width="340px" Enabled="false"></asp:TextBox>        
      </td>      
     </tr>            
     <tr>        
      <td colspan="2">         
       <asp:Button ID="Delete" runat="server" Text="Delete" OnClick="Delete" CausesValidation="false" CommandArgument='<%# Eval("Id") %>' /> 
       <asp:Button ID="AddNewButton" runat="server" Text="AddBelow" OnClick="AddBelow" CausesValidation="false" CommandArgument='<%# Eval("Id") %>' />        
      </td>      
     </tr>     
    </table>     
</ItemTemplate>    

protected void Delete(object sender, EventArgs e) 
{ 
    Button b = (Button)sender as Button; 
    string boundItemId = b.CommandArgument; 
    //handle delete 
} 

protected void AddBelow(object sender, EventArgs e) 
{ 
    Button b = (Button)sender as Button; 
    string boundItemId = b.CommandArgument; 
    //handle add below 
}