Show/hide HTML table row in include file - asp.net

I have a page (Default.aspx) that uses an include file to display left navigation bar content.
<asp:Content ID="Content4" ContentPlaceHolderID="ContentPlaceHolder_leftnav" Runat="Server">
<!--#include file="../../includes/menus/left_nav/menu.htm" -->
</asp:Content>
In menu.htm, I have an HTML table. There is one menu item (table row) that I want to display for only certain users, so I have it set to display:none;.
In the codebehind of Default.aspx, if the user is in the list of allowed users, I want to change the display attribute of that table row, but I can't figure out how to access it. Any ideas? Is there a better way to do it?
Thanks in advance.

A quick way to refactor this may be to convert the menu into a user control that contains a Table control that is easy to manipulate:
<%# Register TagPrefix="uc" TagName="Menu" Src="~/Menu.ascx" %>
<asp:Content runat="server" ID="Content4" ContentPlaceHolderID="ContentPlaceHolder_leftnav">
<uc:Menu runat="server" id="ucMenu" />
</asp:Content>
Menu.ascx replaces menu.htm and would look something like this:
<asp:Table ID="tblMenu" runat="server">
<asp:TableRow ID="trSecured" runat="server">
<asp:TableCell ID="tdSecured">Secured (Make Hyperlink)</asp:TableCell>
</asp:TableRow>
<asp:TableRow ID="trNonSecured1">
<asp:TableCell ID="tdNonSecured">Non-Secured (Make Hyperlink)</asp:TableCell>
</asp:TableRow>
</asp:Table>
Then in your Menu.ascx User Control you can handle the menu display logic:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'CanViewSecuredPage should be in it's own class so you can share it between menu display
'logic and for securing the actual page itself (in case they navigate directly by URL).
If Not CanViewSecuredPage() Then
Dim tblTable As Table = CType(Me.FindControl("tblMenu"), Table)
'Or do .FindControl to not be bound to ordinal.
tblTable.Rows(0).Visible = False
End If
End Sub

Related

Understanding UpdatePanels

I am trying to understand UpdatePanels and best practise for using them.
I am using .Net4.0 with VB.Net.
The idea is to create a conversation app for a clients website and so I have control called Convo.ascx. Code added below.
<asp:UpdatePanel runat="server">
<ContentTemplate>
<h2>Conversation</h2>
<p><asp:Literal ID="lit1" runat="server" /></p>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" />
</ContentTemplate>
</asp:UpdatePanel>
Convo.ascx.vb
Partial Class Convo
Inherits System.Web.UI.UserControl
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
lit1.Text = lit1.Text & "<p>" & TextBox1.Text & "</p>"
End Sub
End Class
On a load page (Default.aspx) I have:
<%# Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<%# Reference Control="~/Convo.ascx" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:scriptmanager ID="Scriptmanager1" runat="server"></asp:scriptmanager>
<div>
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" Text="Add Conversation" />
<asp:PlaceHolder ID="phConversation" runat="server">
</asp:PlaceHolder>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
With Codebehind Default.aspx.vb as
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
AddConvo()
End Sub
Private Sub AddConvo()
Dim getPh As New PlaceHolder
getPh = CType(Me.FindControl("phConversation"), PlaceHolder)
Dim ucConvo As New Convo
ucConvo = CType(LoadControl("~/Convo.ascx"), Convo)
getPh.Controls.Add(ucConvo)
End Sub
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
AddConvo()
End Sub
End Class
So the Convo I add OnLoad remains on the page after extra as been added been any convo added after load is gone once the button on Convo is hit.
So my question is, how can I have these add and remain? Eventually they will be added to database but right now I am trying to understand UpdatePanels as they will become the foundation for this app.
Is there a very good explanation of multi-use UpdatePanels anywhere?
Thanks in advance
PS, im a hobbiest so only VB responses please
The issue actually isn't with the UpdatePanel, but with ASP.NET. ASP.NET web forms uses a control hierarchy for the entire page, and you are adding the controls to the hierarchy "dynamically". Since you are doing it that way, ASP.NET requires you add the control back into the control hierarchy on every postback to the server. The UpdatePanel is a way to post back to the server, and therefore you must re-add the old user controls and new ones to that hierarchy.
Essentially the UpdatePanel was added to make AJAX easy, but you still have to work within the rules of ASP.NET.

