学习ASP。得到了这个问题,制作了9个图像按钮,并希望用相同的处理程序处理它们:
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 :(得分:0)
很可能这一行会根据您发布的代码抛出异常ImageButton boton = (ImageButton)sender;
...如果是,则sender
绝对不是ImageButton
而是其他一些控件类型
答案 1 :(得分:-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");
}
}
此代码可以防止您遇到强制转换异常,并告知您作为罪魁祸首的发件人类型。