Is it possible to pass the contents of a textbox or Listbox into the URL portion of the javascript code window.open(URL)? I have an asp.net listbox control that displays URL values. When ever an end user clicks on another listbox, the URL listbox provides the specific URL. I am trying to pass this URL into the above javascript code, but I do not know the correct syntax to do this. This code will execute as an onclick event.
For clarification, similar to typing “+ ListBox.Text.ToString() +” or ‘” & List.Text & “’” to add the content of a listbox into something else, such as a textbox. Is there specific syntax to do the same, but add the listbox.text into javascript?
Thank you,
DFM
Simply add a client-side onclick handler to your listbox as shown below:
<asp:ListBox id="ListBox1" runat="server" .....
onclick="openPopup(this)">
........
</asp:ListBox>
Then add the following javascript code:
<script type="text/javascript">
function openPopup(e){
window.open(e.value);
}
</script>
Sure, this should be pretty easy with jQuery. Obviously the URL generation could be reduced to a single statement, but should give you the general idea.
$(document).ready(function() {
$("your-element").click(function() {
var str = $("#listbox-id").val();
var url = "your-url.com/" + str;
window.open(url);
});
});
Related
I have a complicated page but I created a simple ASP.NET page with the issue. I have telerik RadAsyncUpload control and a button inside an UpdatePanel as shown:
<asp:UpdatePanel ID="_updatePanel" runat="server" UpdateMode="Conditional">
<ContentTemplate>
...
<telerik:RadAsyncUpload ID="fileUpload" runat="server" MaxFileInputsCount="1" OnClientFilesSelected="fileUpload_ClientFilesSelected" /><br />
<asp:Button ID="_saveNewFileButton" runat="server" OnClick="_saveNewFileButton_Click"
Text="Save"/>
</ContentTemplate>
</asp:UpdatePanel>
When a file is selected I want to disable the _saveNewFileButton and change the text to "Please Wait for Attachment Upload..." but I can't seem to get hold of the button reference in javascript:
var FilesUpdateInterval = null;
//Handles client side FilesSelected event for _newFileUploadButton.
function fileUpload_ClientFilesSelected(sender, args) {
//disable the click event for submit button during upload
var submitButton = $find('<%= _saveNewFileButton.ClientID %>');
submitButton.set_text('Please Wait for Attachment Upload...')
submitButton.set_readOnly(true);
if (FilesUpdateInterval == null) {
FilesUpdateInterval = setInterval(function () { FileCheckForUploadCompletion(); }, 500);
}
}
I am getting submitButton is null error. I tried putting this javascript code outside the updatepanel and inside ContentTemplate with same result. Obviously whatever I am doing is wrong. How do I get hold of the control that is in updatepanel in javascript?
EDIT: I find out that $find works with only telerik controls. So, I have to either use document.getElementById function or JQuery with something like Steve specified. Also, I have to use RegisterClientScriptBlock. I will test with Steve suggestion and then accept the answer.
short version - use $get() or document.getElementById(), as regular HTML elements are not IScriptControls, so $find() will not give you anything, and they don't have the rich client API you are trying to use.
For example
var submitButton = $get('<%= _saveNewFileButton.ClientID %>');
submitButton.setAttribute("value", "Please Wait for Attachment Upload...");
Option 2 - use RadButton.
Using jQuery and vb with ASP.Net, I've done something similar to this, which has worked well, even if it isn't that pretty. The [Whatever] I have was a FormView that didn't always have the control. Also, I didn't use it with a button, but I think that's the syntax for changing the button text. Either way, it might give you some ideas:
$('#<%=GetButtonClientID("_saveNewFileButton")%>').attr('value', 'Please Wait for Attachment Upload...');
And then I have a function like this:
Public Function GetButtonClientID(ByVal argFieldName As String) As String
Dim tmpID As String = "0"
Dim tmpButton As Button = [Whatever].FindControl(argFieldName)
If Not tmpButton Is Nothing Then Return tmpButton.ClientID.ToString Else Return "0"
End Function
I am new on Asp.net. I am using Javascript calender It use Input tag.if I use runat="server" so that I can get value on cs page then it does not display calender. How can I get calender value from input tag to .cs page
Simplest way:
Have a hidden field on your control.
Set its in javascript by using :
$('#<%=hdDate.ClientID %>').val("date value here");
and then you can access this hidden field in cs file.
create a hidden field as in your aspx page which should be runat="server".
then
On button click call javascript function say jsOnclikfun() and write this function as
<script type="text/javascript">
function jsOnclikfun() {
var dateInputtext = document.getElementById("Id Of input textbox which you are trying to make runat=server");
var hdDateValue = document.getElementById('<%= hiddenField_Id.ClientID %>');
hdDateValue.value = dateInputtext.value;
return true;
}
</script>
Now on server side where ever you need data get it from that hidden field as hiddenfieldID.Value.
Hope this helps.
On the click of the button my Onclick event gets fired.
Inside the onclick event I generate somefile at runtime and render it to the browser in the following way. But before rendering it to the browser I am making particular Label visible. But still the Label never become visible. Any idea what is the problem
lblInfoMessage.Visible=true;
Response.ContentType = "text/plain";
Response.AppendHeader("Content-Disposition", "attachment; filename=test.gxml");
doc.Save(Response.OutputStream);
Response.End();
You can either refresh the page (and thus change visibility or contents of controls) or send an attachment. Not both. So you will have to find some other way, maybe client side javascript?
EDIT
In the button you need a OnClientClick in the server side code, this will translate into a client-side "onclick". Here you can call a javascript function where you can (for instance) display some text. Note that this function is executed before the submit action that will generate the file.
Something like this in the html/aspx:
<span id="infoMessage"><!-- empty --></span>
...
<asp:Button OnClientClick="showInfo()" ... />
...
<script type="text/javascript">
function showInfo() {
document.getElementById("infoMessage").innerText =
"This is the info message.";
}
</script>
You can't just show that Label that you have now, as that doesn't exist in the client-side html when it is invisible.
should it not be as below?
lblInfoMessage.Visible=true;
work on asp.net vs 05 C#.Master page header contain the bellow code
<script type="text/javascript" language="javascript" src="JScript.js"></script>
from my one page i call the javascript method
<asp:TextBox ID="txtLength" runat="server" >0</asp:TextBox>
<asp:TextBox ID="txtHeight" runat="server" onchange="javascript:DoChange();">0</asp:TextBox>
javascript method is below :
{
alert("hello world");**//get this alert but don't get the bellow alert.**
var a=document.getElementById("txtLength");
var b=document.getElementById("txtHeight");
alert(a.value*b.value);
}
want to show value on message box .Actually want to calculate the Sft,How to ?
The DOM ID of the text boxes is not the same as the server side asp.net ID. In order to get the client DOM ID, use the ClientID property:
var a=document.getElementById("<%= txtLength.ClientID %>");
var b=document.getElementById("<%= txtHeight.ClientID %>");
Also, you are attempting to multiply two strings together even if that works.
More generally, I recommend using a javascript debugger like Firebug or Chrome's debugger. Then you will be able to see precisely what line the errors are occurring on and what they are.
Kindly refer to the below link
http://social.msdn.microsoft.com/Forums/en/netfxjscript/thread/dc7c1aa6-7571-460c-b039-13cdba8c396c
A TextBox is set to AutoPostback as changing the value should cause a number of (display-only) fields to be recalculated and displayed.
That works fine.
However, when the field is tabbed out of, the focus briefly moves on to the next field, then disappears when the page is redrawn so there is no focus anywhere.
I want the focus to be on the new field, not the textbox I've just changed.
Is there a way to work out which field had the focus and force it to have it again when the page is redrawn?
This is "by design". If you are using ASP.NET 2.0+ you can try calling the Focus method of your TextBox once the postback occurs (preferably in the TextChanged event of the TextBox).
I am not sure if there is any built-in way to track focus but I found this article in CodeProject which should do the trick.
You could also consider refresh display-only fields using AJAX UpdatePanel. This way you won't lose focus from the new field.
Also I have proposed pure server-side solution based on WebControl.Controls.TabIndex analysis, you can use it, if you like.
This is what is happening:
1) TAB on a field - client event
2) Focus on next field - client event
3) Postback - server event
4) Page redrawn - client event new page overrides preious client events
The solution of your problem is to:
a) get the element that has gained focus BEFORE postback
<script>
var idSelected;
$("input").focusin(function () {
idSelected = this.id;
});
</script>
b) store the ClientID (actually in var idSelected) somewhere (i.e. an hidden Textbox, vith ViewState = true) BEFORE postback
** b) get ClientID ** (extract from hidden TextBox and put it in var idSelected) AFTER postback
d) get the element with ClientID and set focus AFTER postback
<script>
$(document).ready(function () {
if (idSelected != null) {
$("#" + idSelected).focus();
idSelected = null;
});
});
</script>
Note: this sample scripts use JQuery.
Remember to put Jquery.js in your solution and a reference in your page
<form id="form1" runat="server" enctype="multipart/form-data" method="post">
<asp:ScriptManager runat="server" >
<Scripts>
<asp:ScriptReference Path="~/Scripts/jquery.js" ScriptMode="Auto" />
....
Note2: this solution works without AJAX.
Look at this answer: to make Javascript work over Ajax you must use code like this:
<script type="text/javascript">
Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
function EndRequestHandler(sender, args)
{
MyScript();
}
</script>