Get Updated Object when using ObjectDataSource and Repeater

I have a data repeater that is binding to an ObjectDataSource on a page. I have the select working but I am having a problem with the Update. When I call a Save button what I want to do is call the function specified in the UpdateMethod and pass it in a parameter of the changed object of the repeater. Problem is I can't figure out how to get the object back out of the repeater. I do not want to specify each individual field as an update parameter as that is really unwieldy and defeats the purpose of data binding. Any help on this would be great.
<%# Page Language="VB" %>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="ObjectDataSource1" ItemType="CompanyObject">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" CssClass="clsLabel">Company:</asp:Label>
<asp:TextBox ID="txtCompany" runat="server" Text='<%# BindItem.Company%>'></asp:TextBox>
</ItemTemplate>
</asp:Repeater>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
SelectMethod="GetData" TypeName="WebApplication1.CompanyObject"
UpdateMethod="UpdateCompany" DataObjectTypeName="CompanyObject"></asp:ObjectDataSource>
</form>
</body>
</html>
Here is the code behind that I want to call:
Public Function UpdateCompany(ByVal company As tblCompany)
'Save the Value here except that company is always null
End Function
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
ObjectDataSource1.Update()
End Sub
You can't do that with a Repeater control: it doesn't keep a copy of the objects to which it was bound. It doesn't do two-way binding.
But other controls do. Check out the FormView, GridView, and DetailsView. For a full treatment of the subject, see here.
I'm not sure I completely follow what's going on here, and I don't have enough reputation to ask in a comment. Where is your save button? I'm guessing it's outside the repeater. Are you trying to save all the companies that appear in your repeater, or is there only one company anyway? What are you trying to get back out of the repeater? Just the company name that's in the textbox? A few more details may provide the help you're looking for.
Also, it might be helpful to add an OnUpdating event to your ObjectDataSource and handle that in your code behind.
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
SelectMethod="GetData" TypeName="WebApplication1.CompanyObject"
UpdateMethod="UpdateCompany" DataObjectTypeName="CompanyObject"
OnUpdating="Company_Updating"></asp:ObjectDataSource>
And then your code behind:
Private Sub Company_Updating(ByVal s As Object, ByVal e As ObjectDataSourceMethodEventArgs)
' use e.InputParameters here to pass in the values you need
End Sub
You can see here for an example of how to use InputParameters.
Update
To answer your question, you should be able to use the following to get the value out of the textbox in the repeater:
Protected Sub Company_Updating(ByVal s As Object, ByVal e As ObjectDataSourceMethodEventArgs)
If (Repeater1.Items.Count > 0) Then
e.InputParameters.Add("CompanyName", CType(Repeater1.Items(0).FindControl("txtCompany"), TextBox).Text
End If
End Sub
But I think a repeater is unnecessary for what you're trying to do here. A repeater is generally used to show a collection of items. If your goal is to simply show one company, couldn't you set the Text property of the TextBox control in your code behind?
Protected Sub Page_Load(ByVal s As Object, ByVal e As EventArgs) Handles Me.Load
If (Not Page.IsPostBack) Then
txtCompany.Text = yourCompanyObject.Name
End If
End Sub
This is not possible with the approach you are following.
We can for sure develop a custom Post Back to achieve the desired output.

PlaceHolders and Persistent Dynamic Controls (ADVANCED)

Warning! This problem is not for the feint of heart. I've spent several days in all trying to troubleshoot it.
I have a Wizard control with about 5 steps, each with several controls ranging from very simple to very complex such as custom jQuery combo boxes based on DropDownLists and TextBoxes. Each step has several of the controls wrapped in a PlaceHolder control.
The Wizard along with all PlaceHolders and their child Controls are nested inside of a View of a MultiView. On the same page I have another View styled like a Form, but not one. This view has corresponding PlaceHolders for each of PlaceHolders within each step of the Wizard.
Depending on the ReferralUrl I call the following function to "Toggle" the view from the Wizard to the form style view by moving all the controls, then setting the active view as follows:
Protected Sub ToggleView() Handles ViewToggle.Click
If Wizard_mv.ActiveViewIndex = 0 Then
ViewToggle.Text = "Toggle Wizard View"
fPH1.Controls.Add(wPH1)
fPH2.Controls.Add(wPH2)
fPH3.Controls.Add(wPH3)
fPH4.Controls.Add(wPH4)
fPH5.Controls.Add(wPH5)
Wizard_mv.ActiveViewIndex = 1
ElseIf Wizard_mv.ActiveViewIndex = 1 Then
ViewToggle.Text = "Toggle Form View"
wPH1.Controls.Add(fPH1)
wPH2.Controls.Add(fPH2)
wPH3.Controls.Add(fPH3)
wPH4.Controls.Add(fPH4)
wPH5.Controls.Add(fPH5)
Wizard_mv.ActiveViewIndex = 0
End If
End Sub
Immediately after this, I use another function for pre-filling the controls with values from a record in a database. After allowing the users to make some changes, they may resubmit the updated record to the database. The problem is that this works just fine if I do so from the Wizard but not after toggling. The trace shows that the Control Tree is empty at the time of submitting the updated record, and hence I cannot grab the user-entered/pre-filled values from this view. The pre-filling works great and the "Selected" values are all correct. The problem arises on the PostBack after clicking submit and it loses all the values and controls.
Please do not answer unless you fully understand my problem and are willing to help. I think the problem very well lies within the page lifecycle. Mysteriously, when I submit from my Wizard, on the postback in Page_Init I get my control values loaded, however when I submit from my form view, neither the controls nor their values are loaded. From what I read, this is an issue of persistence. Really hoping there's a relatively easy solution for this.
Here's a sample of my markup in my Wizard (all styling removed for brevity):
<asp:WizardStep ID="WizardStep1" runat="server" Title="Product Info">
<asp:PlaceHolder ID="wPH1" runat="server" ViewStateMode="Enabled">
<asp:Table ID="ProductInfoTable" runat="server" Width="100%">
<asp:TableHeaderRow>
<asp:TableHeaderCell><h3>Product Line</h3></asp:TableHeaderCell>
<asp:TableHeaderCell><h3>Publication Type</h3></asp:TableHeaderCell>
<asp:TableHeaderCell><h3>Request Type</h3></asp:TableHeaderCell>
</asp:TableHeaderRow>
<asp:TableRow>
<asp:TableCell>
<div class="ui-widget">
<!-- Autocomplete Combobox -->
<asp:DropDownList ID="productLine_ddl" runat="server" DataSourceID="productLineSDS" ViewStateMode="Enabled" DataTextField="Product" DataValueField="ID"></asp:DropDownList>
<asp:TextBox ID="productLine_cb" runat="server" EnableViewState="True"></asp:TextBox>
<button id="productLine_btn" type="button" title="Show All Items"></button>
</div>
</asp:TableCell>
<asp:TableCell>
<asp:DropDownList ID="docType_ddl" runat="server" DataSourceID="docTypeSDS" DataTextField="DocType" DataValueField="ID"></asp:DropDownList>
</asp:TableCell>
<asp:TableCell>
<asp:DropDownList ID="requestType_ddl" runat="server" DataSourceID="requestTypeSDS" DataTextField="RequestType" DataValueField="ID"></asp:DropDownList>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell ColumnSpan="2">
<asp:MultiView ID="Attachment_mv" runat="server" ActiveViewIndex="0">
<!-- File Upload/Browsing Display -->
<asp:View ID="AttachmentUploadView" runat="server">
<h3 class="inlineH">Attach File: </h3>
<asp:FileUpload ID="AttachmentFile_btn" runat="server" />
<asp:Button ID="UploadFile_btn" runat="server" Text="Upload File" />
</asp:View>
<!-- File Attached Display -->
<asp:View ID="FileAttachedView" runat="server">
<h3 class="inlineH">Uploaded File: </h3>
<asp:Label ID="FileAttachedLabel" runat="server" Text="Label"></asp:Label>
<asp:Literal ID="FilesOnServer" runat="server" />
</asp:View>
</asp:MultiView>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</asp:PlaceHolder>
</asp:WizardStep>
My Page Lifecycle Events, as requested (in chronological order for your convenience) :)
Dim referrerPage As String
'Initialize Dynamic controls here. These are controls that need to be prefilled, etc.
Private Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Init
'DO NOT PREFILL at this stage, as the Controls are not yet rendered and will cause errors.
' Use this section for detecting the "referrerPage" and missing "id" parameters when expected, etc.
If Not IsPostBack Then
ViewState.Clear()
End If
Try
referrerPage = Right(Request.UrlReferrer.AbsolutePath, Len(Request.UrlReferrer.AbsolutePath) - Request.UrlReferrer.AbsolutePath.LastIndexOf("/"c) - 1)
Catch ex As Exception
If Not String.IsNullOrEmpty(Session.Item("referrerPage")) Then
referrerPage = Session.Item("referrerPage")
End If
End Try
If StrComp(referrerPage, "wizard.aspx") <> 0 And String.IsNullOrEmpty(Session.Item("referrerPage")) Then 'Initialize Session state to remember "referrerPage"
Session.Add("referrerPage", referrerPage)
End If
If StrComp(referrerPage, "formdetails.aspx") = 0 Then
If String.IsNullOrEmpty(Request.Params("id")) Then
'This line checks for an expected "id" param value and if none exists, forwards the page back to "tcom.aspx"
Response.Redirect(Request.UrlReferrer.AbsolutePath)
Else
ToggleView()
End If
End If
End Sub
'Prefill Dynamic controls here.
Private Sub Page_PreLoad(sender As Object, e As EventArgs) Handles Me.PreLoad
If Not IsPostBack Then
productLine_ddl.DataBind()
docType_ddl.DataBind()
requestType_ddl.DataBind()
'...and several more DataBinds for each individual
' control in the wizard. Nothing more.
End If
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
'Benign code for querying the database to get User info for Page.User.Identity.Name
End If
End Sub
Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As EventArgs) Handles Me.PreRender
If Not IsPostBack Then
'Here is completely benign code for hiding a couple controls.
End If
End Sub
Protected Sub Page_PreRenderComplete(ByVal sender As Object, ByVal e As EventArgs) Handles Me.PreRenderComplete
'PREFILL HERE when appropriate. This occurs after checking the "referrerPage" and ensuring a value for the "id" parameter.
If Not IsPostBack Then
Try
If Not String.IsNullOrEmpty(Request.Params("id")) Then
PrefillWizard(Request.Params("id"))
Else : output.Text += "Source: " + Request.UrlReferrer.AbsolutePath
End If
Catch ex As Exception
End Try
End If
End Sub
To anyone who can help with this, you have my eternal gratitude. ;)
The problem is that the control tree of the page must be exactly the same during every postback. That means that you have to add all the control every time no mater which ActiveViewIndex is set. Preferably in CreateChildControls or page init. In the ToggleView function you can than set the visibility of the controls.

