How to print from aspx page without opening print dialog box - c#

I want to print from aspx page (body part {}). But I don't want to open print dialog box. i have already installed more then one printer are in my computer. But when i click on print button its directly print in my default printer. How can i do it or is it possible to do it in web application?

Print is handled by the browser, not your ASPX page. So you can't do this.

You can't do that for all browsers. Printing is client side, and you can't pass the print dialog.
There is a old script, but it doesn't work anythere exept IE and Netscape. And it is very old:
function printit() {
if ((navigator.appName == "Netscape")) {
window.print() ;
} else {
var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
WebBrowser1.ExecWB(6, -1); WebBrowser1.outerHTML = "";
}
}

yes you can, but you'll need to use a third party class\assembly that does it like
pdf.sharp
or just write one yourself...

add iframe
<iframe id="ifmcontentstoprint" style="height: 0px; width: 0px; position: absolute"></iframe>
and use follwing javascript function
function printform() {
var content = document.getElementById('<%= PrintDivID.ClientID %>').innerHTML;
var pri = document.getElementById("ifmcontentstoprint").contentWindow;
pri.document.open();
pri.document.write(content);
pri.document.close();
pri.focus();
pri.print();
}

Related

Jquery Modal Popup not showing embedded pdf file

I have this issue that I am trying to resolve with modal popup. When I call the popup from the code behind. The link does not show the embedded pdf file, just the blank embedded black screen.
However when I take the embed tag outside of the modal popup and show it based on the response from the code behind, the pdf file shows on the screen. So I know the path is right. I have tried calling the modal popup in the code behind after the pdffile.src but no change in the response.
<script>
function ShowPopup() {
$(function () {
$("#displaypdf").dialog({
modal: true,
height : 800,
width : 800,
});
});
return false;
};
</script>
<div id="displaypdf" style="display:none">
<embed id="pdffile" class="pdfsource" runat="server" />
</div>
protected void SelectButton_Click(object sender, EventArgs e)
{
ClientScript.RegisterStartupScript(this.GetType(), "Popup", "ShowPopup();", true);
string DirPath = Request.QueryString["Dir"];
Button btnButton = sender as Button;
GridViewRow gvRow = (GridViewRow)btnButton.NamingContainer;
Label SelectLink = (Label)gvRow.FindControl("SelectLink");
pdffile.Src = "file:Z:/testdirectory/" + DirPath + "/" + SelectLink.Text;
}
Based on your code, I see that you want the browser to load a local resource (file:Z:...), which is not allowed in most browsers due to the security risk it exposes.
To confirm if that's the problem, try changing:
pdffile.Src = "file:Z:/testdirectory/" + DirPath + "/" + SelectLink.Text;
to
pdffile.Src = "http://www.africau.edu/images/default/sample.pdf";
and see if the document loads (if not, check your browser console for any errors).
I recommend you to serve those documents from a http endpoint, like setting up a virtual folder in IIS pointing it to your local network path (e.g. Z:/testdirectory/) where the files will be located, then set pdffile.Src to this virtual directory file path instead.

How to make a label blink in asp.net in codebehind

I would like for a label to blink or flash in my ASP.NET C# codebehind. Can someone please tell me how I can do that? Here is the code that I have tried:
label1.Attributes.Add("style", "text-decoration:blink");
Using this the label never blinks or flashes. I want to use C# in the codebehind, not JavaScript or HTML.
The "blink" setting for text-decoration has been depreciated, and browsers are not required to support it. Internet Explorer and Chrome to not. Not sure about the others.
Maybe there is some neat jQuery, i don't know. I have used this javascript in my last ASP.NET project:
var g_blinkTime = 400;
var g_blinkCounter = 0;
var g_maxBlinkCount=4;
var g_currentBlinkCount=0;
function blinkElement(elementId){
var blinkingElement=document.getElementById(elementId);
if(blinkingElement != null){
if(g_currentBlinkCount==g_maxBlinkCount){
g_currentBlinkCount=0;
blinkingElement.style.visibility = 'visible';
return;
}
if ( (g_blinkCounter % 2) == 0 ){
blinkingElement.style.visibility = 'visible';
}else{
blinkingElement.style.visibility = 'hidden';
}
if ( g_blinkCounter < 1 ){
g_blinkCounter++;
}
else{
g_blinkCounter--
}
window.setTimeout('blinkElement(\"' + elementId + '\")', g_blinkTime);
g_currentBlinkCount++;
}
}
Since that was an ASP.NET-Ajax app i've used this to register the script for a label:
AjaxControlToolkit.ToolkitScriptManager.RegisterStartupScript(UpdFilterPanel, typeof(string), "GrdChargeFilterInfoBlink", "blinkElement('" & LblInfo.ClientID & "');", True);
But you can also use ClientScriptManager.RegisterStartupScript instead without Ajax.
Edit according to your update: "I want to use C# in the codebehind, no JavaScript or HTML"
Blinking controls is done by the client's browser, so the server is not responsible for it. As mentione by James you cannot use text-decoration:blink since that is not supported by IE and others. So i don't know a different approach than javascript.

