c# HtmlGenericControl not rendering - c#

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,

Related

ASPX page to render image suddenly not working in Firefox. Works in Chrome/IE

I have an ASPX page used to pull an image from the database and write the bytes. I have used this method in the past and it has worked just fine. The basic rendering code is as follows:
GetBannerImage.aspx.cs
Response.Clear();
Response.ContentType = "image/png";
Response.AddHeader("Content-Length", Convert.ToString(banner.Image.Length));
Response.BinaryWrite(banner.Image);
Response.End();
On another page, Default.aspx, I spit out some HTML to an ASP literal, as follows:
this.ltlImage.Text = "<img src='" + VirtualPathUtility.ToAbsolute("~/GetBannerImage.aspx?banner_id=" + banner.Id) + "' />";
I have a break point in the Page_Load event of my GetBannerImage.aspx page.
When I view the page source (in Firefox) of Default.aspx, I can click on the src attribute of the image which links to my GetBannerImage.aspx page, hits the breakpoint, and spits out the image. However, there is no image rendered to the screen on the Default.aspx page, and the break point isn't being hit when Default.aspx loads.
In IE and Chrome I am not having this issue - the image loads fine. I am confident that this is not an issue with my rendering code, and I am positive that the src tag is valid. I do not recall updating Firefox recently, but it appears that this is a new issue. Anyone have any suggestions?
You can try to disable all your FireFox extensions. Maybe something in there is blocking the image.
As #Xm7x suggested, I tried toggling some of my extensions on and off and found out that Ad-Block Plus was preventing the image from rendering. Disabling the extension seemed to allow the image to render fine.

How to change webbrowser html content without effecting page?

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 :)";
}
}
}

Get HTML text from page after scrolling

I am trying to capture HTML text from a page, in the case of this website more info is loaded dynamically as you scroll. So it is easy enough to capture the first page of text, but have do I get the next. I have the auto scroll part worked out fine like this:
string Java = "javascript:window.scroll(0,";//Builds up logic so screen will scroll by one page each time
string JavaScroll = Java + Scroll + ");";
webBrowser1.Navigate(JavaScroll);
Scroll += 3050;
String Morefriends = webBrowser1.DocumentText;
The problem is, even when I scroll, the Morefriends HTML text still remains the same as the first page, like it is not updating to reflect the HTML text from the new page that was just scrolled to.

Invalid ViewState when using jQuery tabs

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.

Like button javascript loading

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

Categories

Resources