ASP.NET change captcha without page refresh - asp.net

I've created a simple captcha system in my assp.net web application, I use an ASPX page as ImageUrl of my captcha image, this ASPX file creates captcha image and returns it to be used as my image source. It works fine but I'm going to insert a Change Captcha button which I'd like to change my captcha WITHOUT REFRESH, I've used to methods with no success,
first I tried Ajax Update panel and inserted my change captcha button and my captcha image into it, my captcha didn't change (of course when I use a button outside of update panel to change captcha, it works fine but I have page refresh),
this is my button click code:
protected void Button1_Click(object sender, EventArgs e)
{
imgCaptcha.ImageUrl = "CreateCaptcha.aspx?New=1";
}
then I used a Jquery Ajax call (web method) to call my CaptchaImage.ASPX file and use it as my image source, but again I had no luck! what is going wrong here? in both cases my CaptchaImage.ASPX file is never called! how can I change my captcha image without page refresh?
thanks

$('#btnSearch').click(function () {
var src = //Url to image
$('#image').attr("src", src);
});
Above code changes source of image (#image) used b'coz image is the id for the imagebox.or whatever u using.

Related

Post back and browser back button issue in asp.net

I am working on web application. I have a grid that shows data to the user. When user click on the any row of the grid, it redirects user to another page, as we have a asp link control on the a column.
Issues
My code is like
if (!Page.IsPostBack)
{
//CODE
}
When user click on the BROWSER BACK button, it does not execute the CODE. Simply show the data from CACHE.
How can I execute the CODE , on browser back button click ?
It's a global problem with ASP.Net. Most of developers thinks like Windows developer. It ends with a postback for mostly any action, including navigation actions.
To solve such problems:
Avoid using button to change page. Generate an hyperlink with the target url.
Wrap parts of the page within an update panel. The benefits is that the browser won't see a page change after each postback. Then you will be able to "refresh" the page without warning dialog
When populating a grid, instead of "selecting" a row from codebehin, generate a link to the same page with "?ID=42" in the url, and bind this value to the grid as the selectedvalue
The root cause of this "evil" behavior, is that postback are issued using HTTP Post request. Then, the browser requires to repost the page to rebuild it.
Using the technics mentionned above, you are issuing GET request. This kind of request doesn't require to resubmit anything.
Try Disabling browser cache for the page that you don't want to cache.
protected void Page_Load(object sender, EventArgs e)
{
Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
Response.Cache.SetValidUntilExpires(false);
Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();
if (!Page.IsPostBack)
{
//CODE
}
}

is there any method for know on which hyperlink user has clicked in asp.net?

hi actually i want to store value that has been clicked by user on web page for instance.
suppose this is my web page content of list
**
**google.com**
**yahoo.com**
**facebook login**
**stackoverflow.com**
**
now suppose user click on facebook login
then how to know that user has clicked on facebook login actually i want to keep record for further processing.
Abatischev's suggestion will work, but there's an easier method that doesn't involve making AJAX calls if you don't want to go through the hassle.
Instead of having the link go directly to the page that you're linking to, you should have it submit to your asp.net page. You can then record the click there before redirecting to the destination page.
Client-side way:
Before finish the page render, add programmatically onclick event to each hyperlink, call on click an async JavaScript script to record url
Server-side way: (extending #Justin's answer)
<asp:LinkButton runat="server" OnClick="urlGoogle_Click">google.com</asp:LinkButton>
protected void urlGoogle_Click(object sender, EventArgs e)
{
DataBase.Record("google.com");
this.Response.Redirect("http://google.com");
}

onRowCommand gets fired again on refreshing the page

In my web page i have a gridview which contains link to download file and link to open another web site in separate page.When i click on link gridview onRowCommand gets executed and website opens in separate window.After this when i refresh the web page, gridview onRowCommand get executed again and opens web site in separate window again which i dont want.I want that when i click on link to navigate to another web site then only it should go.I mean on page refresh, gridview onRowCommand should not get executed.
I am using Page.RegisterStartupScript to open another web site in separate window.
ASP.net 2.0
thanks
-------------------------------------------code----------------
protected void grdDisplayView_onRowCommand(object sender, GridViewCommandEventArgs e)
{
string path = e.CommandArgument.ToString();
if (path.Contains("http"))
{
StringBuilder sbString = new StringBuilder("<script language='javascript'>");
sbString.Append("window.open('");
sbString.Append("http://www.yahoo.com','', 'status=no,width=600,height=500,toolbar=yes,menubar=yes,location=yes,scrollbars=yes,resizable=yes,titlebar=yes,top=198,left=305');");
sbString.Append("</script>");
if ((!Page.ClientScript.IsStartupScriptRegistered("clientScriptExportView")))
{
Page.RegisterStartupScript("clientScriptExportView",sbString.ToString());
}
}
else
{
//download file which is locally
path = Server.MapPath(path);
System.IO.FileInfo file = new System.IO.FileInfo(path);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
HttpContext.Current.Response.ContentType = "Application/octet-stream";
HttpContext.Current.Response.WriteFile(file.FullName);
HttpContext.Current.Response.End();
}
}
If the user clicks the refresh button, any information sent in the previous request will be sent again. That is to say, if the user clicks a button, and if he presses the refresh button, the button click action will be re-sent to the server.
I believe there is no easy way to distinguish between the original button click, and the button click sent via the page refresh.
This is a classic problem, and can be solved (in a not so easy way) using the Post/Redirect/Get pattern. See here (wiki) / here.
I am not sure if this will solve your problem, but it will hopefully set you out on the right track ! Hope it helps !
See this SO post that describes how to implement the pattern in ASP.NET Web Forms.
maybe this is not what you want but i should use:
<a href="new page" target="_blank" >text</a>
in the gridview itemtemplate
this makes sure that there is no code behind executed :)
but i don't know if that's what you want
you can create the link in the ondataitembound event
if you need the code behind you can also use the dataitembound event to add the javascript to for instance an imagebutton to launch a new page and have the code behind of the onclick event:
imagebutton.Attributes.Add("onclick", "window.open('otherpage.aspx?courseid=" + id+ "')")

Can you perform an action based on a seperate window in asp.net?

I have a page of search results, and clicking one opens a new window where they edit the information about it. Once they finish, they currently have to hit "search" again to refresh the results to ensure it saved correctly (it refreshes the search results and reflects the new status).
Is there any way to trigger the page to automatically refresh (ie. automatically click the search button) based on the popup window closing or through some other means?
I echo the sentiments to do this without popup windows, but if you really need to...
I would do this with an update panel wrapped around the results and some javascript added to your save button on the popup page.
On your popup page, I guess you have a button to save your work, something like:
<asp:button id="btn_save" runat="server" OnClick="Save" Text="save" />
on the codebehind:
protected void Save(object sender, EventArgs e)
{
// save code ...
// bla bla
// Add this
ClientScript.RegisterStartupScript(this.GetType(), "save", "<script language='javascript'>updateResults();</script>
}
On the results page add this
<script language="javascript">
function updateResults()
{
__doPostBack('updatePanel_results','');
}
</script>
and wrap your results control in an update panel (in this example called updatePanel_results). In your page_load, you'll probably need to code to call your refresh resutls method.
I'd really avoid having the application flow spanning two windows if possible. Why not impliment some kind of modal dialogue when the user clicks an edit link?
Check out jQuery's modal form implimentation to see what I mean:
http://jqueryui.com/demos/dialog/#modal-form
Opening window does not look like the ideal solution here.
In the new window you have access to parent window in javascript. You can use it to fire refresh/or some other function in mail window.
Is there any way to trigger the page
to automatically refresh (ie.
automatically click the search button)
based on the popup window closing or
through some other means?
You can register a JavaScript event handler for the OnUnload event. However, in my experience it's not reliable (browser-to-browser inconsistencies, etc).
Have you considered using Ajax to keep the search results on the same page as the original window? Or maybe a small Silverlight control?

Firing Postback in ASP.NET Without javascript

Is this possible?
EDIT
I want to user the asp:ListView
List item
I want to use it for editing
I dont want the postback to use Javascript when I put it into the edit mode
I've been trying to use a to do this instead of a link button but to no avail.
.
The only way to trigger a PostBack without JavaScript is by using a submit button. This is an HTML limitation, not an ASP.NET one.
I'm a little unsure of what you're trying to do without more detail. If I knew more specifically what you're trying to accomplish I could give you more details.
Like you mentioned in your post, you could use the <a> tag to send the user to the page you want.
You can add information to the link like so:
Click here for a new item
Then in the page load of newpage.aspx you can check what the action the user selected was, the query parameters are stored in the Request (Language is C#).
protected void Page_Load(object sender, EventArgs e)
{
string action = Request.Params["action"];
if(!String.IsNullOrEmpty(action))
{
switch(action)
{
case "newitem":
//handle the new item action
break;
case "deleteitem":
//handle the delete item action
break;
//handle other actions.
}
}
}
EDIT: You should be aware that the <a> tag will send the user to the page specified, the page though will act as if it is the first time the user has visited the page. With that said, the page variable IsPostBack will be false.

Resources