2017-10-16 37 views
0

學習ASP。得到了這個問題,提出9個imagebuttons,想用相同的處理程序來處理它們:在事件點擊處理器上拋出異常

protected void jugar_Click(object sender, ImageClickEventArgs e) 
    { 
     String turno = (string)Session["Turno"]; 
     ImageButton boton = (ImageButton)sender; 
     if (turno == "1") 
     { 
      boton.ImageUrl = "C:\Users\Epyros\documents\visual studio 2015\Projects\EnTA TE TI\EnTA TE TI\Imagenes\Tic-tac-toe-cross.png"; 
      Session["Turno"] = 2; 
     } 
     else if (turno == "2") 
     { 
      boton.ImageUrl = "C:\Users\Epyros\documents\visual studio 2015\Projects\EnTA TE TI\EnTA TE TI\Imagenes\Tic-tac-toe-nought-e1461667111145.png"; 
      Session["Turno"] = 1; 
     } 
    } 

它拋出一個轉換異常,對什麼是錯

任何想法?

這些都是指的這個處理程序的按鈕:

<asp:Table ID="Table2" runat="server" HorizontalAlign="Center"> 
     <asp:TableRow runat="server"> 
      <asp:TableCell runat="server" Height="100" Width="100" HorizontalAlign="Center" VerticalAlign="Middle" Style="border-bottom: solid 2px red; border-right: solid 2px red"> 
       <asp:ImageButton ID="btn00" runat="server" class="imagen" ImageUrl="C:\Users\Epyros\documents\visual studio 2015\Projects\EnTA TE TI\EnTA TE TI\Imagenes\White_square.jpg" OnClick="jugar_Click"></asp:ImageButton> 
      </asp:TableCell> 
      <asp:TableCell runat="server" Height="100" Width="100" HorizontalAlign="Center" VerticalAlign="Middle" style="border-left:solid 2px red;border-bottom:solid 2px red;border-right:solid 2px red"> 
       <asp:ImageButton ID="btn01" runat="server" class="imagen" ImageUrl="C:\Users\Epyros\documents\visual studio 2015\Projects\EnTA TE TI\EnTA TE TI\Imagenes\White_square.jpg" OnClick="jugar_Click"></asp:ImageButton> 
      </asp:TableCell> 
      <asp:TableCell runat="server" Height="100" Width="100" HorizontalAlign="Center" VerticalAlign="Middle" style="border-left:solid 2px red;border-bottom:solid 2px red"> 
       <asp:ImageButton ID="btn02" runat="server" class="imagen" ImageUrl="C:\Users\Epyros\documents\visual studio 2015\Projects\EnTA TE TI\EnTA TE TI\Imagenes\White_square.jpg" OnClick="jugar_Click"></asp:ImageButton> 
      </asp:TableCell> 
     </asp:TableRow> 
     <asp:TableRow runat="server"> 
      <asp:TableCell runat="server" Height="100" Width="100" HorizontalAlign="Center" VerticalAlign="Middle" style="border-top:solid 2px red;border-bottom:solid 2px red;border-right:solid 2px red"> 
       <asp:ImageButton ID="btn10" runat="server" class="imagen" ImageUrl="C:\Users\Epyros\documents\visual studio 2015\Projects\EnTA TE TI\EnTA TE TI\Imagenes\White_square.jpg" OnClick="jugar_Click"></asp:ImageButton> 
      </asp:TableCell> 
      <asp:TableCell runat="server" Height="100" Width="100" HorizontalAlign="Center" VerticalAlign="Middle" style="border-top:solid 2px red;border-bottom:solid 2px red;border-left:solid 2px red;border-right:solid 2px red"> 
       <asp:ImageButton ID="btn11" runat="server" class="imagen" ImageUrl="C:\Users\Epyros\documents\visual studio 2015\Projects\EnTA TE TI\EnTA TE TI\Imagenes\White_square.jpg" OnClick="jugar_Click"></asp:ImageButton> 
      </asp:TableCell> 
      <asp:TableCell runat="server" Height="100" Width="100" HorizontalAlign="Center" VerticalAlign="Middle" style="border-top:solid 2px red;border-bottom:solid 2px red;border-left:solid 2px red"> 
       <asp:ImageButton ID="btn12" runat="server" class="imagen" ImageUrl="C:\Users\Epyros\documents\visual studio 2015\Projects\EnTA TE TI\EnTA TE TI\Imagenes\White_square.jpg" OnClick="jugar_Click"></asp:ImageButton> 
      </asp:TableCell> 
     </asp:TableRow> 
     <asp:TableRow runat="server"> 
      <asp:TableCell runat="server" Height="100" Width="100" HorizontalAlign="Center" VerticalAlign="Middle" style="border-top:solid 2px red;border-right:solid 2px red"> 
       <asp:ImageButton ID="btn20" runat="server" class="imagen" ImageUrl="C:\Users\Epyros\documents\visual studio 2015\Projects\EnTA TE TI\EnTA TE TI\Imagenes\White_square.jpg" OnClick="jugar_Click"></asp:ImageButton> 
      </asp:TableCell> 
      <asp:TableCell runat="server" Height="100" Width="100" HorizontalAlign="Center" VerticalAlign="Middle" style="border-top:solid 2px red;border-left:solid 2px red;border-right:solid 2px red"> 
       <asp:ImageButton ID="btn21" runat="server" class="imagen" ImageUrl="C:\Users\Epyros\documents\visual studio 2015\Projects\EnTA TE TI\EnTA TE TI\Imagenes\White_square.jpg" OnClick="jugar_Click"></asp:ImageButton> 
      </asp:TableCell> 
      <asp:TableCell runat="server" Height="100" Width="100" HorizontalAlign="Center" VerticalAlign="Middle" style="border-top:solid 2px red;border-left:solid 2px red"> 
       <asp:ImageButton ID="btn22" runat="server" class="imagen" ImageUrl="C:\Users\Epyros\documents\visual studio 2015\Projects\EnTA TE TI\EnTA TE TI\Imagenes\White_square.jpg" OnClick="jugar_Click"></asp:ImageButton> 
      </asp:TableCell> 
     </asp:TableRow> 
    </asp:Table> 

