I have a usercontrol which is on a webpage and that webpage is shown in a iFrame.
Form source:
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="FileUploadForm.aspx.vb"
Inherits="IST.FileUploadForm" %>
<%# Register Src="myFileUpload.ascx" TagName="myFileUpload" TagPrefix="uc1" %>
<!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>
<asp:PlaceHolder ID="ph1" runat="server"></asp:PlaceHolder>
</div>
</form>
</body>
</html>
Code behind:
Imports System.IO
Public Class FileUploadForm
Inherits System.Web.UI.Page
Private WithEvents myFileUpload1 As myFileUpload
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim myUploadLocation As String
myUploadLocation = Request.QueryString("loc")
If myUploadLocation = Nothing Then
myUploadLocation = Server.MapPath("~\temp")
End If
Dim allowed As String
allowed = Request.QueryString("accepts")
If allowed = Nothing Then
allowed = ""
End If
myFileUpload1 = CType(Me.LoadControl("myFileUpload.ascx"), myFileUpload)
myFileUpload1.ID = "fuOfferte"
myFileUpload1.AllowedFiles = allowed
myFileUpload1.FileLabelText = "File"
myFileUpload1.NotAllowedText = "This file extention is not allowed."
myFileUpload1.UploadButtonText = "Upload file"
myFileUpload1.UploadLocation = myUploadLocation
ph1.Controls.Add(myFileUpload1)
End Sub
Private Sub myFileUpload1_FileUploaded(file As String) Handles myFileUpload1.FileUploaded
Session("UploadedFileName") = file
End Sub
End Class
Behind code in main page:
pnMNCfile = New Panel
pnMNCfile.ID = "pnMNCfile"
pnMNCfile.Style("position") = "absolute"
pnMNCfile.Style("left") = "45px"
pnMNCfile.Style("top") = "30px"
pnMNCfile.Style("width") = "540px"
If Request.Browser.Browser.ToLower = "ie" = False Or (Request.Browser.Browser.ToLower = "ie" = True And CInt(Request.Browser.Version.Substring(0, 1)) > 8) Then
pnMNCfile.Style("height") = "80px"
Else
pnMNCfile.Style("height") = "87px"
End If
pnMNCfile.Style("z-index") = "999"
pnMNCfile.BorderStyle = BorderStyle.None
pnMNCfile.BackColor = Drawing.Color.LightGray
pnMNCfile.BorderWidth = 1
pnMNCfile.Attributes.Add("OnMouseOut", "BalloonPopupControlBehavior.hidePopup();")
pnMNC3.Controls.Add(pnMNCfile)
Dim lit As New Literal
lit.Text = "<IFRAME id=""frMNCfileUpload"" frameborder=""0"" scrolling=""auto"" allowtransparency=""true"" runat=""server"" width=""100%"" height=""100%"" src=""FileUploadForm.aspx?loc=" + CStr(Session("userfolder")) + "&accepts=xls_xlsx_doc_docx""></IFRAME>"
pnMNCfile.Controls.Clear()
pnMNCfile.Controls.Add(lit)
The problem is that when the page loads in IE9 you see the iframe very shortly (less than 0.5 sec). In Chrome it is visible without any problem.
When I do a right mouseclick in the iframe area and do a refresh, the content is visible.
I also tried with a border around the iframe. When it is not visible, also the border is not visible. In chrome you can then see the border too.
What is happening here?
Is this a bug in IE or (more likely) am I doing something wrong?
Dirty fix would be to add a auto refresh to the webpage with the usercontrol.
any thoughts?
rg,
Eric
I had the same issue with IE9 for various reasons and works with Chrome.
1. Configure Asp.net to use a cookieless session state. This might help...
<sessionState cookieless="true" mode="InProc"></sessionState>
</system.web>
</configuration>
or
2.
or try this too:
Response.Cache.SetCacheability(HttpCacheability.NoCache);
example:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="GeneralLedgerCodeListingCustomForm.aspx.cs" Inherits="xyz.GeneralLedgerCodeListingCustomForm" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%
Response.Cache.SetCacheability(HttpCacheability.NoCache);
%>
Related
I am trying to show a image from a server's folder, image name is specified by Query string, query gives te userid and then a image from folder with userid is rerieved,
Now i gettiing nothing in imageViewer, it is always blank .
this the code behind i am using :-
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Request.QueryString("UserID") <> "" Then
Dim UserID As String = Request.QueryString("UserID").ToString()
Dim img As Image = Me.FindControl("ImageUser")
img.ImageUrl = Server.MapPath("~\UserImages\" + UserID + ".png")
End If
End Sub
I also tried this code :-
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Request.QueryString("UserID") <> "" Then
Dim UserID As String = Request.QueryString("UserID").ToString()
UserImg.ImageUrl = Server.MapPath("~\UserImages\" + UserID + ".png")
End If
and this is my designer file :-
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="UserPhoto.aspx.vb" Inherits="IslamicPartner_web.UserPhoto" %>
<!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>
<asp:Image ID="ImageUser" runat="server" Height="489px" Width="703px" />
</div>
</form>
Where is the problem ?
Actually i want to view this in WebView in IOS app for user image , like Facebook
End Sub
If you look at the rendered HTML in a browser (usually via "View Source" or similar), you will see that you are sending a URL of something like "C:\Inetpub\websitename\UserImages\userid.png". This is not what you want as the browser does not have access to your web server's disks. The problem is that you are using Server.MapPath inappropriately.
What you want instead is something more like:
If System.IO.File.Exists(Server.MapPath("~\UserImages\" & UserID & ".png")) Then
img.ImageUrl = "\UserImages\" & UserID & ".png")
Else
img.ImageUrl = "\UserImages\UserUnknown.png"
End If
Afternoon All,
I have been advised that i can use iTextSharp to help me convert my web pages into .PDF files. I am using the following link as a sample tutorial but cannot generate the .pdf?
Visit http://www.dotnetspark.com/kb/654-simple-way-to-create-pdf-document-using.aspx
I am using the VB sample. I have added the iTextSharp.dll to my project and added the namespaces as requested. I have simply created a blank page and added a button to the page and using the following code i cant seem to get this to generate the file?
Here is my code...
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Partial Class pdf
Inherits System.Web.UI.Page
Protected Sub btnGeneratePDF_Click(ByVal sender As Object, ByVal e As EventArgs)
'Create Document class obejct and set its size to letter and give space left, right, Top, Bottom Margin
Dim doc As New Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35)
Try
Dim wri As PdfWriter = PdfWriter.GetInstance(doc, New FileStream("d:\myfolder\test.pdf", FileMode.Create))
'Open Document to write
doc.Open()
'Write some content
Dim paragraph As New Paragraph("This is my first line using Paragraph.")
Dim pharse As New Phrase("This is my second line using Pharse.")
Dim chunk As New Chunk(" This is my third line using Chunk.")
' Now add the above created text using different class object to our pdf document
doc.Add(paragraph)
doc.Add(pharse)
doc.Add(chunk)
Catch dex As DocumentException
'Handle document exception
Catch ioex As IOException
'Handle IO exception
Catch ex As Exception
'Handle Other Exception
Finally
'Close document
doc.Close()
End Try
End Sub
End Class
Here is the code for the button...
<%# Page Language="VB" AutoEventWireup="false" CodeFile="pdf.aspx.vb" Inherits="pdf" %>
<!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>
<asp:Button ID="btnGeneratePDF" runat="server" Text="Generate .PDF" />
</div>
</form>
</body>
</html>
Could someone please take a look at this for me and let me know where i am going wrong?
Regards
Betty
Not sure if this will fix it all, but you're at least missing the Click_Event from your buttton.
<asp:Button ID="btnGeneratePDF" runat="server" Text="Generate .PDF" OnClick="btnGeneratePDF_Click" />
(And I see that you asked this question yesterday: Button ClickEvent is not triggered with the same problem, try to remember next time ;-))
I'm new to ASP.NET but have quite a few successful test pages going now which I am using to slowly build up a new website and data application... hence my many questions on here.
Anyway, in my efforts to understand JSON, I have a test page trying to get the data out, but for some reason the script works fine when it's all one page, but not as code behind.
My ASPX file is:
<%# Page Language="VB" AutoEventWireup="false" CodeFile="json.aspx.vb" Inherits="jsonPage" %>
<!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 runat="server">
Output:
<div id="readOut" runat="server"></div>
</form>
</body>
</html>
Code behind:
Imports Jayrock.Json.JsonTextWriter, Jayrock.Json, Jayrock.Json.Conversion, System.Net
Partial Class jsonPage
Inherits System.Web.UI.Page
Sub Page_Load(Sender As Object, E As EventArgs)
Dim cMessage As String = "{""ID"": 8291, ""Item"": ""Epiphone Les Paul Tribute Plus Outfit"", ""Main Image"": ""8291-113247"", ""Colour"": ""Vintage Sunburst"", ""Option"": ""none"", ""Price"": 549.0}"
Dim objResponse As JsonObject = CType(JsonConvert.Import(cMessage), JsonObject)
readOut.InnerText = "Item name is: " & objResponse("Item")
End Sub
End Class
As I say, this is just a test code to try to get to grips with JSON, the text "Item name is:" followed by the result of the JSON parsing, should be posted into the div id="readOut" in the main ASPX page, but it won't... the strange thing is that it works is I take out the Page_Load sub and run the code in the head of the ASPX file.
I've tried comparing this to other files I have that are working and can find no obvious reason why this is happening.
I think this may be your problem. I suspect you weren't even able to hit that code with a breakpoint?
Page_Load(Sender As Object, E As EventArgs)
Should have a handles clause.
Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
When creating dynamic ajax controls you may experience a pre-render issue on postbacks. You are supposed to re-create the controls on postback, however if there are very many of them performance gets very slow between each postback. i.e. clicking on a combobox, it may take several seconds. So what I did was group the controls in panels, store the panels in a collection, then re-call the panels on Postback. This actually works great if the controls inside the panel are standard html controls (textbox, dropdowlist, etc.). But...doesn't work well with ajax controls...yet.
I have included a sample below. Uncomment/Comment the code to test it. If anyone has a good idea how to make this work with ajax controls that would be great.
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="PreRenderAjax.WebForm1" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<!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">
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
<div>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</div>
</form>
</body>
</html>
Public Class WebForm1
Inherits System.Web.UI.Page
Shared panellist As New List(Of Panel)
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If IsPostBack Then
'This is for reloading panels. Works with dropdownlist, but not ajax combobox
For Each pn As Panel In panellist
PlaceHolder1.Controls.Add(pn)
Next
'This for re-creating all the controls again. Not very efficient for ajax controls though.
'CreateInterface()
Else
CreateInterface()
End If
End Sub
Protected Overrides Sub CreateChildControls()
MyBase.CreateChildControls()
End Sub
Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
MyBase.OnInit(e)
EnsureChildControls()
End Sub
Private Sub CreateInterface()
Dim pn As Panel
pn = CreatePanel("panel1")
CreateControls(pn, 5)
pn = CreatePanel("panel2")
CreateControls(pn, 5)
pn = CreatePanel("panel3")
CreateControls(pn, 5)
pn = CreatePanel("panel4")
CreateControls(pn, 5)
End Sub
Private Function CreatePanel(ByVal name As String) As Panel
Dim pn As New Panel
pn.ID = name
pn.Width = 250
pn.BorderStyle = BorderStyle.Solid
pn.BorderColor = Drawing.Color.Blue
pn.Style.Add("margin", "5px")
pn.Style.Add("padding", "5px")
PlaceHolder1.Controls.Add(pn)
panellist.Add(pn)
CreatePanel = pn
End Function
Private Sub CreateControls(ByVal pn As Panel, ByVal howmany As Integer)
Dim cbo As AjaxControlToolkit.ComboBox
'Dim cbo As DropDownList
For i As Integer = 0 To howmany - 1
cbo = New AjaxControlToolkit.ComboBox
'cbo = New DropDownList
cbo.ID = pn.ID & "_cbo" & i
cbo.Width = 200
cbo.Items.Add("Item 1")
cbo.Items.Add("Item 2")
cbo.Items.Add("Item 3")
cbo.Items.Add("Item 4")
cbo.Items.Add("Item 5")
cbo.Style.Add("margin", "3px")
cbo.AutoPostBack = True
pn.Controls.Add(cbo)
Next
End Sub
End Class
I just did a test to see if postback slowness was from the ajax controls themselves or recreation. Well come to find out its tha actual ajax controls themselves. I created a blank page, put 40 ajax combobox's on (adding a couple items to each), turn on postcack. When I started the page and clicked an item it took about 3 or 4 seconds to complete the postback.
Did the same thing with dropdownlist and it works great. too bad you cant use it like a combobox.
This is a really weird one - I will do my best to explain.
I have a basic master page:
<%# Master Language="VB" CodeFile="MasterPage.master.vb" Inherits="master_MasterPage" %>
<!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>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
<asp:PlaceHolder ID="PH1" runat="server" />
<asp:PlaceHolder ID="PH2" runat="server" />
</div>
</form>
</body>
</html>
And a standard child page:
<%# Page Title="" Language="VB" MasterPageFile="~/master/MasterPage.master" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="master_Default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
</asp:Content>
I have the following extension methods for finding controls recursively:
Option Strict On
Option Explicit On
Imports System.Runtime.CompilerServices
Imports System.Web.UI
Public Module ExtensionMethods
<Extension()> _
Public Function FindControlRecursively(ByVal parentControl As System.Web.UI.Control, ByVal controlID As String) As System.Web.UI.Control
If parentControl.ID = controlID Then
Return parentControl
End If
For Each c As System.Web.UI.Control In parentControl.Controls
Dim child As System.Web.UI.Control = FindControlRecursively(c, controlID)
If child IsNot Nothing Then
Return child
End If
Next
Return Nothing
End Function
<Extension()> _
Public Function FindControlIterative(ByVal rootControl As Control, ByVal controlId As String) As Control
Dim rc As Control = rootControl
Dim ll As LinkedList(Of Control) = New LinkedList(Of Control)
Do While (rc IsNot Nothing)
If rc.ID = controlId Then
Return rc
End If
For Each child As Control In rc.Controls
If child.ID = controlId Then
Return child
End If
If child.HasControls() Then
ll.AddLast(child)
End If
Next
rc = ll.First.Value
ll.Remove(rc)
Loop
Return Nothing
End Function
End Module
I have a control with a listview:
<%# Control Language="VB" AutoEventWireup="false" CodeFile="control-1.ascx.vb" Inherits="controls_control_1" %>
<p>
Control 1</p>
<asp:ListView ID="lv" runat="server">
<ItemTemplate>
<div>
<asp:Literal ID="Name" runat="server" Text='<%#Eval("Name") %>' />
<asp:LinkButton ID="TestButton" runat="server">Test</asp:LinkButton>
</div>
</ItemTemplate>
</asp:ListView>
That is databound:
Partial Class controls_control_1
Inherits System.Web.UI.UserControl
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Dim l As New List(Of Person)
Dim j As New Person
j.Name = "John"
l.Add(j)
lv.DataSource = l
lv.DataBind()
End If
End Sub
End Class
Public Class Person
Public Property Name As String
End Class
I have a second control that is very basic:
<%# Control Language="VB" AutoEventWireup="false" CodeFile="control-2.ascx.vb" Inherits="controls_control_2" %>
<p>Control 2</p>
In my child page, I have the following code to load the controls:
Option Strict On
Option Explicit On
Partial Class master_Default
Inherits System.Web.UI.Page
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
Dim controlInstance1 As System.Web.UI.Control = LoadControl("~/controls/control-1.ascx")
controlInstance1.ID = "control_1"
Dim zone As System.Web.UI.Control = Me.Master.FindControlRecursively("PH1")
zone.Controls.Add(controlInstance1)
Dim controlInstance2 As System.Web.UI.Control = LoadControl("~/controls/control-2.ascx")
controlInstance2.ID = "control_2"
Dim zone2 As System.Web.UI.Control = Me.Master.FindControlRecursively("PH2")
zone2.Controls.Add(controlInstance2)
End Sub
End Class
This loads the controls, but if I click the Test button in the listview, the page loses the data in the listview after postback.
If I change the FindControlRecursively calls to FindControlIterative, when I click the test button, the data in the listview is retained after the postback.
Anybody have any idea what the FindControlRecursively call might be doing to cause the listview to lose it's data? This only happens if control-2 is added to the page - if it is not, and control-1 is loaded using FindControlRecursively, data is retained correctly after postback.
Thanks in advance...this one is driving me nuts, and it took me a while to figure out where exactly it was breaking down.
Why don't you simply expose properties that return PH1 and PH2, because the master has the reference of them and you don't need to iterate all child controls of master:
Public ReadOnly Property Container1 As PlaceHolder
Get
Return Me.PH1
End Get
End Property
Public ReadOnly Property Container2 As PlaceHolder
Get
Return Me.PH2
End Get
End Property
You can access them:
Dim ph1 As PlaceHolder = DirectCast(Me.Master, myMaster).Container1
Dim ph2 As PlaceHolder = DirectCast(Me.Master, myMaster).Container2
Another problem is this line:
controlInstance1.ID = "control_2"
You are setting only controlInstance1's ID twice, but that doesn't cause your issue.
Your main problem is that you are adding the controls to the placeholders in Page_Init instead of Page_Load. Therefore the UserControls can't load their ViewState and the ListView is empty. Recreate them in Page_Load and it will work.
Edit: But i must admit that i don't know why your iterate extension wins over the recursive. The reference on the placeholders are the same, they shouldn't work both, weird.
summary:
it works with my properties,
putting all in page's load event handler instead init
with your iterate-extension(for whatever reasons)
It also works if you add both UserControls at last, after you've found the placeholders via FindControlRecursively.
zone.Controls.Add(controlInstance1)
zone2.Controls.Add(controlInstance2)
I'm losing motivation on this, but i'm sure you'll find the answer here. Controls.Add loads it's parent's ViewState into all childs and therefore it depends on when you add the controls, also the index of the controls in their parent controls must be the same on postback to reload the ViewState.
Your recursive extension method touches control1's ID after you've added it to PH1(while searching PH2), the iterative extension does not. I assume that this corrupts it's ViewState in Page_Init.
Conclusion Use properties instead
I figured out why I was seeing the behavior I described above. I changed the recursive function to the following:
<Extension()> _
Public Function FindControlRecursively(ByVal parentControl As System.Web.UI.Control, ByVal controlId As String) As System.Web.UI.Control
If String.IsNullOrEmpty(controlId) = True OrElse controlId = String.Empty Then
Return Nothing
End If
If parentControl.ID = controlId Then
Return parentControl
End If
If parentControl.HasControls Then
For Each c As System.Web.UI.Control In parentControl.Controls
Dim child As System.Web.UI.Control = FindControlRecursively(c, controlId)
If child IsNot Nothing Then
Return child
End If
Next
End If
Return Nothing
End Function
By adding the parentControl.HasControls check, I am preventing the function from searching the listview for child controls, and this allows the listview to load its viewstate later on in the page/control lifecycle.
Also, I tweaked my iterative function to make it more efficient and prevent it from bugging out if a control was not returned:
<Extension()> _
Public Function FindControlIteratively(ByVal parentControl As Web.UI.Control, ByVal controlId As String) As Web.UI.Control
Dim ll As New LinkedList(Of Web.UI.Control)
While parentControl IsNot Nothing
If parentControl.ID = controlId Then
Return parentControl
End If
For Each child As Web.UI.Control In parentControl.Controls
If child.ID = controlId Then
Return child
End If
If child.HasControls() Then
ll.AddLast(child)
End If
Next
If (ll.Count > 0) Then
parentControl = ll.First.Value
ll.Remove(parentControl)
Else
parentControl = Nothing
End If
End While
Return Nothing
End Function
Also, to follow up on my earlier description of the problem - I was able to reproduce the recursive function's intially weird behavior using the iterative function if I removed the If child.HasControls() Then check from the iterative function. Hope that makes sense.
In the end I am sticking with the iterative function because looping should be less expensive than recursion, though in real world scenarios the difference probably will not be noticeable.
The following links were extremely helpful to me in working this out:
http://msdn.microsoft.com/en-us/library/ms972976.aspx#viewstate_topic4
https://web.archive.org/web/20210330142645/http://www.4guysfromrolla.com/articles/092904-1.aspx
http://scottonwriting.net/sowblog/archive/2004/10/06/162995.aspx
http://scottonwriting.net/sowblog/archive/2004/10/08/162998.aspx
Extra thanks to Tim for pointing me in the right direction.