Asp:FileUpload edit "No file selected" message

I just need to know if there's a way to change the message shown by the Asp:FileUpload when no file as been selected.
Thanks.
http://jsfiddle.net/ZDgRG/
See above link. I use css to hide the default text and use a label to show what I want:
html
<div>
<input type='file' title="Choose a video please" id="aa" onchange="pressed()">
<label id="fileLabel">Choose file</label>
</div>
css
input[type=file]{
width:90px;
color:transparent;
}
javascript
window.pressed = function(){
var a = document.getElementById('aa');
if(a.value == "")
{
fileLabel.innerHTML = "Choose file";
}
else
{
var theSplit = a.value.split('\\');
fileLabel.innerHTML = theSplit[theSplit.length-1];
}
};
You replace the text with your own message using CSS pseduo-class :after. You can declare a class like this one:
.bar:after {
content:"Please select a file";
background-color:white;
}
And assign it to your FileUpload control. The content message will replace the original one. Of course upon file selection you need to remove the message, you can do this for example via jQuery .removeClass (assuming ID of your FileUpload is "foo"):
$('#foo').change(function() {
$(this).removeClass("bar");
})
Demo: http://jsfiddle.net/5zhuL/2/
Note that this solution seems to work Webkit-browser's only (Chrome, Opera, Safari) you may need an alternative for others.
Nope, not possible. This is the standard rendering in Chrome and cannot be changed.
If you visit your page source in your browser ASP.NET put an input element with type='file' instead of your FileUpload.
You could simply use CSS to cover the text with something when the value attribute is empty.
Only add this CSS to your code. It simply hide the "No file chosen".
<style>
input[type=file]
{
width:90px;
color:transparent;
}
</style>
http://jsfiddle.net/Lsgbx5ne/1/
input[type=file]{
color:transparent;
}
input[type=file]:after {
color: #000;
content:" Cool!";
}
Improvements over other solutions:
Works for short text
Doesn't change the background color
Pure css
This work in all browser
window.pressed = function(){
var a = document.getElementById('aa');
if(!(a.value == ""))
{
var theSplit = a.value.split('\\');
fileLabel.innerHTML = theSplit[theSplit.length-1];
}
};
input[type=file]{
width:90px;
color:transparent;
}
<div>
<input type='file' title="Choose a video please" id="aa" onchange="pressed()">
<label id="fileLabel">Choose file</label>
</div>

Click Javascript button with C# webbrowser

I need to make a program click a javascript button for me in the webbrowser. Is this possible in any way? I want to accomplish this in C#
the button
INPUT id=str class=text style="TEXT-ALIGN: center" maxLength=4 size=3 value=558 INPUT onclick=doIt_new(1); id=strBtn type=button value=doIt
Yes its the default webbrowser object in visual studio
If you're using WinForm WebBrowser control - take a look at webBrowser.Document.InvokeScript method. It allows to execute JavaScript inside of a document, hosted in WebBroswer control.
string html =
#"<html>
<script>
function doIt_new(x){
alert(x);
}
</script>
</html>";
webBrowser1.DocumentText = html;
webBrowser1.DocumentCompleted += (s, e) =>
{
webBrowser1.Document.InvokeScript("doIt_new", new object[] { 1 });
};

How to change the color of a disabled html control in Internet Explorer

input[disabled='disabled']
{
background-color:#FFFBF0;
color: #28B51D;
}
I am using the following code, but it doesn't work in IE.
It works in the rest of the Browsers.
Since you tagged your question as javascript, here is my advice for IE : include an ie-only script with an ie-triggering html comments, that adds a ie-disabled class to every disabled input. If the status of inputs can change after the initial page load, add a timed observer to your page that sets the class properly.
input[disabled], input.ie-disabled
{
background-color:#FFFBF0;
color: #28B51D;
}
javascript file, included with conditional comment:
function checkDisabled() {
var inputs = document.getElementsByTagName('INPUT');
for(var i=0, l=inputs.length; i<l; i++) {
if(inputs[i].disabled) {
if(inputs[i].className.indexOf('ie-disabled')==-1)
inputs[i].className = inputs[i].className+' ie-disabled';
} else {
inputs[i].className = inputs[i].className.replace('ie-disabled', '');
}
}
}
setInterval(checkDisabled, 1000); // check every second
Here is a test (for IE). Note that the color css attribute is ignored by IE for disabled inputs. If you really need a green text, use readonly instead of disabled.

Categories