抱歉缺乏信息的,我是一個新手

+0

你有一些啓動點擊事件不是ImageButton – Seano666

+3

或會話中不是字符串的東西。缺少堆棧跟蹤或[mcve]使得很難知道。 –

+0

他正在他的'Session [「Turno」]中存儲一個整數值,似乎毫無必要地將它轉換爲一個字符串。但是,這不應該導致強制異常,因爲字符串和整數有一個隱式轉換? IIRC。 – Ratatoskr

回答

0

最大的可能是此行引發異常ImageButton boton = (ImageButton)sender;按您發佈的代碼......如果是那麼sender絕對不是一個ImageButton而其他一些控件類型

-1
protected void jugar_Click(object sender, ImageClickEventArgs e) { 

if (sender is ImageButton) { 

    var turNo = Session["Turno"]; 

    var buttonSender = (ImageButton)sender; 

    switch (turNo) { 
    case 1: 
    buttonSender.ImageUrl = "C:\Users\Epyros\documents\visual studio 2015\Projects\EnTA TE TI\EnTA TE TI\Imagenes\Tic-tac-toe-cross.png"; 
    Session["Turno"] = 2; 
    break; 
    case 2: 
    buttonSender.ImageUrl = "C:\Users\Epyros\documents\visual studio 2015\Projects\EnTA TE TI\EnTA TE TI\Imagenes\Tic-tac-toe-nought-e1461667111145.png"; 
    Session["Turno"] = 1; 
    break; 
    default: 
    // Logic for turNo != 1 or 2 
    break; 
    } 
} 
else 
{ 
    // sender is not the expected type (ImageButton) 
    Trace.WriteLine(string.Format("Sender is an unexpected type. Expected: (ImageButton), got ({0}).", sender.GetType().Name),"WARN"); 
} 
} 

此代碼應預讓你從跑步轉化爲投射異常,並告知你是罪魁禍首的發送者的類型。

+0

如果'sender'不是ImageButton?使用此代碼轉換異常將變爲空引用異常。 – mxmissile

+0

相反,OP可能會得到一個'NullReferenceException'而沒有* actual *類型的信息,並且在世界不是預期的時候延遲執行時間。請參閱https://codeblog.jonskeet.uk/2013/09/19/casting-vs-quot-as-quot-embracing-exceptions/ –

+0

如果發件人不是ImageButton,那麼if中的條件將爲false,並且不執行內部的代碼塊。 – Ratatoskr

相關問題