I have a "Pay" button where we are starting to accept credit card. I need to call a Javascript function from the server side click event. I tried with Response.Write as below but that does not trigger my function that is defined in the separate .js file. What else can I do?
protected void btmMakePayment_Click(object sender, EventArgs e)
{
if (user selected credit card)
{
Response.Write("<script language='javascript' type='text/javascript'>OpenPayPalDialog();</script>");
}
else
{
continue with the current server side logic
}
}
Thank You in advance.
Use ClientScriptManager.RegisterStartupScript, which registers a block of JavaScript to execute when the page loads.
You can use ScriptManager.RegisterClientScriptBlock to invoke JS event:
ScriptManager.RegisterClientScriptBlock(this, typeof(this), "JSKey", "JSFunctionName(<param>);", true);
Related
i am trying to pass a value from client side and recieve it on server side on click of hyperlink.but it should not be called everytime on page load ,currently i am trying like this but it is reloaded every time when page load.so i want it to be called one time.
on click event of hyperlink TDC_No passing value to tdc.aspx
$.each(customers, function () {
var customer = $(this);
$("td", row).eq(0).find("a").text($(this).find("TDC_NO").text());
$("td", row).eq(0).find("a").attr("href", "TDC.aspx?Id=" + $(this).find("TDC_NO").text());
});
calling at page load side
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["Id"] != null)
{
TW12HVGI();
}
}
Any idea would be appreciated.
Not exactly sure what you're aiming for here. Do you mean that you pass the value - and the page loads but then subsequent actions on that page cause the TW12 function to execute again?
If so you are probably looking for a check on IsPostBack:
protected void Page_Load(object sender, EventArgs e){
if (!IsPostBack){
//if its not a post back - check for query string and run the TW12 function
if (Request.QueryString["Id"] != null)
TW12HVGI();
}
}
https://msdn.microsoft.com/en-us/library/system.web.ui.page.ispostback(v=vs.110).aspx
If you want to receive it on server side you will need to post back it using AJAX. On click of hyperlink send an ajax call and data to be saved along with it and then you will receive what ever you sent in action specified in ajax call.
I need to give an alert to user once the specific time has reached. The thing is, it should not be done in any of button click event or some thing. Even if the user is in some other page or some other website, alert should be displayed from my application. I am struggling to work with this theme. I knew with the page load or button click event, but i am blinking with this.
Use settimeout\setinterval to make a js alert after X miliseconds:
setTimeout(function(){alert("Hello")},3000);
This will alert after 3000ms(3 seconds).
setTimeout
setInterval
You can do it on server side too:
private void Page_Load(object sender, EventArgs e)
{
System.Threading.TimerCallback callback = new TimerCallback(AlertEvent);
DateTime myDate= mydate.AddHours(1);
if (DateTime.Now < myDate)
{
var timer = new System.Threading.Timer(callback, null, myDate- DateTime.Now, TimeSpan.FromHours(24));
}
}
private void AlertEvent(object obj)
{
string script = "<script type=\"text/javascript\">alert('THIS IS ALERT FROM C# TO JS');</script>";
ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", script);
}
I am using ajax toolkit fileupload for uploading images. I would like to add an event. Such as response.redirect after last image is uploadet. How will this be possible?
Tried with OnUploadCompleteAll but without any luck
Thanks
Code:
OnUploadComplete="File_Upload"
protected void File_Upload(object sender, AjaxFileUploadEventArgs e)
{
//SAVE UPLOADED FILES
}
Use UploadCompleteAll event.
That will fire after the all the files are uploaded.
Just type OnUploadCompleteAll and press tab twice. A event handler will be put up by visual studio.
Do whatever you want in that handler
When using UploadCompleteAll, the event args type should be AjaxFileUploadCompleteAllEventArgs not AjaxFileUploadEventArgs.
protected void Upload_End(object sender, AjaxFileUploadCompleteAllEventArgs e)
{
}
Add this to your AjaxUploadControl:
OnClientUploadComplete="ClientUploadComplete"
Then this part stays as is on server side:
AjaxFileUpload1.Attributes.Add("OnClientUploadComplete", "Return OnClientUploadComplete");
Last part is to add this to your client side scripts:
function ClientUploadComplete() {
var f = $(".filename").html();
window.location.replace("upload.aspx?filename=" + f);
}
I've got this button
<asp:Button runat="server" ID="btnReviewDocs" CssClass="btnReviewDocs" data-theme="b"
Text="Review Documents" OnClick="btnReviewDocs_Click" OnClientClick="clickHyperlink();"/>
And in 'OnClick' event I'm assembling an URL that I need to set to asp:Hyperlink and at the end of the 'OnClick' I'm setting this URL to the 'NavigateURL' propery of the 'asp:Hyperlink'. Once the 'asp:Hyperlink' has the correct URL I need to call the 'clickHyperlink()' function.
function clickHyperlink() {
var href = $('#hlnkID').attr('href');
if (typeof href !== "undefined") {
$.mobile.showPageLoadingMsg();
window.location.href = href;
}
}
But the 'OnClientClick' event is executed always before the 'OnClick'. Any suggestions for a workaround?
I'm doing all this stuff, because I've got problems with JQuery Mobile and 'Response.Redirect(url);' is changing the page, but not the URL.
I believe that you don't really need to involve the Hyperlink control in the JS part.
Modify your JS function and remove the OnClientClick attribute from the btnReviewDocs button:
<script type="text/javascript">
function clickHyperlink(href) {
$.mobile.showPageLoadingMsg();
window.location.href = href;
}
</script>
On the server, in the btnReviewDocs_Click method:
protected void btnReviewDocs_Click(object sender, EventArgs e)
{
// TODO: set the url, maybe append some params to the
// hlnkID.NavigateUrl value
var url = "http://stackoverflow.com/";
ClientScript.RegisterStartupScript(Page.GetType(),
"clickHyperlink",
"clickHyperlink('" + url + "');",
true);
}
Use the RegisterStartupScript in the ClientScript object to run the code after postback--->
protected void btn_Click(object sender, EventArgs e)
{
//some code
this.ClientScript.RegisterStartupScript(this.GetType(), "clintClick", "clickHyperlink", true);
}
try this
protected void btnReviewDocs_Click(object sender, EventArgs e)
{
//something doing here
Page.ClientScript.RegisterStartupScript(this.GetType(), "test", "<script type='text/javascript'>clickHyperlink()</script>");//call javascript function
}
The answer is mentioned by #Alex Filipovici.
But first you should ask yourself do you really need to go back to the client side to do a redirect ?
Why not call :
Response.Redirect("MyURL");
I my application i am using ajax(Updatepanel).I am using the following code to show confirmation dialog box after finishing some update process in database. but it is not working for me.
Problem:
Confirmation box is not displaying.
code:
protected void imbtnUpdate_Click(object sender, ImageClickEventArgs e)
{
// Database process
string javaScript = "<script language=JavaScript>\n " + "if(confirm('Do you want to update
the files?'))window.location.href = \"Upload.aspx?ID=" + ID +
"&pt=Gm&page=Gms\"; else return false;\n" + "</script>";
RegisterStartupScript("imbtnUpdate_Click", javaScript);
}
this code is just register on the page the javascript that say... if(... bla bla bla)...
Where is the function ?
The RegisterStartupScript, is not going to set on imbtnUpdateClick, this code, and is not going to call it on Clicking the Update.
Also you must always return false, from the code I see.
Advice: You the view page source code, to see the generated html and see what this code do and then you understand whats the problem.
Try this in your code behind:
imbtnUpdate.Attributes.Add("onclick", "return ConfirmUpdate();");
Then put your script into a javascript function called ConfrimUpdate()
Are you using AJAX?
Because if your button is in an update panel, and if you are trying to add this startup script on a partial-postback you should register it by using ScriptManager.
protected void imbtnUpdate_Click(object sender, ImageClickEventArgs e)
{
// Database process
string javaScript = "<script language=JavaScript>\n " + "if(confirm('Do you want to update
the files?'))window.location.href = \"Upload.aspx?ID=" + ID +
"&pt=Gm&page=Gms\"; else return false;\n" + "</script>";
// RegisterStartupScript("imbtnUpdate_Click", javaScript);
ScriptManager.RegisterStartupScript(Page, Page.GetType(),"imbtnUpdate_Click", javaScript , true);
}