ASP.NET: Get value of literal.text after page load? - asp.net

I'm trying to use the following code to get the text value of a literal so that I can pass it to the next page using PreviousPage, but the string is empty, I think it clears the values after the Page_Init stage.
Public ReadOnly Property SendText() As String
Get
Return literal1.Text
End Get
End Property
Is there a way around this?
Im trying to access the data on the next page using:
<%# PreviousPageType VirtualPath="~/Spacing.aspx" %>
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
litTables.Text = PreviousPage.SendText
End Sub
Even better would be if I could set SendText to the HTML contents of a whole div, but the only way I see to access this is using the JavaScript document.getElementById("tables").innerHTML, but I don't see how I could incorporate that into the ReadOnly Property using Get.
I'm using Visual Studio Express For Web 2012.

This isn't really the way you would pass data between pages in an ASP.NET application. The recommended way would be to POST the data or pass it via the URL.

The data I need to transfer is:
<div id="tables">
<asp:Literal ID="lit1" runat="server" ValidateRequestMode="Disabled"></asp:Literal>
<asp:Literal ID="lit2" runat="server"></asp:Literal>
<asp:Literal ID="lit3" runat="server"></asp:Literal>
</div>

Related

Asp button does not fire OnClick event

I've searched the forums and have yet to find a soln to my specific problem. I'm writing an asp.net aspx page with
<asp:Button ID="Add" OnClick="Add_Click" runat="server" Text="Add" CausesValidation="False"/>
where Visual Studio even autofilled my OnClick function, and my VB code behind function looks like this:
Protected Sub Add_Click(sender As Object, e As EventArgs) Handles Add.Click
Server.Transfer("TimeReportingAdd.aspx", False)
End Sub
I have another page with a button set similarly and it works fine. Does not even hit event during a debug. The main page (TimeReporting.aspx) just refreshes on the click and I would like it to redirect to another page (TimeReportingAdd.aspx). The button was not copied but created new and the event was created by double clicking in the designer. runat="server" is present in all my content headers etc. I don't think my TimeReporting.aspx page header needs any validation elements because the page that works doesn't have them. The Add button is declared as follows in my vb code behind:
Protected WithEvents Add As Global.System.Web.UI.WebControls.Button
Also, the button that works in another page is easier, doesnt even have onclick or validation:
<asp:Button ID="Button1" runat="server" Text="Query" />
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
...
End Sub
I've tried deleting the button and adding it again multiple times. Thanks in advance!
Please remove OnClick="Add_Click". This will fix your problem.
There are two things you need to consider:-
When you add OnClick, this means you want to handle click in Java Script.
To handle click in server side, you provide a method in code behind with Handles clause.
First approach overrides second one.
I was able to get this to work by giving this property in my button:
PostBackUrl="~/TimeReportingAdd.aspx"
It was posting back to the initial page TimeReporting.aspx

ASP.NET hyperlink in a IF statement?

I currently have a asp hyperlink control that links to a login page and there is a button called sign out which clears the session variables. I was thinking of trying to clear the session variables with the hyperlink control without going to the login page. Is there any way I could do this maybe with an IF statement?
This is my code for the hyperlink control:
<asp:HyperLink runat="server" ID="Log_out" Class="sign_up" visible="false" NavigateUrl="/Pages/login.aspx">Sign out</asp:HyperLink>
According to the comments, you need this link's code in VB.NET (you only mentioned .NET): https://stackoverflow.com/a/7231659/284240
If so, here it is. Let us know if you are looking for something else.
Public Sub linkGoSomewhere_Click(sender As Object, e As EventArgs)
Response.Redirect("Default.aspx")
End Sub
See this link for how to set onclick event in code behind:
How to code a Button's Click Event?

Display loading gif while web page loads

I am using VS 2013 (asp.net, VB).
I have a web page that has a sub that executes a lot of code. The sub runs when the page loads but also when a button is clicked. The code within the sub basically imports values from a database and then saves them to an excel document which then performs a solver calculation and saves the excel document.
Anyway, the page load time is quite long so I want to display a loading GIF whilst the sub is executed.
I thought I could do something like the following.
The gif:
<div id="loadingGif" runat="server">
Loading. Please Wait....<br /><br />
<img src="images/RTO_Loader.GIF" alt=""/>
</div>
The page load:
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
loadingGif.Visible = True
exportToExcelEvent()
loadingGif.Visible = False
End Sub
This doesn't work. I don't get any errors and the excel document saves fine. I haven't been able to find any understandable help on this. From what I have read threading may solve this but I cant seem to get it working.
Thanks if you can help.
Remove runat="server" attribute from 'loadingGif' div and replace it with:
style="display:none;"
Also remove lines from Page_Load that change visibility of that div.
Add OnClientClick event to button and set it to:
OnClientClick="document.getElementById('loadingGif').style.display = 'block';"
You can move that javascript to functiona and call function instead.
Another option is to use Ajax UpdatePanel with UpdateProgress.

asp.net redirect to section of a different page without using # tags

I'm trying to get to a particular section of a glossary page without using the #section in the url redirect. Basically, I'm trying to go from page a to a section of page b where the urls should look like:
http://location/a.aspx and http://location/b.aspx
But instead of just loading page b, go to a particular section of b, which normally I would do with anchor tags.
So for example, the normal method would be:
http://location/b.aspx#section
I want to do that without having that extra bit in the URL. I thought perhaps using a session variable, but once I get to the new page, I don't really know what to code to change to that new section without again adding that #section to the url. I'm using vb as the code behind the pages.
How can I accomplish this?
You could also try Page.SetFocus method.
Usage is as follows:
SetFocus(Control) or SetFocus(String) where Control is the object you want to focus on and String is the ID of the control you want to focus on.
Example:
ASPX Page:
<body>
<form id="form1" runat="server">
<asp:textbox id="textbox1" runat="server" /><br />
<asp:textbox id="textbox_focus" runat="server" />
</form>
</body>
Code-behind:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Page.SetFocus("textbox_focus")
End Sub

