add an initial line-through text-decoration on a label in a asp listview
My goal is to display a list of tasks with checkboxes. Checking the checkbox for a task will update the display of the task so a line-through text decoration will appear. Everythign is working great, except I can't figure out how to initially show the list of tasks with a line-through if they are completed, or normal if the task is not completed. Here's code excerpts:
<asp:Listview .../>
...
<ItemTemplate>
<asp:HiddenField ID="TaskCompleted" runat="server" Value='<%#Bind("TaskCompleted")%>'/
<asp:Checkbox ID="CompletedCheckbox" runat="server" AutoPostBack="True" OnCheckedChanged="CompletedCheckboxChange" Checked='<%#IIf(Eval("TaskCompleted"), "True", "False")%>' />
<asp:Label id="TaskLabel" text='<%#Eval("TaskDesc")%>' runat="server" />
</ItemTemplate>
...
</asp:ListView>
Then the code behind (minus the database stuff which works fine):
Protected Sub CompletedCheckboxChange( ByVal sender As Object, ByVal e As EventArgs )
Dim Completed As CheckBox = TryCast( sender, CheckBox )
Dim AnnualProgramTasksId as HiddenField = TryCast(Completed.Parent.FindControl("AnnualProgramTasksId"), HiddenField)
Dim TaskLabel As Label = TryCast(Completed.Parent.FindControl("TaskLabel"), Label)
If Completed.Checked Then
'update task displayed, give it a line-through
TaskLabel.Style("text-decoration") = "line-through"
Else
'update task displayed, give it a line-through
TaskLabel.Style("text-decoration") = "none"
End If
End Sub
So this works great, when I click on a checkbox the lable gets a line-through or non开发者_Go百科e based on the checkbox. Only problem is when I initially load the page, I can't figure out how to update the style of TaskLabel to show a line-through or not. I've tried some various routes, but nothing is panning out. Any ideas?
This is how I have always done something like whay you are trying to. Try
<asp:Label id="TaskLabel" text='<%#Eval("TaskDesc")%>' runat="server"
OnDataBinding="TaskLabel_DataBinding" />
and
Protected Sub TaskLabel_DataBinding( ByVal sender As Object, ByVal e As EventArgs )
Dim Completed As CheckBox = TryCast(DirectCast( sender, Control).Parent.FindControl("CompletedCheckbox"), CheckBox)
CompletedCheckboxChange(Completed, EventArgs.Empty)
End Sub
精彩评论