Sunday, 15 August 2010

c# - Changing the color of a linkbutton on click? -



c# - Changing the color of a linkbutton on click? -

i have user command called courses displays available courses. in user command file, utilize repeater command display courses. course of study names rendered linkbuttons within itemtemplate. utilize user command in page called foo.aspx. using javascript function alter color of value within itemtemplate when clicked. color changes sec when click, goes original color. know doing wrong here?

my javascript function.

<script> function changecolor(e) { e.style.color = "red"; } </script> <asp:linkbutton id="linkbutton1" onclientclick="return changecolor(this);" runat="server">linkbutton</asp:linkbutton>

as said on comment, careful page postback.

to prevent linkbutton color reseted, think should save them within viewstate

i don't know whether it's best solution or not, @ to the lowest degree i've seek help :)

here aspx:

<asp:repeater id="myrepeater" onitemcommand="myrepeater_itemcommand" runat="server"> <itemtemplate> <asp:linkbutton id="mylinkbutton" runat="server" commandname="change_color" forecolor="<%# system.drawing.color.fromname(container.dataitem.tostring()) %>">your text</asp:linkbutton> </itemtemplate> </asp:repeater>

here code behind (c#):

first create viewstate (this key prevent info lost after postback)

list<string> listdata { set { viewstate["listdata"] = value; } { if (viewstate["listdata"] == null) homecoming new list<string>(); else homecoming (list<string>)viewstate["listdata"]; } }

on page load:

protected void page_load(object sender, eventargs e) { if (!ispostback) { listdata = new list<string>(); listdata.add("blue"); listdata.add("blue"); listdata.add("blue"); } myrepeater.datasource = listdata; myrepeater.databind(); }

when click linkbutton repeater trigger onitemcommand

protected void myrepeater_itemcommand(object sender, repeatercommandeventargs e) { if (e.item.itemtype == listitemtype.item || e.item.itemtype == listitemtype.alternatingitem) { if (e.commandname == "change_color") { linkbutton linkbutton = (linkbutton)e.item.findcontrol("mylinkbutton"); if (linkbutton.forecolor != system.drawing.color.red) { linkbutton.forecolor = system.drawing.color.fromname("red"); listdata[e.item.itemindex] = "red"; //this key! prevent color reset, save them within viewstate } else { linkbutton.forecolor = system.drawing.color.fromname("blue"); listdata[e.item.itemindex] = "blue"; } } } }

c# javascript asp.net

No comments:

Post a Comment