FormView on a Master Page can't see databound controls through ContentPlaceHolder boundary

I have a number of similarly structured FormViews. In an effort to avoid duplicate markup, I've created a master page that contains the FormView, and placed a ContentPlaceHolder inside the FormView. The specific databound controls - which are the only thing that change from page to page - are then on the page that uses that master page.
So I have a master page that looks something like this:
<%# master ... %>
...
<form runat=server>
...
<asp:formview runat="server" ... >
<edititemtemplate>
... Lots of common markup ...
<asp:contentplaceholder id='FormRows' runat='server' />
... Lots more common markup ...
</edititemtemplate>
</asp:formview>
...
</form>
and a page using that master page that looks something like this:
<%# page masterpagefile="Form.Master" ... %>
<asp:content contentplaceholderid="FormRows" runat="server" >
...
<p>
Field One:
<asp:textbox runat=server text='<%#Bind("Field1")%>' id='Field1' />
</p>
<p>
Field Two:
<asp:textbox runat=server text='<%#Bind("Field2")%>' id='Field2' />
</p>
...
</asp:content>
With an existing record, the FormView sees through to the databound controls (Field1, etc) and populates them with the correct data. But when inserting or updating, it doesn't see them, and they're not included in the insert or update. In the FormView_ItemInserting event, e.Values is empty; likewise in the FormView_ItemUpdating event, e.NewValues is empty.
So:
Is there a way to provoke the FormView on the master page to see through to the databound controls inside the ContentPlaceholder?
Failing that, is there a straightforward way of identifying controls that are databound with <%#Bind(...)%> so that I can add them manually to the values bag?
There are a couple of things that come to mind why this setup will not work and may lead to more code than markup.
If you have a datasource defined in the master page it will not handle the different data bound controls from each page without adding more logic to the master page to change the query etc.
All form views will be coupled together increasing the complexity of changes down the road
I would go with separate pages for each FormView reducing the complexity of code, debugging and the ability to change
Just my two cents
I think this will prove difficult, if not possible; in fact I'm surprised that the databinding works at all!
You may want to try a different method of encapsulating your FormView control.
You could try placing the FormView control in an .ascx control with a PlaceHolder where you now have the ContentPlaceHolder.
Then on each ASPX page, you could have a mirror ASCX page that contains the filler for the placeholder. You could give them the same names (Page1.aspx uses Page1.ascx) or set up a naming convention like Page1-Content.ascx, so that your FormView ascx would figure out what it's filler control is named, use Page.LoadControl() to load the control by path, and plug that content in during the Init phase.
Now, your content controls have the advantage of being able to have public properties, so you could bind to those public properties, and have the properties shuttle the data to and from the appropriate server controls in the filler .ascx file.
Unfortunately it's double the files (because of the ASPX and ASCX required for each page) but fairly work-unintensive compared to the alternative (duplicating all that code)
Of course, you haven't told us what all your common markup is, but your common markup could go into a CommonMarkupHeader.ascx and CommonMarkupFooter.ascx as well and included on each page's unique FormView.
Where do you have server form tag? May be in content place holder insted of master page, so your values not send to server page after submit
You might be able to do something like this...
Define an interface for your "data pages" that has a method signature that returns a bindable data source..
public interface IFormViewChild {
IEnumerable GetFormData();
}
Then you can have your "data pages" implement that interface...
public class ChildDataPage : Page, IDataPage {
public IEnumerable GetFormData() {
// code to return stuff here
}
}
Finally, in your masterpage's Load() event...
if (Page is IFormViewChild) {
myFormViewControl.DataSource = ((IFormViewChild)Page).GetFormData();
myFormViewControl.DataBind();
}
Please keep in mind that this is all psudo code typed directly into this web form editor.. so it's probably wrong. But it might not be :)
Here's a provisional solution - not elegant, but it works. In the code-behind for Form.Master I have something along these lines:
Private Sub FormView1_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertEventArgs) Handles FormView1.ItemInserting
ManuallyAddValues(e.Values)
End Sub
Private Sub FormView1_ItemUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewUpdateEventArgs) Handles FormView1.ItemUpdating
ManuallyAddValues(e.NewValues)
End Sub
Private Sub ManuallyAddValues(ByRef Values As IOrderedDictionary)
For Each Field As Core.Field In FormView1.DataSourceControl.IncludedFields
If Values(Field.Name) Is Nothing Then
Dim DataboundControl As Control = FormView1.FindControl("FormRows").FindControl(Field.Name)
Values.Add(Field.Name, GetValue(DataboundControl))
End If
Next
End Sub
This isn't so elegant because
I have to know the names of all databound controls
This relies on the assumption that the ID of each control matches the fieldname
The 'GetValue' function (not included here) is a clumsy solution: it checks for various types (textbox, dropdownlist, checkbox, etc.) and gets the string value from the appropriate property (textbox.text, dropdownlist.selectedvalue, checkbox.checked, etc.).
I'd still love to at least have a way of knowing what's bound with the '<%#Bind("Foo")%>' syntax and getting that information directly.

Resources