call javascript from server - c#

my button on server works on alertMsg() how ever doesnt work on playSelected()
my button on Html works on playSelected() and alertMsg()
anyone can figure out for me why?
javascript
function playSelected() {
var a = "Video/" + document.getElementById("TextBox2").value + ".flv";
jwplayer("mediaplayer").setup({
flashplayer: "jwplayer/player.swf",
file: a,
image: "jwplayer/preview.jpg"
});
}
function alertMsg() {
alert("testing123");
}
button in html
input type="button" runat="server" value="Click me!" onclick='playSelected()'
button in server
asp:Button ID="Button2" runat="server" text="call javascript" OnClientClick="playSelected(); return true;" UseSubmitBehaviour="false"

<asp:Button runat="server"
OnClientClick='playSelected(); return true;'
UseSubmitBehaviour="false" />

Make sure your java script is allowed on the clietn browser. Also do postbacks logic in your page_load event. I would rather create the Button on overriden CreateChildControls method of the page, and on overriden OnPreRender wireup my java script and register the script with ClientScriptManager method RegisterStartupScript. So that you can have nice page life cycle control.

Related

Submit Event Not Firing on Control

I have a webpage with a control to handle all the comments so I don't have to copy the code from one page to another. When I click the button, the code to handle the submit is not happening. I want the page to have no .axd references.
This is my form statement on the .aspx page
<form id="formAlpha" method="post" runat="server" action="">
<Comm:Comm ID="comments" runat="server" />
</form>
The processing for saving the comments is in the control.aspx
In the code of the webpage, I set the action to be
formAlpha.Action = Request.RawUrl;
This is my submit button in the control.
<asp:button ID="cmdSubmit" CausesValidation="false" UseSubmitBehavior="true" text="Submit" runat="server" OnClick="cmdSubmit_Click" OnClientClick="return ValidateSubmission();" />
The JS code executes correctly, it displays an alert box and then returns true. THe page reloads but the click event doesn't work. The load event fires up again and the IsPostBack is false. Its not submitting but reloading.
I'm using WebForms C#, not MVC
Just an idea taken from the official MSDN page, remove the "return" (and maybe the semicolon too) keyword from the onClientClick declaration:
OnClientClick="ValidateSubmission()"

asp:button with no post back or refreshing the page

I have button in my page that when click on fires a jquery method that shows a div, but if the browser doesn't support javascript I want to fire a code behind method (using a postback). Is this possible? I'm already using this code but i can't see any results:
<asp:Button ID="Button1" runat="server" Text="Upload Files Here" Width="187px" onClick="CodeBehindMethod" OnClientClick="show_upload_box();return false"/>
and this is my jquery code:
function show_upload_box() {
$("#pop_up_background").css({
"opacity": "0.4"
});
$("#pop_up_background").fadeIn("slow");
$("#signup_pop_up_box").fadeIn('slow');
window.scroll(0, 0);
}
If the browser doesn't support JavaScript then your button would not work.
In ASP.NET, the runat="server" instruction is actually tied to a JavaScript method that submits a form (view page source to see that method).
Change or remove the value of that property to prevent the post back to your server.

Understanding hidden fields

