學習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>
抱歉缺乏信息的,我是一個新手
你有一些啓動點擊事件不是ImageButton – Seano666
或會話中不是字符串的東西。缺少堆棧跟蹤或[mcve]使得很難知道。 –
他正在他的'Session [「Turno」]中存儲一個整數值,似乎毫無必要地將它轉換爲一個字符串。但是,這不應該導致強制異常,因爲字符串和整數有一個隱式轉換? IIRC。 – Ratatoskr