i am using WATIN (i am using 2 days...)to get any web site's content like that:
Deault.aspx:
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="some_textbox" runat="server"> </asp:TextBox>
<asp:Button ID="submit_button" runat="server" Text="search"
onclick="submit_button_Click" />
<asp:Label ID="lblMsg" runat="server" Text="" ></asp:Label>
</div>
</form>
</body>
My Client Codes via Watin(http://watin.org/documentation/getting-started/)
private void button1_Click(object sender, EventArgs e)
{
//launch a new IE browser
using (FireFox browser = new FireFox("http://localhost:3411/Default.aspx"))
{
//now we have access to the browser object
//filling a textbox and clicking a button is as easy as
browser.TextField(Find.ByName("some_textbox")).TypeText("foobar");
browser.Button(Find.ByName("submit_button")).Click();
//we can also access the full html of the page to perform regex matches, scrapes, etc...
string fullPageSource = browser.Html;
}
}
i have 2 question:
1) i dislike to open in web browser it must be run back ground. because i will searh some data in 20-30 pages one event. PLease don't monitor me web pages every thing must run back ground
2) i am writing textbox name button name or whatelse. i don't have any idea textboxname or search button name because i will search some product in amazon.
i need some trick how to make a price comparison like http://www.pricegrabber.com/ WATIN is good idea or do you have any httprequest dll or method?
Watin is designed to drive the web browser in the same way that a user would. If you don't want to open a web browser, neither Watin nor Watir is an effective solution to what you want to do. (legality issues aside).
You'll probably want to use some other means to make HTTP requests and then parse through the returned HTML, once that is you've gotten written permission from Amazon to access their site with a data gathering tool, since doing so without the permission is against their conditions of use.
Related
Right now in my HTML I have:
<asp:FileUpload ID="fileUpload" runat="server" />
<asp:Button ID="pbcUploadBtn" runat="server" Text="Upload" onclick="uploadBtnClick" />
In my code-behind:
protected void uploadBtnClick(object sender, EventArgs e)
{
if (this.fileUpload.HasFile)
{
this.fileUpload.SaveAs(Server.MapPath("~/upload/") + this.fileUpload.FileName);
}
}
Is it possible to, on the click of the button, call javascript to lead the user to the next page (FileEditor.aspx). I need to lead the user to the next page while keeping the filename so it can operate on it with XmlDocument methods. How do I pass back the GUID generated in the uploadBtnClick() method? I'm guessing the second page will have another code-behind, so I'm not sure how I can operate on that specific file?
After you save the uploaded file, you can use Server.Transfer to load the next page in your flow without changing the URL. This will, however, still cause the page to go through the entire ASP.NET life cycle.
I have a file upload inside the update panel, and an upload button that uploads the file to the server. Is it possible to upload the file without clicking the upload button? I want to remove the upload button and upload the file as soon as the file is selected from the user's machine. Or, have a 4 second timer, then call the upload_click to check if the fileupload has a file or not. How can I do it without a button inside update panel?
<asp:UpdatePanel ID="UP_DDL" runat="server" UpdateMode="Always" ChildrenAsTriggers="true">
<ContentTemplate>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Upload" OnClick="Upload_Click" runat="server" Text="Upload" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="Upload"/>
</Triggers>
</asp:UpdatePanel>
protected void Upload_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
//create the path to save the file to
string fileName = Path.Combine(Server.MapPath("~/Bulk Upload"), FileUpload1.FileName);
//save the file to our local path
FileUpload1.SaveAs(fileName);
}
}
I am sure you can use any of the events on the HTML INPUT File element to fire up a full post back, which is all you need in order to upload the file automatically.
Google, at least on the standard interface, uses some sort of Flash plugin to accomplish what you want.
There might be some other jQuery plugins that provide this functionality out of the box. This one, for example, seems to do it.
You would need to mimic the click with some client-side code, such as using submit from Javascript - and this is not supported by at least one notable browser, I believe, IE.
Otherwise, you could use jQuery ajax calls to static web methods which do uploads on the fly, behind the scenes, when the value of the file upload control have changed.
You can hide Upload button with display:none inline style and add following code to Page_PreRender method:
FileUpload1.Attributes["onchange"] = ClientScript.GetPostBackClientHyperlink(Upload, "");
Or consider to use AsyncFileUpload control from AjaxControlToolkit
I am working on an asp.net 4.0 application. One of the site does is allow users to search for stuff in the database. The page that is done on looks like this:
<asp:Panel ID="pnlSearch" runat="server" DefaultButton="lnkSearch" cssclass="searchbar box">
<span id="searchFor"><strong>Search for</strong></span>
<asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
<asp:LinkButton ID="lnkSearch" runat="server" Text="Search" onclick="lnkSearch_Click"></asp:LinkButton>
</asp:Panel>
lnkSearch_Click performs the query on the database, then binds the results to a asp:Repeater.
This all works, but there is a problem: The URL is not changed for the user, and so there is no way for the user to save a query for later, or to link to to their friends.
I could solve this by doing something a bit like:
<script>
function doSearch() { location.href='~/defaultpro.aspx?search='+escape($('txtSearch').value);
</script>
<a ID="lnkSearch" href="javascript:void(0);" Text="Search" onclick="doSearch();" />
But this feels like going out of my way to avoid using the tools asp.net provides. Is there a better way to do this?
The best way of doing it is to have a form with method set to get:
<form method="get">
But ASP.NET requires the one huge form that wraps all the controls, so its not an option for you.
Other way you can implement this, is to create an event handler for click event of your Search button:
protected void SearchButton_Click(object sender, EventArgs e){
Response.Redirect(Request.RawUrl + "?search=" + Server.UrlEncode(txtSearch.Text));
}
And bind all the data in the PageLoad event
If you are using .Net 4.0 why are you not using URL Routing?
I wrote this article explaining how to use the new feature.
So you would have a click event that performs your search and then at the end of the search (logic code) you can keep their search in the URL by using URL Routing.
Another method you could use is Sessions. I would not suggest cookies as they are on the client and prone to manipulation. One last option you could use is Context.
If you are going from one page directly to the next you could use page history.
I'm learning C# and working on my first ASP.Net e-commerce website.
I have the following repeater:
<asp:Repeater ID="Cartridges" runat="server" OnItemCommand="Cartridges_ItemCommand">
<ItemTemplate>
<asp:LinkButton ID="buy" runat="server" CommandName="AddtoCart" CommandArgument=<%#Eval("cartID") %> Text="Buy"></asp:LinkButton>
</ItemTemplate>
However, when the code is rendered the repeater produces JavaScript to postback which is no good when populating a shopping cart if the user has JavaScript turned off!
<div class="wrap-cart">
<div class="cartbuy"><a id="ContentPlaceHolder1_Cartridges_buy_0" href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$Cartridges$ctl00$buy','')">Buy</a></div>
<div class="cartimg"><img src="pic/epson-comp-set50.jpg" alt="product" /></div>
<div class="carttext">
<p class="cartdesc"><span class="homeprinters">product</span></p>
<p class="cartprice">£x.xx</p>
</div>
All I want to do is to pass the cartID to a data table. Is there a way round it so JavaScript is not involved at all?
For completeness here's my code behind:
protected void Cartridges_ItemCommand(object source, RepeaterCommandEventArgs e)
{
cartidtest.Text = "added";
if (e.CommandName == "AddtoCart")
{
string varCartID = (e.CommandArgument).ToString();
//more code here
}
}
Sure, you can pass the selected ItemID via QueryString
First, Generate an anchor tag like this:
AddToCart
This may be accomplished in an ItemTemplate:
<a href='<%# "NextCheckoutStep.aspx?ItemID=" + Eval("ItemID").ToString() %>'>AddToCart</a>
Finally, in NextCheckoutStep.aspx, access:
Request.QueryString["ItemID"]
Notes
You can pass information back to the same page. Just replace NextCheckoutStep.aspx with CurrentCheckoutStep.aspx. You will just need to put some routing code into your Page Load (or init) handlers (with postback = false) to react to the various page states that are created by this method. Sometimes a state diagram is helpful in working with this design.
Make sure to 100% sanitize your QueryString-Reads, because malicious users will attempt to put nasty values in the query string.
You don't have to use the QueryString, you can also get by with the HTTP POST collection; however, you need to sanitize here too, because you can never trust inputs coming from the client.
I would have a link which includes the item id in a querystring. I would expect the user's cartId to be stored in a cookie or in a session variable.
<asp:Repeater ID="Cartridges" runat="server">
<ItemTemplate>
<p>Buy</p>
</ItemTemplate>
</asp:Repeater>
A LinkButton renders as a link that immediately forces a submit, so it needs javascript.
You will have to use a plain HyperLink to render a url like mypage.aspx?id=cartid.
You can use instead of LinkButton
I have a file upload Control and I have made this invisible.
I want to enable a browse Button for that file upload control when I click another Button.
How can I do it?
First make a file up loader like this one
To upload a file you need to do 2 things
1) Select the file. (click browse button)
2) Send it to server. (click the upload button)
So first lets write a java-script to do these.`
<script type="text/javascript" >
function uploadImage() {
$('#MainContent_UploadButton').click()
}
function selectFile() {
$('#MainContent_FileUploadControl').click();
}
</script>
Now make the file upload controller upload itself as soon as a file is selected
<asp:FileUpload id="MainContent_FileUploadControl" runat="server"
onChange="uploadImage()" class="hidden"/>
Then make a new button and let it select the file as soon as it is clicked.
<asp:Button ID="MainContent_UploadButton" runat="server" Text="Upload File"
OnClientClick="selectFile(); return false;" />
The most important point is put "return false" in the onClientClick field. It will block the buttons post back and let you choose a file without refreshing the page.
Now hide the unwanted components using css and you are done !!
I think this is not possible. This would likely be a security issue if a script could upload (or at least trigger the upload process) invisible from any user interaction.
Update:
Seems that someone actually developed a solution to hide the upload control. From what I read it seems to take some effort to develop and uses JavaScript.
Personally, I wouldn't dare to guarantee that this works on all platforms (just imagine someone with a BlackBerry or Windows Phone visits your website...) and thus avoid it.
<asp:FileUpload ID="FileUpload1" runat="server" style="display:none;"/>
<input id="btnFileUpload" type="button" value="Add" runat="server" />
btnFileUpload.Attributes.Add("onclick", "document.getElementById('" + FileUpload1.ClientID + "').click();");