当点击图像按钮时更改图像url

问题描述:

我有三个图像按钮,他们有默认图像url,我试图改变图像的网址,当用户点击任何图像按钮和默认检索时,用户点击其他图像按钮试图做到这一点,但我没有当点击图像按钮时更改图像url

<aspx> 
<div class="hom_but_sa hom_but_saR"> 
    <asp:ImageButton ID="BTNPromotion" ImageUrl="images/home-bu_npro.jpg" 
     runat="server" Width="134" Height="34" border="0" 
     OnClick="BTNPromotion_Click" /> 
</div> 
<div class="hom_but_a hom_but_sbR"> 
    <asp:ImageButton ID="BTNNewProduct" ImageUrl="images/home-bu_pro.jpg" 
     runat="server" Width="134" Height="34" border="0" 
     OnClick="BTNNewProduct_Click" /> 
</div> 
<div class="hom_but_a"> 
    <asp:ImageButton ID="BTNEvent" runat="server" ImageUrl="images/home-bu_news.jpg" 
     Width="134" Height="34" border="0" OnClick="BTNEvent_Click" /> 
</div> 
</div> 

<cs> 
protected void BTNEvent_Click(object sender, ImageClickEventArgs e) 
{   
    BTNEvent.ImageUrl = "images/home-bu_news.jpg";  
} 
protected void BTNNewProduct_Click(object sender, ImageClickEventArgs e) 
{ 
    BTNNewProduct.ImageUrl = "images/home-bu_pro_r.jpg"; 
} 
protected void BTNPromotion_Click(object sender, ImageClickEventArgs e) 
{  
    BTNPromotion.ImageUrl = "images/home-bu_npro_r.jpg"; 
} 
+0

所以问题是什么?我测试了你的代码,它正在工作......有什么缺失?请发布实际发生的事情以及您希望发生的事情。 – Kelsey 2010-09-18 15:08:56

按我的意见,我不知道你实际的问题是什么,但也许你需要某种类似这样的切换呢?

protected void BTNEvent_Click(object sender, ImageClickEventArgs e) 
{   
    BTNEvent.ImageUrl = "images/home-bu_news_r.jpg"; 
    BTNNewProduct.ImageUrl = "images/home-bu_pro.jpg"; 
    BTNPromotion.ImageUrl = "images/home-bu_npro.jpg"; 
} 
protected void BTNNewProduct_Click(object sender, ImageClickEventArgs e) 
{ 
    BTNEvent.ImageUrl = "images/home-bu_news.jpg"; 
    BTNNewProduct.ImageUrl = "images/home-bu_pro_r.jpg"; 
    BTNPromotion.ImageUrl = "images/home-bu_npro.jpg"; 

} 
protected void BTNPromotion_Click(object sender, ImageClickEventArgs e) 
{  
    BTNEvent.ImageUrl = "images/home-bu_news.jpg"; 
    BTNNewProduct.ImageUrl = "images/home-bu_pro.jpg"; 
    BTNPromotion.ImageUrl = "images/home-bu_npro_r.jpg"; 
} 

一个清洁的方法是只需要点击事件由一个事件连接来处理这一切的ImageButtonOnClick

OnClick="BTN_Click" 

然后实现Click这样的:

protected void BTN_Click(object sender, ImageClickEventArgs e) 
{ 
    ImageButton btn = (ImageButton)(sender); 
    BTNEvent.ImageUrl = (btn.ID.Equals("BTNEvent")) ? 
     "images/home-bu_news_r.jpg" : "images/home-bu_news.jpg"; 
    BTNNewProduct.ImageUrl = (btn.ID.Equals("BTNNewProduct")) ? 
     "images/home-bu_pro_r.jpg" : "images/home-bu_pro.jpg"; 
    BTNPromotion.ImageUrl = (btn.ID.Equals("BTNPromotion")) ? 
     "images/home-bu_npro_r.jpg" : "images/home-bu_npro.jpg"; 
} 

尝试在ImageButton的

这可以通过使用Javascript可以轻松实现设置EnableViewState="false"。拨打以下所有三个按钮的onclick事件功能,请检查下面的代码,

function imageurl(id) 
{ 
//assuming image button ids as test1,test2,test3 

switch(id) 
{ 
    case 'test1': 
     document.getElementById('test1').href = 'test1newurl'; 
     document.getElementById('test2').href = 'test2oldurl'; 
     document.getElementById('test3').href = 'test3oldurl'; 
    break; 

    case 'test1': 
     document.getElementById('test1').href = 'test1oldurl'; 
     document.getElementById('test2').href = 'test2newurl'; 
     document.getElementById('test3').href = 'test3oldurl'; 
    break; 

    case 'test1': 
     document.getElementById('test1').href = 'test1oldurl'; 
     document.getElementById('test2').href = 'test2oldurl'; 
     document.getElementById('test3').href = 'test3newurl'; 
    break; 
} 

} 
+1

这将会在回发后丢失。 – Aristos 2010-09-18 15:27:38

+0

@Aristos:多数民众赞成在多数民众赞成在真实但只给了他如何可以在JavaScript中完成的替代方案。同样的逻辑可以应用在服务器端。 – Sandy 2010-09-18 15:50:51

+0

好的,但我认为他需要点击服务器端。 – Aristos 2010-09-18 15:52:27