How to open a Pdf file in IFrame? - c#

How Can (in a list with candidates) select one pdf/docx file and open it in a Iframe?
Here is an Image of how the list of candidates looks like:

Dont know exactly where do you want to show that iframe, but this code will help you:
Insert a Literal into your aspx:
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
And this is your C#
string pathFinal = "pdf/yourPDF.pdf"
string embed = "<object data=\"{0}\" type=\"application/pdf\" width=\"100%\" height=\"800px\">";
embed += "If you are unable to view file, you can download from here</object>";
Literal1.Text = string.Format(embed, ResolveUrl(pathFinal));

Related

HTML find image for C# webBroswer from resources

I'm showing an HTML file (Documentation.html)'s content inside a webBrowser control. The HTML file is located as resource.
string htmlFile = Properties.Resources.Documentation;
webBrowser1.DocumentText = htmlFile;
There is an image inside Documentation.html which will be shown properly in any browser:
<img src="Resources/Image.png">
Unfortunately all the text of the HTML-file appears normally but not the image.
Can I set the images path to the resources (like Properties.Resources.Image) or something else?
You can embed the image's data in documentation.html file like
<img alt="Embedded Image" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIA..." />
ref:
Embedding Base64 Images
You can do it either direcly or even by code like
htmlFile .Replace("Resources/Image.png", string.concat("data:image/png;base64," + imageData))

How to get asp:hyperlink to display text instead of url

I have a panel that gets a hyperlink added dynamiclly through C# with values from an sql database.
However some of the URLs are really long and quite uneccessary to display.
I have not found any good ways to hide/disable the url showing and replace it with text. I can not use normal <a href> as it handled server side.
EDIT added some of the code.
<asp:HyperLink ID="moduleHyperlink" runat="server"></asp:HyperLink>
now in C#
HyperLink hyp = createHyperlink(btn.link);
moduleHyperlink.Controls.Add(hyp);
This will display for the user the entire btn.link (url string) which might be really long and it looks messy on the webpage. I would rather have a text saying "External Link", which when clicked redirects the user to the url.
You can add Text property to show some valid link title instead of showing the URL.
HyperLink hyp = createHyperlink(btn.link);
hyp.Text = "YourTextForTheLink";
moduleHyperlink.Controls.Add(hyp);`

How to show image in an aspx file?

I'm using this
<asp:Image ID="UserPicture" runat="server" />
to show image in the aspx file and the code behind looks like this:
string imagePath = "~/ProfilePictures/" + UsernameBox.Text;
UserPicture.ImageUrl = imagePath;
But after loading the page, the picture won't show up, only the default picture icon appears. Any suggestions how to do it properly? (Maybe resize the image?)
Thanks
User Server.MapPath() method:
string imagePath = "ProfilePictures/" + UsernameBox.Text;
Make sure UsernameBox.Text must be the xyz.jpg OR any of the file name with extension.

URL is not rendering correctly in C#

I have Hyperlink in a datalist. HTML is given below:
<asp:HyperLink ID="lnkEntry" runat="server"><%# DataBinder.Eval(Container.DataItem, "Title") %></asp:HyperLink>
i have saved the URL of a Entry in the Database, like "http://test.com/posts/New-test-in-March" And used the following way to Bind it to Hyperlink in Item_databound of Datalist.
lnkEntry.NavigateUrl = objEntry.PermaLink;
But when it renders into the browser the URL get changed to "http%3a//test.com/posts/New-test-in-March"
i have tried to use the URLDecode but it doesn't make any change in output.
lnkEntry.NavigateUrl = Server.UrlDecode(objEntry.PermaLink);
Please help me how can i fix this issue.
you should use HttpUtility, like this:
lnkEntry.NavigateUrl = HttpUtility.UrlEncode(objEntry.PermaLink);
or WebUtility if running recent versions of .net.
Check this thread for more info.

Dynamically add HTML to ASP.NET page

Could someone please advise what the "correct" method is for adding HTML content to an ASP.NET page dynamically?
I am aware of the following declarative method.
//Declaration
<%= MyMethodCall() %>
//And in the code behind.
protected String MyMethodCall()
{
return "Test Value";
}
Is there a better or best practice way?
EDIT: I am building a Galleriffic photo gallery dynamically depending on the images located in a specific folder.
Depends what you want to do.
For controls/text I normally use a LiteralControl and set the Text property as the HTML I want to add, then this control can be added anywhere on the page that you want it to appear
LiteralControl reference is
here
ok seeing as you want it for Galleriffic, I guess it would pseudo-appear as such...
LiteralControl imageGallery = new LiteralControl();
string divStart = #"<div id='thumbs'><ul class='thumbs noscript'>";
imageGallery.Text += divStart;
foreach ([image in images])
{
string imageHTML = #"<li><a class='thumb' name='optionalCustomIdentifier' ref='path/to/slide' title='your image title'>
<img src='path/to/thumbnail' alt='your image title again for graceful degradation' /></a>
<div class='caption'>[caption]<div></li>";
imageGallery.Text += imageHTML;
}
string divEnd = #"</ul></div>";
imageGallery.Text += divEnd;
this.[divOnPage].Controls.Add(imageGallery);
Aspx :
<div id="DIV1" runat="server"></div>
Code behind :
DIV1.InnerHtml = "some text";
There are several ways to do that, which to use really depends on your scenario and preference.
Web User Controls: Can be added dynamically and you get the full editor support of Visual Studio.
XML literals (VB.NET only): Very convenient way to quickly put together HTML in code.
Templates: Add a plain HTML document to your solution and include it as a resource. Then you'll get editor support and you won't clutter your code with HTML source.
Another option
//.aspx
<asp:Literal ID="myText" runat="server"></asp:Literal>
//.aspx.cs
protected Literal myText;
myText.Text = "Hello, World!";

Categories

Resources