Unable to change visibility of Panel in rendered HTML code - c#

I have an aspx page (say MyPage.aspx) where a part of it has the following structure -
<asp:DataList ... >
<HeaderTemplate>
...
</HeaderTemplate>
<ItemTemplate>
<asp:Table ID="table" runat="server">
<asp:TableRow ... >
<asp:TableCell ... >
<asp:ImageButton ID="btnToggle" OnClick="ToggleVisibility" ... >
</asp:TableCell>
...
</asp:TableRow>
</asp:Table>
<asp:DataGrid ... >
</asp:DataGrid>
<asp:Panel ID="panel" runat="server" ...>
<asp:Button ID="button1" runat="server" ...>
<asp:Button ID="button2" runat="server" ...>
</asp:Panel>
</ItemTemplate>
<AlternatingItemTemplate>
...
</AlternatingItemTemplate>
</asp:DataList>
What I am trying to do is that whenever btnToggle is clicked, it toggles visibility of panel. I'm getting the panel in ToggleVisibility() like this -
Dim panelToggle As Panel = sender.Parent.Parent.Parent.Parent.Controls(5)
In this function I'm able to change its Visible property, but its visibility doesn't change on the rendered HTML page (checking through browser).
I'm unable to figure out why is that. Kindly, help.
Thanks.

Add the OnItemCommand event to the DataList that handles the button click. You don't need to add the OnClick event to the button itself anymore.
<asp:DataList ID="DataList1" runat="server" OnItemCommand="DataList1_ItemCommand">
<ItemTemplate>
<asp:Button ID="btnToggle" runat="server" Text="Button" />
<asp:Panel ID="panel" runat="server">
Panel content.
</asp:Panel>
</ItemTemplate>
</asp:DataList>
Then in code behind
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
//find the panel in the datalist item object and cast it back to a panel
Panel panel = e.Item.FindControl("panel") as Panel;
//you can now access it's properties
panel.Visible = false;
}
VB
Protected Sub DataList1_ItemCommand(ByVal source As Object, ByVal e As DataListCommandEventArgs)
'find the panel in the datalist item object and cast it back to a panel
Dim panel As Panel = CType(e.Item.FindControl("panel"),Panel)
'you can now access it's properties
panel.Visible = false
End Sub

Related

Refreshing content in an update panel after postback

I have a simple command event that toggles the visibility of two buttons.
When the controls are wrapped in an update panel as below, each subsequent click does not toggle the visibility of the buttons even though the code executes as expected.
Testing
The code works as expected when the update panel is removed.
After clicking the button refreshing the page reflects the changes made.
The issue clearly lies with refreshing the content after a postback.
I have tried upd.update and updateMode="always"
ASPX
<asp:UpdatePanel ID="upd" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Repeater ID="rpt" runat="Server">
<ItemTemplate>
<asp:Button ID="btnOn" runat="Server" Text="ON" CommandName="ON" CommandArgument='<%#: Container.ItemIndex%>' UseSubmitBehavior="false" />
<asp:Button ID="btnOff" runat="Server" Text="OFF" CommandName="OFF" CommandArgument='<%#: Container.ItemIndex%>' UseSubmitBehavior="false" />
</ItemTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
VB.NET
Protected Sub Page_PreRenderComplete(sender As Object, e As EventArgs) Handles Me.PreRenderComplete
For Each item In rpt.Items
'Do a check and set output'
btnOn.Visible = Not Output
btnOff.Visible = Output
Next
End Sub

Setting Visiblity of panel inside of formview

I have a FormView that has elements that I want to make available to different users depending on the users access permissions. I have encased each of these inside of a panel and ID'd the panel so that I can call them out from the code behind potentially drilling into the FormView with FindControl but have been unsuccessful so far in getting it worked out.
I've never had much luck with FindControl and was wondering if some of you that are more knowledgeable about it could point me in the right direction here is an example of my code on the aspx and C# code behind that IS NOT working out!
If this were functioning then I would place conditionals to for valid groups under this protected void for this panel to allow these users access to this panel and likewise do the same for other panels where permission was applied.
In this way I would be presenting a custom FormView for each user group based on my ACL. But I just can't get through my head how to drill in with the FindControl properly.
ASPX SAMPLE:
<asp:FormView Width="100%" ID="ChangeFormFV" DefaultMode="Insert" runat="server" DataKeyNames="CAssetID" DataSourceID="UpdateSqlDataSource">
<InsertItemTemplate>
<asp:Panel runat="server" ID="ShortDescPnl" Visible="false">
</asp:Panel>
<asp:Panel runat="server" ID="LongDescPnl" Visible="false">
</asp:Panel>
<asp:Panel runat="server" ID="AddNotesPnl" Visible="false">
</asp:Panel>
<asp:Panel runat="server" ID="ManufacturerPnl" Visible="false">
</asp:Panel>
<asp:LinkButton runat="server" Text="Insert" CommandName="Insert" ID="InsertButton" CausesValidation="True" /> <asp:LinkButton runat="server" Text="Cancel" CommandName="Cancel" ID="InsertCancelButton" CausesValidation="False" />
</ItemTemplate>
C# Codebehind (not working):
protected void ChangeFormFV_Databound(object sender, EventArgs e)
{
if (Session["SessionUType"].ToString() == "ITSec")
{
ChangeFormFV.Row.FindControl("ShortDescPnl.visiblity")="true";
}
}
Appreciate any help that can be offered, been searching for references and reading all I can but just not getting what I need from my results.
The FindControl method returns the actual control. Set the visibility on the control itself. Something like this:
Panel control = ChangeFormFV.Row.FindControl("ShortDescPnl") as Panel;
if (control != null)
control.Visible = true;

