I have a function on JS that must execute before the codebehind, but i'm having trouble retrieving it.
It will return a variable that i should use on the codebehind. The function i created is this one:
<script type="text/javascript" >
function returnHash() {
var checkout = new DirectCheckout('086F7D02A071267FEBF102EB759D9845B8B3333DFA65BC45B807F41A8525AD8D'); /* Em sandbox utilizar o construtor new DirectCheckout('SEU TOKEN PUBLICO', false); */
debugger;
var month = document.getElementById("txtDataVencUser").value;
var year = document.getElementById("txtDataVencUser").value;
var cardData = {
cardNumber: document.getElementById("txtNumeroCartaoUser").value,
holderName: document.getElementById("txtNomeCartaoUser").value,
securityCode: document.getElementById("txtCodSegurancaUser").value,
expirationMonth: month.substring(0, 2),
expirationYear: "20" + year.substring(3, 5)
};
checkout.getCardHash(cardData, function (cardHash) {
----------------------------> I NEED TO RETRIEVE "cardHash" VALUE IN CODEBEHIND
}, function (error) {
console.log(error);
});
}
</script>
I don't know if this step is correct, but i am using the onClientClick to try to execute the JS before the codebehind:
<asp:Button ID="btnFinalizarCompraJunoUser" runat="server" Text="FINALIZAR COMPRA" OnClientClick="returnHash()" OnClick="btnFinalizarCompraJunoUser_Click" Visible="true" />
I am having trouble figuring out what to do next, can someone help me please?
There are a number of ways - perhaps as many flavors of ice cream.
But, since you are going to run a "code behind" stub based on the click?
I would just drop a hidden field on the page, and then just set that value in js
So right below your button - add a hidden field say like this:
<form id="form1" runat="server">
<br />
<asp:Button ID="Button1" runat="server" Text="Button"
OnClientClick="myfun();return true" Width="68px"
ClientIDMode="Static"/>
<asp:HiddenField ID="HiddenField1" runat="server" ClientIDMode="Static" />
</form>
<script type="text/javascript">
function myfun() {
// do whatever - walk the dog - have fun!!!
var f1 = document.getElementById("HiddenField1");
f1.value = "2222";
}
</script>
So in above I just shove 2222 into the hidden1 field.
Now, in the the code behind button stub, you can do this:
Debug.Print("my click f1 = " & HiddenField1.Value);
So the code behind will now have a good old regular plane jane control to look at and get the value in question.
So, it depends on how many values - but using a code behind friendly (asp.net) control and setting the value with the js client side code probably is the least effort here.
I also of course used "staticID" for the above. If you for some reason don't like (or want) to use staticID for above, then the js selection would be:
var f1 = document.getElementById("<%=HiddenField1.Clientid%>");
f1.value = cardHash;
Related
I have four text boxes in 4 different tabs of an ASP.NET Page. I need to provide same validation message to all the text boxes. Presently I am using four different functions with same functionality. Kindly suggest a way to identify which textbox is being used make that a single funtion
Code from Commend:
<asp:TextBox ID="textBox1" runat="server" ValidationonGroup="abcd"></asp:TextBox>
<asp:CustomValidator ID="ID1" runat="server" ValidationGroup="abcd"
ControlToValidate="textBox1" ErrorMessage="message"
ClientValidationFunction="fname"></asp:CustomValidator>
--Javascript Fn--
function fname(sender, args) {
var x = document.getElementById('<%=txtBox1.ClientID%>').value;
if (some condition) {
args.IsValid = false;
} else {
args.IsValid = true;
}
}
Edit
if you are using Asp.Net then you should use like this
Aspx page code
<div>
<asp:TextBox runat="server" ID="txt1" ClientIDMode="Static" onKeyPress="javascript:text_changed(this.id);"></asp:TextBox>
</div>
JS Code
<script type="text/javascript">
function text_changed(event)
{
var id = event;
alert(id);
}
</script>
You got the textbox id in id variable and now u can get value etc using this id you are applying your conditions.... i hope this will help u
In my ASP.Net default.aspx, I have dropdown list control and following javascript code
<asp:DropDownList ID="ddlGenres" runat="server" AppendDataBoundItems="True" AutoPostBack="True" DataSourceID="EntityDataSource2" DataTextField="cityID" DataValueField="cityID" OnSelectedIndexChanged="ddlGenres_SelectedIndexChanged" ClientIDMode="Static" >
<script type="text/javascript">
var ddlClientID = '<%=ddlGenres.ClientID%>';
//document.getElementById(ddlGenres)
function SetddlVal(nIndx)
{
//$("#ddlGenres")
var ddlListSelect = document.getElementById(ddlClientID);
ddlList.SelectedIndex = nIndx;
}
</script>
====================================================================
In a external javascript file, I am calling the function SetddlVal.
I debugged the code and the ddlList selectedIndex is successfully changed within the script.
=====================================================================
The issue I am facing is, when the SelectedIndex value is changed in the javascript, The ddlGenres_SelectedIndexChanged code is not being triggered.
Please let me know if I am missing anything.
Thanks
Nate
In javascript the property of an <select> input is selectedIndex not SelectedIndex.
Change your function to this:
function SetddlVal(nIndx)
{
var ddlListSelect = document.getElementById('<%=ddlGenres.ClientID%>');
ddlListSelect.selectedIndex = nIndx;
}
<asp:Button ID="Button1" runat="server" Text="Lunch" CausesValidation="false"
CssClass="bigbuttons"
style="background:url(../App_Themes/Images/green-box.gif)"
Font-Bold="True" ForeColor="White" Font-Size="Large"
OnClientClick="openmodalWin(); return false;" />
<br />
javascript
function openmodalWin() {
var CloseTImeOfPopUp = window.showModalDialog("ClockPopUP.aspx", "Clock",
"dialogWidth:550px;dialogHeight:350px,");
}
As you can see, I'm calling the javascript function "openmodalWin" on the ButtonClick event. I checked that the function is returning CloseTimeofPopUp sucessfully and I am able to display it via an Alert().
[Moderator Edit: I want the returned value of that function to be sent to the server so that I can store it in the database?]
I want the return value from the javascript function, i can store on the same server side code i.e. Button1_OnClick() event and then i can store it to database.
Please tell me how i can do this ?
You can set the value of CloseTimeOfPopUp in a hidden variable (defined with runat="server") and then you can access that in the code behind.
In aspx page you can do:
<input type="hidden" runat="server" id="hidden1" />
In javascript
function openmodalWin() {
var CloseTImeOfPopUp = window.showModalDialog("ClockPopUP.aspx", "Clock", "dialogWidth:550px;dialogHeight:350px,");
document.getElementById('hidden1').value = CloseTImeOfPopUp;
}
In code behind in C#
var myVal = hidden1.Value;
in addition to Habib.OSU
in the page you are opening with showModalDialog
you should return the value like this...
javascript in the Modal Dialog Page:
function ReturnVal(valToReturn) {
window.returnValue = valToReturn;
window.close();
}
Hi, I have following java script function:
function EnableDisableTextBox(chkBoxId, txtBoxId) {
var isChk = document.getElementById(chkBoxId);
document.getElementById(txtBoxId).disabled = !(isChk.checked);
}
When I am trying to call above function, by clicking check box its not working as expected
<asp:CheckBox ID="chkBachelors"
onclick="javascript:EnableDisableTextBox('chkBachelors','txtFirstDegree');"
runat="server" Text='<%$Resources:Resource, FirstDegree %>' TextAlign="Left"/>
<asp:TextBox ID="txtFirstDegree" CssClass="form-text" runat="server"
MaxLength="250"></asp:TextBox>
Expected Result (When user click chkBachelors check box):
if "chkBachelors" check box is checked
then enable "txtFirstDegree" text box
else
disable "txtFirstDegree" text box
What is the problem and how to solve it?
<asp:CheckBox ID="chkBachelors"
onclick="EnableDisableTextBox(this);"
runat="server" Text='' TextAlign="Left"/>
<asp:TextBox ID="txtFirstDegree" CssClass="form-text" runat="server"
MaxLength="250"></asp:TextBox>
<script language ="javascript" type="text/javascript">
function EnableDisableTextBox(checkbox)
{
var txtBoxId= "<%=txtFirstDegree.ClientID%>";
document.getElementById(txtBoxId).disabled = !(checkbox.checked);
}
</script>
<%=chkBachelors.ClientID%> and <%=txtFirstDegree.ClientID%> will give the client side ID of the Asp.Net controls .You don't need to pass them explicitly
function EnableDisableTextBox() {
var isChk = document.getElementById(<%=chkBachelors.ClientID%> );
document.getElementById(<%=txtFirstDegree.ClientID%>).disabled = !(isChk.checked);
}
I'm trying to pass a textarea value to the server side. The textarea cant be runat=server though.
heres my code:
<script type="text/javascript">
function replace() {
//Replace < and > on textarea
var obj = document.getElementById('recipient_list');
var str = obj.value;
str = str.replace(/</i, "(");
str = str.replace(/>/i, ")");
obj.value = str;
alert("rec_lst.value: " + document.getElementById('recipient_list').value);
//Pass value to server.
alert("passing to server");
document.getElementById("ctl00$ContentPlaceHolder1$txtEmails").value = str;
alert("Passed to server");
alert("txtEmails.value: " + document.getElementById("ctl00$ContentPlaceHolder1$txtEmails").value);
}
</script>
This isn't working though... Any ideas how to fix or better implement this??..
Try this:
var txtEmail = document.getElementById("<%=txtEmails.ClientID%>");
txtEmail.value = str;
However this won't pass anything to the server, just change text box value.
To have it sent to the server user will have to click submit button, or if you have server side button have such code to "auto click" it thus performing post back:
document.getElementById("<%=btnSubmit.ClientID%>").click();
you cant pass value from client to server using JS .
JS is only a client side code.If you can do something like this, Hacking will be so easy :)
one thing you can do is add some text or label control as invisible and assign the value to the control and pass it to server when some event happens.
Sample code
<asp:TextBox ID="textbox1" runat="server" ></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="callOnEvets()"/>
function callOnEvets()
{document.getElementById('textbox1').value = 10; }