Protected Sub DataList2_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs)
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
Dim DataList2 = DirectCast(sender, DataList)
Dim ParentItem = DirectCast(DataList2.NamingContainer, DataListItem)
Dim HiddenField2 = DirectCast(ParentItem.FindControl("HiddenField2"), HiddenField)
Dim QID As Integer = Int32.Parse(HiddenField2.Value)
If QID = 33 Then
For Each li As DataListItem In DataList2.Items
'retrieves the label control in the current DataListItem
Dim lbl As Label = CType(e.Item.FindControl("Label3"), Label)
If lbl IsNot Nothing Then
If lbl.Text = "Self Directed" Or lbl.Text = "Systems" Then
lbl.Visible = False
End If
End If
Next
End If
End If
End Sub
This code will hide "Systems" but not "Self Directed". I have checked capitalization and the way it is worded and it is written EXACTLY the same way it is in the database. I also have code similar to this on another page that is hiding both "Self Directed" and "Systems". Pretty much the only difference here is that one takes place in a DataList, the other (working code) takes place in a DetailsView.(which is shown below)
Protected Sub dvSurveyQuestions_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles dvSurveyQuestions.DataBound
'The event handler checks the row count of the DetailsView control. If it is zero then the mode of the DetailsView is changed to Insert using ChangeMode() method.
If dvSurveyQuestions.Rows.Count = 0 Then
dvSurveyQuestions.ChangeMode(DetailsViewMode.Insert)
End If
If dvSurveyQuestions.CurrentMode = DetailsViewMode.ReadOnly Then
'Finds the hidden field of Question ID for data binding purposes
Dim QuestionID As Integer = CInt(CType(dvSurveyQuestions.FindControl("hiddenQuestionID"), HiddenField).Value)
If QuestionID = 33 Then
Dim blText As BulletedList = dvSurveyQuestions.FindControl("blText")
For intCursor = (blText.Items.Count - 1) To 0 Step -1
If blText.Items(intCursor).Text = "Self Directed" Or blText.Items(intCursor).Text = "Systems" Then
blText.Items.RemoveAt(intCursor)
End If
Next
End If
End If
End Sub
End Class
DetailsView (the second section of code from the original post....the code that works to hide both labels)
<asp:DetailsView ID="dvSurveyQuestions" runat="server"
AllowPaging="True" AutoGenerateRows="False"
CellPadding="4" DataKeyNames="QuestionID"
DataSourceID="dsSurveyQuestions" ForeColor="#333333"
GridLines="None" Height="50px" Width="100%" BorderWidth="1px"
BorderColor="#F7F6F3">
<Fields>
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:HiddenField ID="hiddenQuestionID" runat="server" Value='<%# Bind("QuestionID") %>'>
</asp:HiddenField>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Answer(s)" SortExpression="PicklistID">
<ItemTemplate>
<asp:HiddenField ID="hiddenPicklistID" runat="server" Value='<%# Bind("PicklistID") %>' />
<asp:BulletedList ID="blText" runat="server" DataSourceID="dsPicklist" DataTextField="TEXT">
</asp:BulletedList>
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="dsPicklist" runat="server"
ConnectionString="<%$ ConnectionStrings:SurveyConnectionString %>"
SelectCommand="SELECT p.TEXT FROM PICKLIST p
JOIN C_Survey_Questions c
ON p.PICKLISTID = c.PicklistID
AND c.QuestionID = @QuestionID
AND c.SurveyID = @SurveyID
WHERE p.PICKLISTID IS NOT NULL
AND c.PicklistID IS NOT NULL">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="SurveyID"
PropertyName="SelectedValue" Type="Int32" />
<asp:ControlParameter ControlID="hiddenQuestionID" Name="QuestionID"
PropertyName="Value" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
The site is located at:
http://products.pfgbest.com/Survey/Survey.aspx then scroll down to Question number 24 (Do you intend to work with a Broker or participate in a Managed program?) Self-Directed just WILL NOT go away!
I would put a breakpoint just inside the foreach loop and step through the code carefully, paying attention to the lbl value and whether the visible false is triggered when it should.
Superguppie.
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.
@Metal, I put a breakpoint on the line and it would skip over the hide part when on the words "Self Directed" but not over the word "Systems."
@SuperGuppie, I don't believe there are any trailing spaces and since this type of code is used on 2 different pages, it makes no sense to me that "Self Directed" would get hidden on one page, but not the one in question.
There is an ID field that associates the text, I could try to use that again and see if I can hide the labels, but every time I try it, I must write it wrong because none of the labels hide.
Can either of you help with code that will hide ID = 0 and ID = 2?
Ok. One last crazy idea if you will bare wih me. Create a brand new page and copy and paste all the markup/code from the existing page to this new page. Any luck?
I think you don't need to use foreach loop, remove the foreach loop and try
If QID = 33 Then
Dim lbl As Label = CType(e.Item.FindControl("Label3"), Label)
If lbl IsNot Nothing Then
If lbl.Text = "Self Directed" Or lbl.Text = "Systems" Then
lbl.Visible = False
End If
End If
End If
Please 'Mark as Answer' if my post helped you
--------------------------------------------------
Muhammad Amin
محمد امين
grinnellja
Participant
1221 Points
624 Posts
Hiding text works in DetailsView but not in DataList
Feb 16, 2012 08:26 PM|LINK
Protected Sub DataList2_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then Dim DataList2 = DirectCast(sender, DataList) Dim ParentItem = DirectCast(DataList2.NamingContainer, DataListItem) Dim HiddenField2 = DirectCast(ParentItem.FindControl("HiddenField2"), HiddenField) Dim QID As Integer = Int32.Parse(HiddenField2.Value) If QID = 33 Then For Each li As DataListItem In DataList2.Items 'retrieves the label control in the current DataListItem Dim lbl As Label = CType(e.Item.FindControl("Label3"), Label) If lbl IsNot Nothing Then If lbl.Text = "Self Directed" Or lbl.Text = "Systems" Then lbl.Visible = False End If End If Next End If End If End Sub<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1" Width="100%" CellPadding="4" ForeColor="#333333"> <ItemTemplate> <asp:HiddenField ID="HiddenField2" runat="server" Value='<%# Eval("QuestionID") %>'> </asp:HiddenField> <asp:Label ID="lblQuesNum" runat="server" Font-Bold="True" Text='<%# Eval("QuestionNum") %>'> </asp:Label> <strong>)</strong> <asp:Label ID="Label2" runat="server" Font-Bold="True" Text='<%# Eval("Question") %>'> </asp:Label> <asp:HiddenField ID="hiddenPicklistID" runat="server" Value='<%# Eval("PicklistID") %>'> </asp:HiddenField> <asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("AnswerType") %>'> </asp:HiddenField> <asp:DataList ID="DataList2" runat="server" DataSourceID="dsPicklist" OnItemDataBound="DataList2_ItemDataBound"> <ItemTemplate> <asp:HiddenField ID="hidPickID" runat="server" Value='<%# Eval("PICKLISTID") %>'></asp:HiddenField> <asp:Label ID="Label3" runat="server" Text='<%# Eval("TEXT") %>'></asp:Label> </ItemTemplate> </asp:DataList> <asp:Label ID="Label1" runat="server" Text='<%# Eval("Script") %>'></asp:Label> </asp:DataList>This code will hide "Systems" but not "Self Directed". I have checked capitalization and the way it is worded and it is written EXACTLY the same way it is in the database. I also have code similar to this on another page that is hiding both "Self Directed" and "Systems". Pretty much the only difference here is that one takes place in a DataList, the other (working code) takes place in a DetailsView.(which is shown below)
Protected Sub dvSurveyQuestions_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles dvSurveyQuestions.DataBound 'The event handler checks the row count of the DetailsView control. If it is zero then the mode of the DetailsView is changed to Insert using ChangeMode() method. If dvSurveyQuestions.Rows.Count = 0 Then dvSurveyQuestions.ChangeMode(DetailsViewMode.Insert) End If If dvSurveyQuestions.CurrentMode = DetailsViewMode.ReadOnly Then 'Finds the hidden field of Question ID for data binding purposes Dim QuestionID As Integer = CInt(CType(dvSurveyQuestions.FindControl("hiddenQuestionID"), HiddenField).Value) If QuestionID = 33 Then Dim blText As BulletedList = dvSurveyQuestions.FindControl("blText") For intCursor = (blText.Items.Count - 1) To 0 Step -1 If blText.Items(intCursor).Text = "Self Directed" Or blText.Items(intCursor).Text = "Systems" Then blText.Items.RemoveAt(intCursor) End If Next End If End If End Sub End ClassDetailsView (the second section of code from the original post....the code that works to hide both labels) <asp:DetailsView ID="dvSurveyQuestions" runat="server" AllowPaging="True" AutoGenerateRows="False" CellPadding="4" DataKeyNames="QuestionID" DataSourceID="dsSurveyQuestions" ForeColor="#333333" GridLines="None" Height="50px" Width="100%" BorderWidth="1px" BorderColor="#F7F6F3"> <Fields> <asp:TemplateField Visible="false"> <ItemTemplate> <asp:HiddenField ID="hiddenQuestionID" runat="server" Value='<%# Bind("QuestionID") %>'> </asp:HiddenField> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Answer(s)" SortExpression="PicklistID"> <ItemTemplate> <asp:HiddenField ID="hiddenPicklistID" runat="server" Value='<%# Bind("PicklistID") %>' /> <asp:BulletedList ID="blText" runat="server" DataSourceID="dsPicklist" DataTextField="TEXT"> </asp:BulletedList> </ItemTemplate> </asp:TemplateField> </Fields> </asp:DetailsView> <asp:SqlDataSource ID="dsPicklist" runat="server" ConnectionString="<%$ ConnectionStrings:SurveyConnectionString %>" SelectCommand="SELECT p.TEXT FROM PICKLIST p JOIN C_Survey_Questions c ON p.PICKLISTID = c.PicklistID AND c.QuestionID = @QuestionID AND c.SurveyID = @SurveyID WHERE p.PICKLISTID IS NOT NULL AND c.PicklistID IS NOT NULL"> <SelectParameters> <asp:ControlParameter ControlID="DropDownList1" Name="SurveyID" PropertyName="SelectedValue" Type="Int32" /> <asp:ControlParameter ControlID="hiddenQuestionID" Name="QuestionID" PropertyName="Value" Type="Int32" /> </SelectParameters> </asp:SqlDataSource>The site is located at: http://products.pfgbest.com/Survey/Survey.aspx then scroll down to Question number 24 (Do you intend to work with a Broker or participate in a Managed program?) Self-Directed just WILL NOT go away!
MetalAsp.Net
All-Star
112752 Points
18373 Posts
Moderator
Re: Hiding text works in DetailsView but not in DataList
Feb 16, 2012 08:32 PM|LINK
grinnellja
Participant
1221 Points
624 Posts
Re: Hiding text works in DetailsView but not in DataList
Feb 17, 2012 07:44 PM|LINK
I edited the original post with the HTML markup and the URL of the site......thanks
MetalAsp.Net
All-Star
112752 Points
18373 Posts
Moderator
Re: Hiding text works in DetailsView but not in DataList
Feb 18, 2012 02:33 AM|LINK
superguppie
All-Star
48225 Points
8679 Posts
Re: Hiding text works in DetailsView but not in DataList
Feb 20, 2012 03:28 PM|LINK
Have you checked if there are trailing spaces?
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.
grinnellja
Participant
1221 Points
624 Posts
Re: Hiding text works in DetailsView but not in DataList
Feb 21, 2012 05:40 PM|LINK
@Metal, I put a breakpoint on the line and it would skip over the hide part when on the words "Self Directed" but not over the word "Systems."
@SuperGuppie, I don't believe there are any trailing spaces and since this type of code is used on 2 different pages, it makes no sense to me that "Self Directed" would get hidden on one page, but not the one in question.
There is an ID field that associates the text, I could try to use that again and see if I can hide the labels, but every time I try it, I must write it wrong because none of the labels hide.
Can either of you help with code that will hide ID = 0 and ID = 2?
MetalAsp.Net
All-Star
112752 Points
18373 Posts
Moderator
Re: Hiding text works in DetailsView but not in DataList
Feb 21, 2012 06:01 PM|LINK
Change "Self Directed" to "Self" everywhere and try again. What is the database column type of TEXT?
It just doesn't make sense that lbl.Text equals Self Directed, but your If statement is false. I hate it when logical things turn illogical!
grinnellja
Participant
1221 Points
624 Posts
Re: Hiding text works in DetailsView but not in DataList
Feb 21, 2012 06:08 PM|LINK
I changed it to "Self" and still nothing happened. In the database TEXT is varchar(64)
MetalAsp.Net
All-Star
112752 Points
18373 Posts
Moderator
Re: Hiding text works in DetailsView but not in DataList
Feb 21, 2012 06:22 PM|LINK
mameenkhn
Contributor
2026 Points
391 Posts
Re: Hiding text works in DetailsView but not in DataList
Feb 21, 2012 07:07 PM|LINK
I think you don't need to use foreach loop, remove the foreach loop and try
If QID = 33 Then Dim lbl As Label = CType(e.Item.FindControl("Label3"), Label) If lbl IsNot Nothing Then If lbl.Text = "Self Directed" Or lbl.Text = "Systems" Then lbl.Visible = False End If End If End If--------------------------------------------------
Muhammad Amin
محمد امين