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);
}
Related
I have a website where there are two pages, page1 contain textbox and button1,
if I cliked button1 it will open page2 which contain button2,
if I cliked button2 it will assign value from page2 to the textbox in page1
but the problem is the value will display in the textbox after I refresh page1,
and my question is how I can update textbox value directly without refresh in page1 after I click button2 in page2 like this:
here is my code:
page1 aspx.cs:
<script type="text/javascript">
function openPopup() {
window.open("page2.aspx", "_blank", "WIDTH=1080,HEIGHT=790,scrollbars=no, menubar=no,resizable=yes,directories=no,location=no");
}
<asp:button text="clik" id="button1" runat="server" onclientclick="return openPopup()" xmlns:asp="#unknown" style="margin-right:30%" />
protected void Page_Load(object sender, EventArgs e)
{
if (Session["userID"] != null)
{
txtbox.Text = HttpContext.Current.Session["userID"].ToString();
}
}
page2:
protected void button2(object sender, EventArgs e)
{
Session["userID"] = row.Cells[0].Text;
}
we can do this in following way:
create a javascript function on page 2, that refresh textbox1 on
page 1.
on button2 click from page 2, using scriptmanager, call
the function created in step 1 with require param.
So, basically you code be like:
// place this in page2.aspx
// javascript function to update text box in page 1;
function UpdateParentText(value){
if(typeof(value) != undefined){
window.opener.document.getElementById("TextBox1").value=value;
}
}
//2. code behind call to the javascript function from page2.cs
protected void button2(object sender, EventArgs e)
{
// code behind call to the javascript function.
ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "MyScript", "UpdateParentText('" + row.Cells[0].Text + "'", true);
}
If you are not using AJAX, then use this:
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "MyScript", "UpdateParentText('" + row.Cells[0].Text + "'", true);
You may use:
window.postMessage() — to send the message
window.addEventListener(“message”,callback) — to receive and process the message
See sample here.
thanks everyone I solve it by using session with javascript
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 want to create a script like this an run that :(below is attemp)
protected void txtPassword_TextChanged(object sender, EventArgs e)
{
script = "<script type='text/javascript'>";
script += "function showmsg(){";
script += "$('#msg').slideDown('normal');";
script += "$('#msg span').addClass('message');";
script += "setTimeout('showmsg',100);";
script += "}";
script += "</script>";
ClientScript.RegisterClientScriptBlock(this.GetType(), "ShowMessage",script);
}
Where is my mistake ? why there is no run ?
Presuming the script is now a part of the page, the function showmsg() needs to be called:
<script type="text/javascript">
$(function() {
showmsg();
});
</script>
since you are using jQuery, this should execute the code in the showmsg function.
There is no need to register the script this way in code. You can simply check for the change in text of the textbox using javascript/jquery.
Since you are using jquery, the change() event does not work as in standart JavaScript when detecting change in textbox text change.
Have a look at this link where something similar was already answered.
https://stackoverflow.com/a/1481155/448407
Hope this helps.
You don't need to register the script. Just do this to handle the change event on the client side:
$(document).ready(function() {
$('#<%= txtPassword.ClientID %>').change(function() {
alert('Handler for .change() called.');
});
});
You use the control's ClientID property to find it with jQuery and subscribe to change event.
Hope it helps!
It seem that you are using some jquery markup to do apply some animation, remember that with jquery you must load the Document before manipulate the DOM.
$(document).ready(function()
{
//your script
});
seems you forgot to say that your script is a string, it shoud work this way.
protected void txtPassword_TextChanged(object sender, EventArgs e)
{
string script = "<script type='text/javascript'>";
script += "function showmsg(){";
script += "$('#msg').slideDown('normal');";
script += "$('#msg span').addClass('message');";
script += "setTimeout('showmsg',100);";
script += "}";
script += "</script>";
ClientScript.RegisterClientScriptBlock(this.GetType(), "ShowMessage",script);
}
I have a simple email form written in ASP.NET with the logic in the codebehind file. It's all in C# (the logic that is...). Anyways, on page load I have the following:
protected void Page_Load(object sender, EventArgs e)
{
RequestorName.Text = Request.Form["UserName"].ToString();
RequestorTitle.Text = Request.Form["JobTitle"].ToString();
RequestorEmail.Text = Request.Form["Email"].ToString();
RequestorPhone.Text = Request.Form["Phone"].ToString();
RequestorAddress1.Text = Request.Form["Address"].ToString();
RequestorAddress2.Text = Request.Form["City"].ToString() + " " + Request.Form["State"].ToString() + ", " + Request.Form["Zip"].ToString();
}
This works great as it pulls the users information into a few fields so they don't have to fill everything out by hand.
My other 2 methods in the code behind:
protected void SubmitForm_Click(object sender, EventArgs e)
{
SendEmail();
}
protected void SendEmail()
{
try
{
//compose email and send
}
catch (Exception ex)
{
ErrorMessage.Text = "Error: " + ex.ToString();
}
}
On my form page the button code is this:
<center>
<asp:Button runat="server" Text="Submit" ID="Submit" OnClick="SubmitForm_Click" class="button red" />
</center>
The error occurs when I click the send button on the form that generates the email and sends it. I can remove the Page_Load code and works great but I'd like to keep it there so the user doesn't have to fill out as much information.
I've used my Google Fu and read a ton of threads/articles but can't seem to find the solution...
Thanks for any assistance.
Add check for IsPostBack:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
RequestorName.Text = Request.Form["UserName"].ToString();
RequestorTitle.Text = Request.Form["JobTitle"].ToString();
RequestorEmail.Text = Request.Form["Email"].ToString();
RequestorPhone.Text = Request.Form["Phone"].ToString();
RequestorAddress1.Text = Request.Form["Address"].ToString();
RequestorAddress2.Text = Request.Form["City"].ToString() + " " + Request.Form["State"].ToString() + ", " + Request.Form["Zip"].ToString();
}
}
Have you tried adding if (Page.IsPostBack == false) to your Page_Load event?
I assume that the Request.Form code comes from fields that the user has filled out, but without seeing the rest of your markup, I'm not sure why you'd have to re-assign values from the form to what appear to be other fields on the form.
Where specifically is the error occurring?
From your code, I'm assuming that you are posting to your email form from another page and passing the parameters across.
If that's the case then assuming your .Text are the page controls then you should look at containing the control fillers in an If(!IsPostback) {...} for the first loading of the page only. Then your email code can read from the local controls.
My guess is that the "Request.Form[..." items are probably the ones kicking back error on postback.
HTH
Dave
I have a web form in ASP.Net with C# code behind. It's a simple thing, and I'm pretty new so I'm kind of stuck.
In the source code of the web form I have a button called "print" that looks like this:
<asp:Button ID="btnPrint" runat="server" onclientclick="window.print();" Text="Print" />
No problem. In the code behind I have this:
protected void btnPrint_Click(object sender, EventArgs e)
{
//get current Date/Time
string dateTime = DateTime.Now.ToLongDateString() + ", at " + DateTime.Now.ToShortTimeString();
//set it to labelDate
lblDate.Text = "Requested on " + dateTime;
}
So the problem is that when I hit the print button, the form prints before the code executes and stamps the label (lblDate.Text).
Soooo... my noob question is how to get that date/time stamp to process before the form prints?
Thanks for your advice.
Mark
My first instinct would be to ditch the server side event, and populate the time stamp with javascript before the print call.
Try something like this:
protected void btnPrint_Click(object sender, EventArgs e)
{
//get current Date/Time
string dateTime = DateTime.Now.ToLongDateString() + ", at " + DateTime.Now.ToShortTimeString();
//set it to labelDate
lblDate.Text = "Requested on " + dateTime;
ScriptManager.RegisterStartupScript(this, this.GetType(), "key", "window.print();", true);
}
Instead of calling a windows.print(), call a function that adds the Requested datetime.
That will do the work.