creating and running a client script from asp.net - c#

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);
}

Related

set the asp:textbox val() in jQuery and get the Text of the same on the server side code

I have successfully set the text of asp:textbox using jQuery val() function, now I want the same value of the textbox on click of asp:button on the server side code.
$("#textboxId").val('some text');
protected void button_Click(object sender, EventArgs e)
{
// getTheText is blank
string getTheText = textboxId.Text.Trim();
}
<script type="text/javascript">
$(document).ready(function () {
$('#<%= TextBox1.ClientID %>').val("my value");
});
</script>
and in code behind on button click use
protected void Button1_Click(object sender, EventArgs e)
{
var value = TextBox1.Text;
}
this will work. it work for me i test it.
I also had the same problem and finally found a solution.
string getheText =Page.Request.Form["textboxId"].ToString().Trim();
But be careful if you use "Content" in master page the id must be like that
string gettheText = Page.Request.Form["ctl00$ContentPlaceHolder1$textboxId"].ToString().Trim();
if your textbox is an aspx server control then you can directly set Text by using
textboxId.Text = "Some Value";

How to call onclientclick after onclick

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");

conditionally call JavaScript function

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);

Registering Javascript function that manipulates radwindow from ajaxified radgrid

I'm trying to open a radwindow client side with some registered script.
Question 1: Is there a reason radwindow wouldn't be found if executed on startup, if so why and how do I fix it?
Question 2: Whenever I ajaxify the radgrid the event no longer fires. This makes sense, as there is not a postback occurring, so the page never starts up. How do I get registered scripts to execute in an ajax enviornment?
---- Relevant Code Behind ----
protected void RadgridProjects_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
{
if (e.CommandName == "Member")
{
Session["ProjectId"] = (e.Item as GridDataItem).GetDataKeyValue("ProjectId").ToString();
radGridProjectMembers.Rebind(); //Not the same grid!
ClientScriptManager cs = Page.ClientScript;
string js = "<script type='text/javascript'>ShowWindow()</script>";
cs.RegisterStartupScript(this.GetType(), "showwindow", js);
}
}
---- Javascript Function ----
function ShowWindow()
{
alert("code fired");
var radWin = $find("<%= RadWindow1.ClientID %>");
radWin.show();
radWin.moveTo(650, 450);
radWin.set_width(500);
radWin.set_height(400);
}
The window never opens, but my testing alert does fire. It should be noted that when I use a client event for the script the window DOES open.

Confirm function in codebehind

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);
}

Categories

Resources