2

I have a user control which has a linkbutton. On the link button click, I am redirecting to the default page. The default page uses this user control. I am trying to set the color of the linkbutton to purple, if visited. I think this is not working because of the page post back which is happening. Below is my code.

void LinkButton_Click(Object sender, EventArgs e) 
  {
     Response.redirect("~/Default.aspx")
  }

Css

 .d:hover{
        color:green;
    }

    .d:visited{
        color:purple;
    }
</style>

I don't think it is the css problem because when I hover, the color changes to green. The visited link color does not change. Is it because the page is being rendered again on the link button click? How would I fix this?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
trant trum
  • 219
  • 5
  • 14
  • Im new to web dev also. But i think your right, the page reloaded again so the css for visited link is not working. Hmm, if you have studied cookies and session, maybe you can haved saved cookies for a boolean value if the link is clicked and then manipulate that value for your css. Or maybe there is a simple css solution than what i have tried to suggest.haha :P – bumbumpaw Jun 25 '14 at 03:41
  • @Niang Thanks for the reply. I think there is an easy css way to do it. That's why I'm here hahaha – trant trum Jun 25 '14 at 03:46
  • 1
    maybe this link might help you, [see this](http://stackoverflow.com/questions/17294286/highlighting-a-li-element-on-click-and-keeping-it-highlighted-even-after-page). This is a different example,but you can try to work this out. – bumbumpaw Jun 25 '14 at 03:54
  • @Niang Thanks for the link. I guess, I have to do this with server side with session variables as you mentioned. – trant trum Jun 25 '14 at 03:56

2 Answers2

2

Ok this is a very interesting question and I should say that I've found a trick to do this, which is quite possible to use it across the whole application, so here is my solution:

You can use the same OnPreRender="LinkButtons_PreRender" on your link buttons only once and then you will have something like this on your aspx:

 <asp:LinkButton ID="LinkButton1" runat="server" 
        OnClick="LinkButton1_Click" OnPreRender="LinkButtons_PreRender">LinkButton</asp:LinkButton>

 <asp:LinkButton ID="LinkButton2" runat="server" 
        OnClick="LinkButton2_Click" OnPreRender="LinkButtons_PreRender">LinkButton</asp:LinkButton>

and in your code behind you can fix this like the code below:

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Session[((LinkButton)sender).ID + "visited"] = System.Drawing.Color.Purple;
        // your code here
    }

    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        Session[((LinkButton)sender).ID + "visited"] = System.Drawing.Color.Purple;
        // your code here
    }

    protected void LinkButtons_PreRender(object sender, EventArgs e)
    {
        LinkButton lnkbtn = (LinkButton)sender;
        lnkbtn.ForeColor = (System.Drawing.Color)(Session[lnkbtn.ID + "visited"] ?? System.Drawing.Color.Blue);
    }

And that's it! here is the result:

enter image description here

(Performance hint: always kill the sessions when you don't need them)

Ali
  • 2,574
  • 1
  • 17
  • 24
0

You need to use hyperlink for that purpose, because the link button uses JavaScript to post back or navigate to another page .

it doesn't use the HREF attribute to redirect. so in that case css visited will not work as you expect.

for that purpose you need to use hyperlink or native html anchor(a) tag.

Maulik Anand
  • 1,429
  • 3
  • 15
  • 19