Wednesday, March 28, 2012

LinkButton in a DataList is shown incorrectly

I use a DataList to show LinkButton′s (CommandName="Select") of which the names are read from a database. That part works nice.

ItemTemplate : LinkButtons on a white background with brown text
SelectedItemTemplate : LinkButtons on a brown backgound with white text

The strange behaviour when I click a LinkButton:

    the click event routine works okaywhen I click the first time a button, its is shown incorrectlySadthe button text is shown still in brown on a white backgrounda brown border is shown arround the buttonwhen I click the same button again it is shown correctlySmilethe button text is shown in white on a brown backgrounda brown border is shown arround the button

Does somebody has an idea what is happening here?

<asp:ScriptManagerID="ScriptManager1"runat="server"></asp:ScriptManager>
<asp:UpdatePanelID="UpdatePanel1"runat="server">
<Triggers>
<asp:AsyncPostBackTriggerControlID="DataListGrupos"EventName="SelectedIndexChanged"/>
</Triggers>
<ContentTemplate>
<asp:DataListID="DataListGrupos"runat="server"DataKeyField="IdGrupo"DataSourceID="SqlDataSourceGrupos" RepeatColumns="8"CellPadding="5"RepeatLayout="Table"SelectedIndex="0"ShowFooter="False"ShowHeader="False">
<ItemTemplate>
<asp:LinkButtonID="LinkButtonGrupo"runat="server"BackColor="White"CommandName="Select" Font-Bold="True"Font-Names="verdana"Font-Size="Larger"ForeColor="#C04000"Font-Underline="True"Text='<%# Eval("Grupo") %>'></asp:LinkButton>
</ItemTemplate>
<SelectedItemStyleBackColor="#C04000"/>
<SelectedItemTemplate>
<asp:LinkButtonID="LinkButtonGrupo"runat="server"BackColor="#C04000"CommandName="Select"Font-Bold="True"Font-Names="verdana"Font-Size="Larger"ForeColor="White"Text='<%# Eval("Grupo") %>'></asp:LinkButton>
</SelectedItemTemplate>
<ItemStyleBackColor="White"/>
</asp:DataList>
</ContentTemplate>
</asp:UpdatePanel>

Idea For the time being I have bypassed the problem to have at least the result I wanted:

ProtectedSub DataListGrupos_SelectedIndexChanged(ByVal senderAsObject,ByVal eAs System.EventArgs)Handles DataListGrupos.SelectedIndexChanged

Dim MyButtonAs LinkButton

MyButton =CType(Me.DataListGrupos.Items(Session("PreviousGrupoIndex")).FindControl("LinkButtonGrupo"), LinkButton)
MyButton.BackColor = Drawing.Color.White
MyButton.ForeColor = Drawing.Color.FromArgb(&HC04000)
MyButton.Font.Underline =True

MyButton =CType(Me.DataListGrupos.SelectedItem.FindControl("LinkButtonGrupo"), LinkButton)
MyButton.BackColor = Drawing.Color.FromArgb(&HC04000)
MyButton.ForeColor = Drawing.Color.White
MyButton.Font.Underline =False

Session("PreviousGrupoIndex") =Me.DataListGrupos.SelectedIndex
...

EndSub

No comments:

Post a Comment