Like button javascript loading - c#

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

Related

HTMLAgilityPack set input into text field, then scrape results? C#

What I am trying to do is making it so that I can start my program, and in a textbox, I enter something I would like to search. This then loads a a web page i have defined, finds the search field on the web page, and takes the string from that textbox and enters it into the search field. From there, it hits the "search" button on the webpage, or simulates the enter button. Then, it loads a new web page, which lets say contains a specific xPath that i have set the program to search for. It then pulls the string from that xPath, and loads it into a datagridview.
Now, I have already figured out how I scrape results from 1 web page. (ie, if i just load the specific web page and scrape the xPath.)
The issue, however, is that i want to automatically search for different results, and pull the strings from each web page that has my results.
(IE: Searching for 2+2, it returns 4. Searching for 1+2, it returns 3. but "4" and "3" are on separate webpages. so instead of copying each link to a webpage, having the tool take care of that for me.)
This is what I have so far. This doesn't give me any errors, but I am at a loss trying to figure out how one would hit search or press enter.
I prefer the pressing enter method, if possible.
HtmlAgilityPack.HtmlWeb web = new HtmlAgilityPack.HtmlWeb();
HtmlAgilityPack.HtmlDocument doc = web.Load(urlField.Text);
HtmlNode node = doc.DocumentNode.SelectNodes(pathSfield.Text).Firs‌​t();
if (node != null)
{
node.SetAttributeValue(null, searchFpath.Text);
}

Open a URL and pre-fill a textbox in an ASPX page from code-behind

So what I'm doing is a little different than this:
Access a control's property in an aspx page from another aspx page
because what I'm trying to do is "push" a value forward to the next page rather than "pull" it from the previous page.
What I need to do is open a new page from within an Intranet website application (actually, the page will have been previously used by the user) and pre-fill a textbox.
Can this be done? I'm currently forwarding to the new page using:
String SUrl = "frmAuditSearch.aspx";
Server.Transfer(SUrl, true);
What I also need to do is fill a textbox called txtAuditID on frmAuditSearch.aspx with the AuditID from the current page I'm on.
And yes, I know I can use something like:
String SUrl = "frmAuditSearch.aspx?AuditID = '" + MyAuditID + "'";
Server.Transfer(SUrl, true);
but I'm trying to avoid that because most of the time there's no AuditID and that'd require tinkering with too much code.
Sounds like you'd want to fill the text box with something like this.
txtAuditID.Text = (Request["AuditID"] ?? String.Empty).ToString().
This allows the absence of the parameter if needed

Open URL from JSON via C# and ASP.Net