ASP.NET InsertItemTemplate always visible

I have a ListView. At the moment I have to click in a Button to show the InsertItemTemplate, as shown below.
Default.aspx:
<LayoutTemplate>
<ul>
<asp:PlaceHolder ID="ItemPlaceHolder" runat="server" />
</ul>
<asp:Button ID="New" Text='New' CommandName="Create" OnClick="Novo_Click" runat="server" />
</LayoutTemplate>
Default.aspx.cs:
protected void Novo_Click(object sender, EventArgs e)
{
ListViewID.InsertItemPosition = InsertItemPosition.LastItem;
}
How can I have the InsertItemTemplace visible without needing to click a button?
Just add that line in the ListView declaration: InsertItemPosition="LastItem" to show, the template, at the end or
InsertItemPosition="FirstItem" to show, the template, in the top
The default is InsertItemPosition.None, which indicates that the InsertItemTemplate content will not be rendered by the ListView control.
<asp:ListView ID="ContactsListView"
DataSourceID="ContactsDataSource"
DataKeyNames="ContactID"
OnItemInserted="ContactsListView_ItemInserted"
InsertItemPosition="LastItem"
runat="server">
...
</asp:ListView>

Access DataLists inside the ListView

I'm OK with C# and VB.nET
I have a DataList (Question) inside the ListView (Section). ListiView is to hold the sections. DataList holds the questions of a section. Let's say I have 3 sections, each section has 2 questions.
<asp:ListView ID="lvSection" runat="server">
<LayoutTemplate>
<div id="itemPlaceholder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
<div>
<p><%#Eval("Section")%>
<asp:HiddenField ID="hfSectionId" runat="server" Value='<%#Eval("SectionId")%>' />
<hr />
</p>
</div>
<asp:DataList ID="dlQuestion" runat="server" >
<ItemTemplate>
<asp:Label ID="lblQuestion" runat="server" Text='<%# Eval("Question") %>'></asp:Label>
<asp:HiddenField ID="hfQuestionId" runat="server" Value='<%# Eval("QuestionId") %>' />
</ItemTemplate>
</asp:DataList>
</ItemTemplate>
</asp:ListView>
<br/>
<asp:Button runat="server" Text="Submit" onclick="Submit_Click" />
I'm trying to access the DataList dlQuestion when the button "Submit" is click like this:
Protected Sub Submit_Click(ByVal sender As Object, ByVal e As EventArgs)
'but I need to loop through all DataLists inside the ListView
'Maybe there are ways to get all the DataLists into a collection and then can loop through each one of them
'this will get only one DataList. Here's the pseudocode
Dim question As DataList = lvSection.FindControl("dlQuestion")
For Each item As DataListItem In quest.Items
Dim questionId As HiddenField = item.FindControl("hfQuestionId")
Next
End Sub
But it does not get anything back, question always gets nohting. I think it's because there are 3 DataList inside the ListView now, due to 3 sections, and it cannot find DataList dlQuestion anymore. How do I access these DataLists of ListView from the code behind? I need to loop through each control of the DataList .
Thank you.
You would need to do it as this:
for each item As ListViewDataItem in lvSection.Items
Dim list As DataList = item.FindControl("dlQuestion")
If (list IsNot Nothing) Then
For Each dlItem As DataListItem In quest.Items
Dim questionId As HiddenField = dlItem.FindControl("hfQuestionId")
Next
End If
Next
You have to access the data list in each item in the ListView first, not at the listview level.

Values set inside dropdowns selectedindexchanged not showing in modalpopupextender

I am using a modal popup that contains a dropdown box. When the dropdown is changed I'm trying to retrieve data and assign it labels also within the modal. I observe the label values being set in the debugger but they do not show in the modal.
Modal/Panel Code:
<asp:Panel ID="pnlUpdate" runat="server" CssClass="modalPopup">
<div>
<asp:UpdatePanel runat="server" ID="upSubnetUpdate" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label runat="server" ID="pnlLblSubnet" CssClass="searchLabel">Subnet:</asp:Label>
<asp:DropDownList runat="server" ID="ddlSubnet" OnSelectedIndexChanged="ddlSubnet_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList><br />
</ContentTemplate>
</asp:UpdatePanel>
</div>
<div>
<asp:Label runat="server" ID="lblIPStartUpdate"></asp:Label>
<asp:Label runat="server" ID="lblIPEndUpdate"></asp:Label>
<asp:Label runat="server" ID="lblGatewayUpdate"></asp:Label>
<asp:Label runat="server" ID="lblSubnetMaskUpdate"></asp:Label>
</div>
</asp:Panel>
Dropdown Code
protected void ddlSubnet_SelectedIndexChanged(object sender, EventArgs e)
{
SubnetInfo si = GetSubnetInfo(ddlSubnet.SelectedItem.Text);
lblIPStartUpdate.Text = si.IP_Start;
lblIPEndUpdate.Text = si.IP_End;
lblGatewayUpdate.Text = si.Gateway;
lblSubnetMaskUpdate.Text = si.Subnet_Mask;
}
I'm not sure if this is a page lifecycle issue or a limitation of the modal popup.
Thanks for the help!
You need to put the DropDown and the labels in an UpdatePanel. The dropdown is in an UpdatePanel, but it cannot update the labels if they're not in an UpdatePanel too.

Categories

Resources