How to use Eval in codebehind to set Page.Title

I have a SQLDataSource that is bound to a ListView control but I want to place parts of the bound record into the HTML TITLE attribute. Here is my codebehind file that I want to change so it can use Eval to construct a dynamic TITLE based on the data content:
Public Partial Class zShowAd
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.Page.Title = " Dynamically set in ASPX page"
'how to use Eval here instead of the above constant ??
End Sub
End Class
Here is the corresponding .aspx file:
<%# Page Language="vb" AutoEventWireup="false" MasterPageFile="~/zSEO.master"
CodeBehind="zShowAd.aspx.vb" Inherits="Zipeee.zShowAd" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div>
<asp:ListView ID="ShowAd" runat="server" DataSourceID="aPosting">
<LayoutTemplate>
<asp:PlaceHolder runat="server" ID="itemPlaceholder"></asp:PlaceHolder>
</LayoutTemplate>
<ItemTemplate>
<div>
<div id="wrapper">
<div id="header"></div>
<div id="main">
<div id="nav"> AdID: <%#Eval("AdID")%></div>
<div id="extras">Price: <%#Eval("Price")%></div>
<div id="content"> <%#Eval("AdDesc")%></div>
</div>
<div id="footer"></div>
</div>
</div>
</ItemTemplate>
</asp:ListView>
<asp:sqldatasource runat="server" id="aPosting"
ConnectionString="<%$ ConnectionStrings:ZIPeeeConnectionString2 %>"
SelectCommand="spGetAdByID" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:QueryStringParameter Name="AdId" QueryStringField="a" Type="String" />
</SelectParameters>
</asp:sqldatasource>
</div>
</asp:Content>
You can call a method (Sub) of the page's code behind by putting the following somewhere inside the ItemTemplate of your ListView:
<%# SetPageTitle(Eval("SomeProperty")) %>
Then in your code behind (sorry it's in C#):
protected void SetPageTitle(object title)
{
this.Title = title.ToString();
}
Alternatively, you can also pass the complete data item, instead of just one property:
<%# SetPageTitle(Container.DataItem) %>
Update (to answer your comment):
<%# ... %> is a so-called data-binding expression. It only works inside of a data-bound control (the ListView in your example) and it always works with the current record (typically you display more than one record in a data-bound control like the ListView).
So when you use <%# Eval("Price") %>, you are displaying the value of the current record's "Price" column. If your query, would return more than one record, then this would be executed for each record, and when setting the page title (as shown above), the page's title would be the value from the last record.
On the other hand <%= ... %>, is just a normal server-side code snippet (don't know if there is a specific name for it), which does not know about the data-binding context (e.g. which is the current record).
Please see the following question for more details: When should I use # and = in ASP.NET controls?

Showing completely different output based on the query-string

I am trying to learn asp.net (vb.net) and I'm having some trouble. I want to change a pages content based on the querystring.
In classic asp I would do:
<% If request.querystring("page") = 1 THEN %>
-entire page-
<% Else %>
-different page-
<% End If %>
The closest I could get in .net is
Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
If Request.QueryString("page") = 1 Then
lblMessage1.Text = "message"
Else
lblMessage1.Text = "message2"
End If
End Sub
That only seems good for small things. What would be the best method to change an entire page?
You could do the following (simple redirect):
If Request.QueryString("page") = 1 Then
Response.Redirect("MyPage1.aspx")
Else
Response.Redirect("MyPage2.aspx")
End If
You could also do this (read more here):
If Request.QueryString("page") = 1 Then
Server.Transfer("MyPage1.aspx")
Else
Server.Transfer("MyPage2.aspx")
End If
And finally one more option (show/hide different panels on the page):
If Request.QueryString("page") = 1 Then
MyPanel1.Visible = true
MyPanel2.Visible = false
Else
MyPanel1.Visible = false
MyPanel2.Visible = true
End If
I would suggest using the MultiView control.
In a nutshell, you would create two multiview "Views", each with the html that you would want to show. Then you could look at the querystring parameter and switch the active view of the multiview accordingly.
This has a lot of advantages to Response.Redirect() like others suggested. First off, that would always generate at least two browser requests. Also, Response.Redirect() throws a ThreadAborted exception behind the scenes, which can confuse people diagnosing the application.
Example MultiView control:
ASPX:
<form id="form1" runat="server">
<div>
<asp:MultiView ID="MultiView1" runat="server">
<asp:View runat="server">
Hi, this is Page 1
</asp:View>
<asp:View runat="server">
Hi, this is Page 2
</asp:View>
</asp:MultiView>
</div>
</form>
Code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Request.QueryString("page") = "1" Then
MultiView1.ActiveViewIndex = 0
Else
MultiView1.ActiveViewIndex = 1
End If
End Sub
You really have a few options, you could:
Response.Redirect(url) to a different page based on the input.
You could have an ASP:Panel with the "visible" property set to false and toggle that value based on the input.
Why not use different files instead? redirect to different pages. That would avoid having to have if statements everywhere.
OR
put your data in panels and just hide one or the other panel1.visible = (true/false). That's the best thing to do if you have to have it all in the same aspx page.
I prefer doing it on the ASPX page using DataBinding:
<asp:PlaceHolder runat="server" ID="Messages">
<asp:Label runat="server" Visible=<%# Request.QueryString("page") = 1 %> Text="Message 1" />
<asp:Label runat="server" Visible=<%# Request.QueryString("page") <> 1 %> Text="Message 2"/>
</asp:PlaceHolder>
Then on the server side:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Messages.DataBind()
End Sub
For future reference, you can still use the classic ASP way to control content. Here's an ASPX page I wrote just now:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%
if (3 == 9)
{%>
<span>Hello</span>
<%
}
else
{
%> <span>What?</span > <%
}
%>
</div>
</form>
</body>
</html>
When the page renders, it displays 'What?' on the page.
However, I would say that this is bad practise and poor design! Use either womp's suggestion of a multiview, or a page redirect.

Resources