我在VS中的ASP.NET中有这个 我在转发器中有图像按钮
<asp:Repeater ID="RepeaterImages" runat="server">
<ItemTemplate>
<asp:ImageButton ID="Imagebutton2" runat="server" CssClass="choicebutton" ImageUrl='<%# DataBinder.Eval(Container.DataItem, "answer" )%>' CommandArgument='<%# DataBinder.Eval(Container.DataItem, "question" )%>' CommandName='<%# DataBinder.Eval(Container.DataItem, "answer" )%>' OnCommand="checkAnswer" />
</ItemTemplate>
</asp:Repeater>
我想更改图像按钮的边框颜色为
public void checkAnswer(object sender, CommandEventArgs e)
{
string trueanswer = e.CommandArgument.ToString();
string urlanswer = e.CommandName.ToString();
if (urlanswer == q_image)
{
}
else
{
// Imagebutoon2 doesnt exist in current context why?
Imagebutton2.BorderColor = Color.Red;
}
}
答案 0 :(得分:0)
您可以使用ItemDataBound
执行此任务:
protected void RepeaterImages_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
RepeaterItem item = e.Item;
if ((item.ItemType == ListItemType.Item) || (item.ItemType == ListItemType.AlternatingItem))
{
Imagebutton Imagebutton2= (Imagebutton)item.FindControl("Imagebutton2");
string trueanswer = Imagebutton.CommandArgument.ToString();
string urlanswer = Imagebutton.CommandName.ToString();
if (urlanswer == q_image)
{
}
else
{
// Imagebutoon2 doesnt exist in current context why?
Imagebutton2.BorderColor = Color.Red;
}
}
}
不要忘记将其添加到转发器服务器标记:OnItemDataBound="RepeaterImages_ItemDataBound"
<强>更新强>
如果你想在点击图像按钮时发生这种情况,那就更简单了。您可以使用sender
访问点击的图片按钮:
public void checkAnswer(object sender, CommandEventArgs e)
{
ImageButton btn = (ImageButton)sender;
string trueanswer = e.CommandArgument.ToString();
string urlanswer = e.CommandName.ToString();
if (urlanswer == q_image)
{
}
else
{
// Imagebutoon2 doesnt exist in current context why?
btn.BorderColor = Color.Red;
}
}