I have variable URL strings that are read from an external JSON block into the C# code behind.
I am then creating clickable buttons in a table which need to open a new window and launch those URLs. These are held in object's String variables.
However, I cannot find a way to make a function on the aspx side that opens a window on click and uses the URL string.
Currently I am adding a attribute to the button
Button b = new Button();
b.Attributes.Add("onClick", "OpenURL()");
bCell.Controls.Add(b);
With this I can open a window, but I can't seem to get the URL I deserialized from the JSON string over to the OpenURL()
function OpenURL(url) {var x = window.open(url, 'mynewwin');
function on the front end.
Since the url varies, I cannot hard code it anywhere.
All of the buttons, rows, and cells are generated dynamically from the JSON strings. So no hard coding can happen on these.
//First time poster. Tried to look for solutions but failed
If you know what the url is at the time you're creating the button, you can do:
Button b = new Button();
var url = "some url";
b.Attributes.Add("onClick", string.Format("OpenURL({0})",url));
bCell.Controls.Add(b);
If you don't know the url until after the page is loaded, you can store it in a variable on the page and retrieve it when you click the link.
<script>
var url;
//have whatever you use to set the url call this function
function setUrl(inputUrl){
url = inputUrl;
}
function OpenURL(){
var x = window.open(url,'some window');
}
</script>

How to add a querystring after url when asp.net button is clicked

I have a asp.net page that has a button to click. When click on it, I wish to have a querystring such as ?id=1 added after the normal url. How can I do that from server side c# code?
Three ways... server-side redirect, LinkButton, and client-side button or link.
You can have your button event handler redirect to a location with a querystring...
Response.Redirect("myPage.aspx?id=" + myId.toString(), true);
You can render the button as a LinkButton and set the URL...
LinkButton myLinkButton = new LinkButton("myPage.aspx?id=" + myId.toString(), true);
Or, you can render the button as a client side link - this is what I do when using Repeater controls...
<a href='myPage.aspx?id=<%# Eval("myID") %>'>Link</a>
I prefer the last method, particularly when I need a whole bunch of links.
BTW, this is application of KISS - all you need is a regular old link, you don't need to jump through server-side hoops to create a link with a querystring in it. Using regular client-side HTML whenever possible is how to keep ASP.Net simple. I don't see enough of that technique in the wild.
I realize this is an old question, but I had the exact same problem when we wanted to create a new URL string so Google Analytics could "count" when a form was submitted.
I was using an ASP:Button to submit the form and saving to a database and displaying a thank-you message on postback. Therefore, the URL pre- and post-submit was the same, and I had to modify it in some way.
I used the following code and it worked for me:
C#, in Page_Load:
btnSubmit.PostBackUrl = "Page.aspx?id=" + id.ToString() + "&newquerystring=newvalue";
where Page.aspx is the page with the button that I want to postback to (the same page), ID is a dynamic ID I'm using to grab content from our CMS, and obviously the new querystring item.
I hope this helps.
There are various way to add querystring in url. You can use following code If you want to add value on server side:
protected void Button_Click(object sender, EventArgs e)
{
Int32 id = 1;
// Or your logic to generate id
string url = String.Format("anypage.aspx?id={0}",id.ToString());
}
How to build a query string for a URL in C#?
Or you can use this code.
string url = Request.Url.GetLeftPart(UriPartial.Path);
url += (Request.QueryString.ToString() == "" ) ? "?pagenum=1" : "?" + Request.QueryString.ToString() + "&pagenum=1";

How do I get the URL to change to the correct/current page with jQuery Mobile and Reponse.Redirect?

I have an Inbox Message page that contains a list of messages sent to a person by another user. Clicking on this message will open up a thread of messages between these two people with the ability to reply to a message sent by the original sender (this page is MessageContent.aspx). This goes to another page (called MessageReply.aspx) that allows the person replying to create a new message in a textarea control with a button to "Send Message" which adds that reply to the end of the list of messages in the previous thread and does a Response.Redirect to go back to that page.
Overall I have everything working how I want to, but the only issue now is that the URL never changes when going from the MessageReply page back to the MessageDetail (one containing all the thread messages) page.
For example, the MessageDetail URL is "http://mysite/MessageContent.aspx?ThreadId=24".
Replying to a message goes to this URL: "http://mysite/MessageReply.aspx?message=26".
When the message gets sent to the Detail page and redirects to it, the URL still shows "http://mysite/MessageReply.aspx?message=26"
Here is the code I have in the MessageDetail to open the Reply page (using a HyperLink control):
string url = SPContext.Current.Site.ServerRelativeUrl + "/MessageReply.aspx";
HyperLink ReplyHyperLink = (HyperLink)e.Item.FindControl("MessageReply");
ReplyHyperLink.Attributes.Add("rel", "external");
ReplyHyperLink.NavigateUrl = QueryStringUtils.AppendParameter(url, MessageQueryString, item.Id);
Here is the code I have for the MessageReply SendMesage button:
// Send the message
var contentEditor = (HtmlTextArea)this.FindControl("ContentEditor");
client.SendMessageReply(_messageId.Value, SubjectTextBox.Text, contentEditor.InnerText);
Message message = client.Read(_messageId.Value);
// Redirect back to the MessageContent page
string url = SPContext.Current.Site.ServerRelativeUrl + "/MessageContent.aspx";
string pageUrl = QueryStringUtils.AppendParameter(url, ThreadIdQueryString, message.ThreadId);
Response.Redirect(pageUrl);
Anyone have any idea how to get the URL to change when it sends the message reply? Everything else is working fine. I greatly appreciate it :)
The most simple route for this would be turn off ajax on the form itself that is being submitted. All the assets required to build the next page should already be in cache so when your next page is bounced with the redirect, provided you've crafted each page to stand alone, the load times will not be any higher.
To turn off Ajax on a form that's giving you trouble...
<form ... ... data-ajax="false">
Then, just be sure that the next page is constructed as a standard JQM page that could be called directly with a GET request and everything should be fine.

Categories

Resources