I was wondering if you guys could help me understand hidden fields, since I don't think I am getting them to work.
On the aspx page I have:
<asp:HiddenField ID="hidVal" value="" runat="server" />
On a button click I have a JavaScript function called
<button type="button" id="search" onclientclick="search_click()">Search</button>
With the function being
function search_click() {
document.getElementById('hidVal').Value = "1";
<% save(); %>
}
In aspx.cs I have a function that does this:
using (System.IO.StreamWriter file = new System.IO.StreamWriter(#"C:\Users\fgreene\Desktop\savedAdresses.txt", true))
{
file.WriteLine(hidVal.Value);
}
After clicking the button I look into the file and there is no change.
Is my approach correct or am I not understanding how this works?
Putting <% save(); %> in a JavaScript function in an aspx page causes save to be run when the page is built on the server, not when the surrounding JavaScript function is called on the client. At this point, the hidden field is empty, so your file gets a blank line written to it. When the user clicks the button, the hidden field is filled, but there's nothing to tell the server that this has happened.
What you need to do instead is something like:
// In your aspx, for the javascript function: remove the call to save,
// use the correct ID for the hidden field
function search_click() {
document.getElementById('<%= hidVal.ClientID %>').Value = "1";
}
// In your aspx in place of the button put
<asp:Button id="search" runat="server"
onclientclick="search_click(); return true;" onclick="search_click">
Search
</asp:Button>
// This results in a button that calls the javascript function on click, and
// then posts back to the server saying that the button has been clicked
// In your C#, this function gets called when the client posts back to
// say that the button has been clicked.
public protected void search_click(object sender, EventArgs e)
{
using (System.IO.StreamWriter file = new System.IO.StreamWriter(
#"C:\Users\fgreene\Desktop\savedAdresses.txt", true))
{
file.WriteLine(hidVal.Value);
}
}
You'll need to reference the client ID of the hidden field, which is probably not hidVal, as the actual client side ID generated in the HTML will be based on the parent control's naming container. There's two ways to fix this. First, you could make the client ID static on the control (which basically tells ASP.NET make the ID exactly what I said.):
<asp:HiddenField ID="hidVal" value="" ClientIDMode="Static" runat="server" />
Second, you can look up the ClientID property from the server when you generate your JavaScript:
document.getElementById('<%= hidVal.ClientID %>').Value = "1";
This would render out the actual client ID directly in the JavaScript code. Either approach is probably fine, but the second one would only work if the JavaScript is embedded directly in your ASPX file and not in a static .JS file.
Calling server side methods:
The second part of your question is about calling server side code when the button is pressed. You should do this by attaching an OnClick handler to your button:
<button runat="server" id="BtnSearch" onclientclick="search_click()" OnClick="btnSearch_Click">Search</button>
When the button is pressed, the page will be posted back and the btnSearch_Click event handler will be called. You'll then be able to handle any server side logic, as well as check the value of your hidden field. Hope this helps!
What you're trying to do is execute a server side function (save) via a client side call. This won't work as it is calling save() when the page first loads and then putting the return value (which is probably nothing) into the code where you put
<% save(); %>
Instead you need your button to fire the save function, but fire your javascript first. You do this by creating an asp:button (which renders as ) and then adding both an "OnClientClick" (for your javascript) and "OnClick" (for your server side function).
<asp:Button id="btnSearch" runat="server" OnClick="btnSearch_Click" OnClientClick="search_click()" text="Search" />
Then in your C# code you need the method to be named the same as the OnClick value:
protected void btnSearch_Click(object sender, EventArgs e)
{
using (System.IO.StreamWriter file = new System.IO.StreamWriter(#"C:\Users\fgreene\Desktop\savedAdresses.txt", true))
{
file.WriteLine(hidVal.Value);
}
}
The easiest way to create your server side function is to double click on the button in design view, as it will add the correct method call for you.
Hope this helps.
You can change hidden field value in both serverside and client side events.
Change value in client side
<script type="text/javascript">
function setvalue() {
document.getElementById('hidVal').value = "1";
}
</script>
<div>
<asp:HiddenField ID="hidVal" value="" runat="server" />
<br />
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="setvalue();" />
</div>
Change value in serverside
<div>
<asp:HiddenField ID="hidVal" value="" runat="server" />
<br />
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</div>
protected void Button1_Click(object sender, EventArgs e)
{
hidVal.Value = "1";
}

JavaScript in button onclick not working

I have a webpage (ASP.NET C#) that has a button:
<asp:Button ID="btnHide" runat="server" OnClick="HidePanel" Text="Hide"/>
and I'm doing a JavaScript alert like:
public void HidePanel(object sender, EventArgs e)
{
Page.ClientScript.RegisterStartupScript(this.GetType(),"Hello","<script type=text/javascript> alert('Hello') </script>");
}
If I modify the function to not have object sender and EventArgs e then I can call it from Page_Load, and it works fine, I get the alert.
As written above I expect to see the alert when I click the button, but it's not happening. I'm sure it's something obvious, but I don't see it.
Use OnClientClick instead of OnClick. And add a return false to avoid a postback on the page.
<asp:Button ID="btnHide" runat="server" OnClientClick="alert('Hello'); return false;" Text="Hide"/>
You could try using,
ClientScript.RegisterClientScriptBlock(GetType(), "nameOfScript", scriptString);
I'ved used that before in a click event.
edit:
I can't edit posts yet, but your code works fine for me as well.
And depending on the situation he might want to do some server side stuff as well on the click event, or build up the script dynamically.
Edit:
If you already registered a script with the same name, it won't run, eg.
Page.ClientScript.RegisterStartupScript(this.GetType(), "Hello", "<script type=text/javascript> var i = 0;</script>");
Page.ClientScript.RegisterStartupScript(this.GetType(), "Hello", "<script type=text/javascript> alert('other hello') </script>");
You can remove the code to register the JavaScript code and instead do this:
<asp:Button ID="btnHide" runat="server"
OnClick="HidePanel" Text="Hide" OnClientClick="alert('Hello');"
UseSubmitBehavior="false" />
This:
UseSubmitBehavior="false"
will cause the button to fire the client-side script, and it will run the server-side code (post-back).

How to make the button_Click event opens the page in a pop-up window in ASP.NET?

The question is simple but I can't find a simple answer to it! .. btw I'll need to pass a QueryString to the page to be open.
Any ideas ?
You can actually link a javascript code into .NET with C#, below is an example, you could replace with your info, and push the parameters.
Response.Write("<script type='text/javascript'>window.open('Page.aspx?ID=" + YourTextField.Text.ToString() + "','_blank');</script>");
You can append on the end of it ?Field=your value passing&nextField=another value.
Is the answer to do this in javascript. As you make the underlying page in asp.net, provide it with the javascript to catch the buttons onclick event and call window.open(URL)
It depends on what you're trying to do but the simplest is to use the OnClientClick property of the Button. Take a look at http://msdn.microsoft.com/en-us/library/7ytf5t7k.aspx, in particular the details bout this property a little bit down.
Basically you'd do something like
<asp:Button ID="Button1" Runat="server"
OnClientClick="ShowPopup();"
Text="Test Client Click" />
With the JS to do your popup
<script type="text/javascript">
function ShowPopup() {
window.open('ThankYou.aspx');
}
</script>
You can also do both an OnClientClick and an OnClick if you need as well.
<asp:Button ID="Button1" Runat="server"
OnClick="Button1_Click"
OnClientClick="ShowPopup();"
Text="Test Client Click" />
Code behind
protected void Button1_Click(Object sender, EventArgs e)
{
Label1.Text = "Server click handler called.";
}

Categories