I want to replace some string in html page with c# on webbrowser component. For example there is some text like "Welcome Page" which is loading that page by ajax.
I Want to vanish that and reload, I tried to;
webbrowser1.document.body.innerhtml = webbrowser1.document.body.innerhtml.replace("Welcome Page","");
When I look for the string, "Welcome Page", It disappeared. So it works fine. But,
All the javascript functions stopped in the page. Page became a static page.
So thats the problem,
How can I change Any string in the page witout effecting any features of page?
EDİT:
I tried # Mehran Hatami's solution. And it works fine. Now there is no javascript or other problems.
I solved with
foreach (HtmlElement link in webBrowser1.Document.GetElementsByTagName("span"))
{
if (link.GetAttribute("id").ToString() == "messegetext")
{
MessageBox.Show(link.InnerHtml);
if (link.InnerHtml.IndexOf("this is message which posted by ajax") > -1)
{
link.InnerHtml = "any new text to replaced I want :)";
}
}
}
Related
I've got a aspx page that dynamically renders social media sharing links.
The code looks something like this.
System.Web.UI.HtmlControls.HtmlGenericControl newDiv =
new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
newDiv.ID = "customFB1";
newDiv.InnerHtml = "<div class='fb-share-button' data-href='http://www.website.com/folder/"+custNumber+".html' data-layout='button_count' data-mobile-iframe='true'><a class='fb-xfbml-parse-ignore' target='_blank' href='https://www.facebook.com/sharer/sharer.php?u=http://www.website.com/folder/"+custNumber+".html&src=sdkpreparse'>Share</a></div>";
customFB.Controls.Add(newDiv);
Now the source works perfectly. Unfortunately 80% of the time when the page loads I have to hit F5 (hard refresh) to actually render the html. When I expect the source when its not render, its there, but won't display.
Any thoughts on how I can get the control to render without hitting F5. My customers won't know to hit F5 hah.
Thanks,
I have problems with printed html pages with mshtml.dll. My pages contains links to bootstrap css. In browser (IE, FF, Chrome) all looks fine and prints also fine. When I trying to print page with MSHTML something goes wrong, and page looks like css does not work. I am loading html content from disk, and then print it:
var htmlContent = File.ReadAllText(filePath);
var htmlDocument = new HTMLDocument() as IHTMLDocument2;
htmlDocument.write(htmlContent);
htmlDocument.execCommand("Print", true, 0);
htmlDocument.styleSheets; // contains all linked css files
Does anyone experience such problem? Maybe there are other methods to print html page without browser?
I have a fairly simple page with a set of jQuery tabs, the content of some is called via ajax. I also have a search box in the masterpage in my header.
When I open the tabbed page the search box works fine. However once I have clicked on one of the ajax tabs the search box fails to work with an "Invalid Viewstate" yellow screen of death.
I believe this is because the ajax page is replacing the __VIEWSTATE hidden input with its own.
How can I stop this behaviour?
UPDATE: I have noticed that the YSOD only appears in IE and Chrome, Firefox doesn't seem to have the same issue. Although how the browser influences the ViewState, I'm not sure.
UPDATE: I've put a cut down version of the site that shows the issue here: http://dropbox.com/s/7wqgjqqdorgp958/stackoverflow.zip
The reason of such behavior is that you getting content of the ajaxTab.aspx page asynchronously and paste it into another aspx page. So you getting two instances of hidden fields with __VIEWSTATE name and when page posted back to server theirs values are mixing (might depends on how browser process multiple controls with same name on submit). To resolve this you can put second tab's content into a frame:
<div id="tabs">
<ul>
<li>Default Tab</li>
<li>ajax Content</li>
</ul>
<div id="tabs-1">
<p>
To replicate the error:
<ul>
<li>First use the search box top right to search to prove that code is ok</li>
<li>Then click the second ajax tab, and search again.</li>
<li>N.B. Chrome / IE give a state error, Firefox does not</li>
</ul>
</p>
</div>
<iframe id="tabs-2" src="ajaxTab.aspx" style="width:100%;" ></iframe>
</div>
Also, I'm not sure but this seems like error in the Web_UserControls_search control. In my opinion, NavBarSearchItemNoSearchItem_OnClick method must be refactored as below:
protected void NavBarSearchItemNoSearchItem_OnClick(object sender, EventArgs e)
{
var searchFieldTbx = NavBarSearchItemNo;
var navBarSearchCatHiddenField = NavBarSearchCatHiddenField;
var term = searchFieldTbx != null ? searchFieldTbx.Text : "";
if (term.Length > 0) //There is actually something in the input box we can work with
{
//Response.Redirect(Url.GetUrl("SearchResults", term));
Response.Redirect(ResolveClientUrl("~/Web/SearchResults.aspx?term=" + term + "&cat=" + navBarSearchCatHiddenField.Value));
}
}
Draw attention that we resolving client url when redirecting to search results page and instead of navBarSearchCatHiddenField use navBarSearchCatHiddenField.Value as cat parameter.
I guess that you use AJAX to fill the content of the tab. So in this case, content of your tab will be replaced by the new one from ajax and certainly _VIEWSTATE will be replaced. At server, do you use data from ViewState? In the "static tabs", you should prevent them auto reload by using cache:true
Your issue is that with your ajax call you bring in a complete ASPX page. Including the Form tag and its Viewstate. If you remove the Form tag from ajaxTab.aspx you will see everything works fine. asp.net does not know how to handle two Form tags in one page. Same goes for hidden Viewstate fields. You cannot bring in a full aspx page via ajax. Just bring in the content Div you want to display and you`ll be good to go.
I have an website with some quotes(Platon,Aristotel etc..).All of this quotes are displayed on one site, they get pulled from database.I have a method that automaticly creates Like Button for each of these quotes when web site gets loaded:
System.Web.UI.HtmlControls.HtmlContainerControl fbIframe = new System.Web.UI.HtmlControls.HtmlGenericControl("iframe");
fbIframe.Attributes["scrolling"] = "no";
fbIframe.Attributes["frameborder"] = "0";
fbIframe.Attributes["style"] = "border:none; overflow:hidden; width:250px; height:21px;";
fbIframe.Attributes["allowTransparency"] = "true";
fbIframe.Attributes["src"] = "//www.facebook.com/plugins/like.php?href=" + HttpUtility.UrlEncode("http://www.example.com/like/" + Quote[0]) +
"&send=false&layout=button_count&width=250&show_faces=false&action=like&colorscheme=light&font&height=21";
How does it work: When someone likes URL for example http://www.example.com/like/4 Facebook externalhit crawles this link and my page(like.aspx routed to like/{ID}) creates Title and Meta decription dinamicly, it pulls quote from db and set it as meta description.. if it's not external hit user gets redirected to correct quote on my main page where all of them are displayed.
The problem is that I have 30-40 of this on page and site loads so slow. Is there some script that would allow me this kind of freedom to be able to change href of like url but to load like buttons asynchronously after page is loaded?
Please go to detail as much as you can, because I never did any of Fb programming.
Thanks
i have made a page in asp.net, i have a costing calculator which has more than 50 fields, dependent on each other, one is the result of previous two and like that, i want my page to be printed in a well manner, and the header of the page which is in master page should not be in print, also the color schemes i want to adjust, let me know the best solution for this which .net provides
Put the content inside <div id="divid">YOUR CONTENT NEEDS TO BE PRINTED</div>
Then call the javascript function on button click which will print the selected area or only html of div. pass the id of div on calling javascript function.
function CallPrint(var strid)
{
var prtContent = document.getElementById(strid);
var WinPrint = window.open('','','letf=10,top=10,width="450",height="250",toolbar=1,scrollbars=1,status=0');
WinPrint.document.write("<html><head><LINK rel=\"stylesheet\" type\"text/css\" href=\"css/print.css\" media=\"print\"><LINK rel=\"stylesheet\" type\"text/css\" href=\"css/print.css\" media=\"screen\"></head><body>");
WinPrint.document.write(prtContent.innerHTML);
WinPrint.document.write("</body></html>");
WinPrint.document.close();
WinPrint.focus();
WinPrint.print();
WinPrint.close();
return false;
}
Call CallPrint('DivGrid');" on onclick() of button or use below: but.Attributes.Add("OnClick", "return CallPrint('DivGrid');");
Nothing to do with .Net and everything to do with a print stylesheet. You can create a stylesheet which will only work for when the page is printed. And you can change everything from what displays to postion to colours.
Use:
<LINK rel="stylesheet" type"text/css" href="print.css" media="print">
Note media="print" means it'll be used for printed pages.
I know that question has been asked a long time ago, however there is no accepted suggestion. So here my approach for friendly print version when using Master page.
Create an empty master page (PrintVersion.Master) to serv as print version. Add what ever you need to be print (like logos) if anything to that master page.
from your content page, add a print link with target blank. Make the href to load the current page. From the href add a querystring so you can capture it from your content page preinit event.
From your content page preinit event detect if the querystring to print exists, then specify the blank master page like: MasterPageFile = "~/Ful/Path/of/your/PrintVersion.Master"
Optional, on the PrintVersion.Master on document.ready call: window.print(); The browser print dialog will automatically open.
You can make a new printable page version, which doesn't include a header. This version can also have the layout you need.