How can I get a listview hiddenfield and use it in codebehind oncheckedchanged function
<a开发者_StackOverflow中文版sp:ListView ID="TasksList" runat="server" DataKeyNames="AnnualProgramTasksId"
DataSourceID="TasksListSqlDataSource">
...
<LayoutTemplate>...</LayoutTemplate>
...
<ItemTemplate>
<tr>
<td>
<asp:HiddenField ID="AnnualProgramTasksId" runat="server" Value='<%#Bind("AnnualProgramTasksId")%>'/>
<asp:Checkbox ID="CompletedCheckbox" runat="server" AutoPostBack="True" OnCheckedChanged="CompletedCheckboxChange" Checked='<%#IIf(Eval("TaskCompleted"), "True", "False")%>' />
</td>
<td><%#Eval("TaskDesc")%></td>
<td></td>
</tr>
</ItemTemplate>
...
<asp:sqldatasource ID="TasklistSqlDataSource...
SelectCommand="Select AnnualProgramTasksId, TaskDesc, TaskCompleted FROM AnnualPreogramTasks"
</asp:sqldatasource>
...
Code Behind:
Protected Sub CompletedCheckboxChange( ByVal sender As Object, ByVal e As EventArgs )
Dim Completed As CheckBox = TryCast( sender, CheckBox )
response.write( Completed.Checked ) ' This works. I'll use this to determine what to enter in the database for the 'Checked' column
' **How can i get the value of AnnualProgramTasksId?**
' (I'll use this to construct a sql statement to update the appropriate database row.
End Sub
Dim AnnualProgramTasksId as HiddenField = TryCast(Completed.Parent.FindControl("AnnualProgramTasksId"), HiddenField)
Maybe Something like this would work, havent tested it though
Dim AnnualProgramTasksId As New List(Of Integer)
For Each task As ListViewItem In Me.TasksList.Items
Dim CompletedCheckboxs As CheckBox = DirectCast(task.FindControl("CompletedCheckbox"), CheckBox)
If CompletedCheckboxs IsNot Nothing Then
If CompletedCheckboxs.Checked Then
Dim HiddenProductID As HiddenField = DirectCast(task .FindControl("AnnualProgramTasksId"), HiddenField)
Dim ID As String = CInt(HiddenProductID.Value.ToString)
End If
Next
精彩评论