I have a variable in aspx.cs when I click the Datagrid particular row;
Then from the javascript, I should get that value and pass into aspx.cs variable.
how to do this?
Using html controls
First you use a hidden input control as:
<input type="hidden" value="" id="SendA" name="SendA" />
Second you add to that control the value you like to send on code behind using javascript as:
document.getElementById("SendA").value = "1";
And then on post back you get that value as:
Request.Form["SendA"]
Using asp.net Controls
The same way if you use asp.net control can be as:
<asp:HiddenField runat="server" ID="SendA" Value="" />
<script>
document.getElementById("<%=SendA.ClientID%>").value = "1";
</script>
and get it on code behind with SendA.Value;
And of course you can use ajax calls to send on code behind values, or simple call url with url parameters that return no content.
Related
I want otdval at code behind in asp.net C#
otdval = (parseFloat(otdval) + parseFloat(saletax)); // duesignmain;
$("#lblotd").text(String(Math.round(otdval)).replace(/(\d)(?=(\d\d\d)+(?!\d))/g,"$1,"));
Store otdval variable value in hidden field and retrieve in code behind.
In aspx create a hidden field
<asp:HiddenField id="hdnField" runat="server" />
In JavaScript
document.getElementById('hdnField').value = otdval;
In JQuery
$('input:hidden[id*=hdnField]').val(otdval);
In Codebhind
string otdvalValue = hdnField.Value;
I using .ascx
I am trying to use my jQuery to perform some action, the below code is how I call server control id.
$("input").mouseenter(function() {
//some function
}
<input type="text" id="family1" value="family"1/>
<input type="text" id="family2" value="family"2 runat="server"/>
My problem right now is my family1 able to call jQuery function but my family doesn't.
Is it because I am using .ascx so I cannot pass the server control id to my jQuery?
Is that any idea to solve?
You can get that, by using ClientID
Please check here
Use:
$("#<% family2.ClientID %>").mouseenter(function() {
//some function
}
because you can reuse your control, asp.net is adding some prefix to your ID. This resulting ID varries from page to page. You can check which is the resulting ID on your page and hardcode the ID into the jQuery Selector but using clientID is the way to go.
You can use it on all pages with your control without worries.
Use this ClientIDMode="Static"
<input type="text" id="family2" ClientIDMode="Static" value="family" runat="server"/>
Edit 1
ClientIdMode="Static": This is most basic, simple mode and it makes the client side ID static.
Whatever value you put for the ClientID or ID is that which will be used for the client side ID. One odd condition here, if a static ClientIDMode is used in a repeating control, the developer is responsible for ensuring client side ID uniqueness.
http://www.codeproject.com/Articles/34151/ASP-NET-4-0-Client-ID-Feature
$(document).ready(function () {
$('.ctl00_m_g_3b0d8e69_1961_4bea_886d_413493ff7f9c_ctl00_checkclass').hover(function (event) {
alert('dada');
var c = alert($(this).attr("id"));
alert(c);
});
});
<input type="text" id="family1" value="family1" class="checkclass"/>
<input type="text" id="family2" value="family2" class="checkclass" />
<input type="text" id="family3" value="family3" class="checkclass" />
I'm using ASP.NET to pass a value to a JavaScript function and, for some reason I haven't been able to determine, it isn't working when I try to pass in a value from another control. Instead, it acts like there is a syntax error and it just submits back to the main form.
Does anyone know why?
Example:
<asp:TextBox ID="txtToSay" runat="server" Text="Something"></asp:TextBox>
<asp:Button runat="server" ID="btnSaySomething1" Text="Say Something"
OnClientClick="saySomething(<%=txtToSay.Text%>);" /> <!-- doesn't work -->
<asp:Button runat="server" ID="btnSaySomething1" Text="Say Something"
OnClientClick="saySomething('<%=txtToSay.Text%>');" /> <!-- doesn't work -->
<asp:Button runat="server" ID="btnSaySomething2" Text="Say Something"
OnClientClick="saySomething('Something');" /> <!-- works -->
<script type="text/javascript">
function saySomething(txt){
alert(txt);
};
</script>
Additional Information:
Web Application running on .NET 4.0
Language: C#
Update:
After working with this a while, I've determined that you can't use <%%> tags in ASP controls. Additionally, if you're looking for dynamic evaluation of control values AVOID AVOID AVOID using <%=someControl.Text%> or similar constructs since they are only evaluated once a request is submitted to the server. If you need a static value from another control at runtime, simply set that value in the page load event or handle it another way in the code behind.
Javacript will search for variable name = txtToSay.Text in saySomething function call, Put quotes around it to make it string value
Change
OnClientClick="saySomething(<%=txtToSay.Text%>);"
To
OnClientClick="saySomething('<%=txtToSay.Text%>');"
You can get the txtToSay.Text without passing it this way
<script type="text/javascript">
function saySomething(txt){
alert(document.getElementById('<%=txtToSay.Text%>').value);
};
</script>
you need to put ' around your text in the saySomething() call.
Like this:
<asp:Button runat="server" ID="btnSaySomething1" Text="Say Something" OnClientClick="saySomething('<%=txtToSay.Text%>');" />
UPDATE
<%= %> won't work inside an asp.net control. Can you set it from the code-behind?
I.E
btnSaySomething1.OnClientClick = "Text to say"
I have a text field, I need to send these text value to the same page which is in jsp .
I just want to assign javascript value to jsp variable.
You can't. JSP runs on the server, Javascript runs on the client, so when Javascript runs the JSP variable doesnt exist anymore.
Consider using AJAX.
You could set a hidden field.
Put this in your JSP form:
<input type="hidden" id="foo" name="foo" />
Execute this script whenever you want to fill the field:
document.getElementById("foo").value = "some value";
When you submit the form, it'll be available as follows in the servlet:
String foo = request.getParameter("foo"); // "some value"
// ...
See also:
Communication between Java/JSP/JSF and JavaScript
I've created a jsp page. In that when i select 1 check-box or both check-box or none, the corresponding text-boxes and list-box must be displayed in the same page.
For that i tried of calling a javascipt function when i click the checkbox. The javascript function contain code to display the textboxes. But it didn't work.
Since I'm doing this project in struts, I don't know how to get check-box value. And calling of JavaScript function works. But didn't enter into jsp code in JavaScript function.
My code is
<tr>
<td>SEJ:</td>
<td>SEJ 1:<html:checkbox property="sej1" value="on" onclick="checkbox_trial()"></html:checkbox></td>
<td>SEJ 2:<html:checkbox property="sej2" value="on" onclick="checkbox_trial()"></html:checkbox></td>
</tr>
<script type="text/javascript">
function checkbox_trial()
{
<% if(request.getParameter("sej1")=="on"){
%>
<tr><td>SEJ1 Age<html:text property="sej1_age"></html:text></td></tr>
<tr><td>SEJ1 DOI<html:text property="sej1_doi"></html:text></td></tr>
<%}
else if(request.getParameter("sej2")=="on"){%>
<tr><td>SEJ2 Age<html:text property="sej2_age"></html:text></td></tr>
<tr><td>SEJ2 DOI<html:text property="sej2_doi"></html:text></td></tr>
<%}
else if((request.getParameter("sej1")=="on")&&(request.getParameter("sej2")=="on")){%>
<tr><td>SEJ1 Age<html:text property="sej1_age"></html:text></td></tr>
<tr><td>SEJ1 DOI<html:text property="sej1_doi"></html:text></td></tr>
<tr><td>SEJ2 Age<html:text property="sej2_age"></html:text></td></tr>
<tr><td>SEJ2 DOI<html:text property="sej2_doi"></html:text></td></tr>
<%}
else{%>
NOTHING <% } %>
}
This is how it works: Java/JSP runs at webserver, produces HTML/CSS/JS, webserver sends it to webbrowser, webbrowser runs HTML/CSS/JS. Not Java/JSP. Rightclick the page in webbrowser and choose View Source. If Java/JSP has done its job right, you shouldn't see any line of it in there.
If you want to invoke Java/JSP code using JavaScript, you've got to invoke another HTTP request to the webserver so that it can execute the Java/JSP code based on the specific request. You can do that by either submitting the form or firing an asynchronous (ajaxical) request.
Given the skills shown as far and the fact that you're using Struts, I think ajax is going to be a bit too complex. I'd suggest to just submit the form on click of the checkbox
<input type="checkbox" name="show" value="true" onclick="submit()" />
and then let JSP conditionally display the input fields (just a JSTL example since I don't do Struts)
<c:if test="${param.show == 'true'}">
<input type="text" />
<select />
</c:if>
Update: you've by the way another major problem in the code. You can't compare string values by == in Java code (you can do so in EL only). In Java code you need to use equals() method. Otherwise they will be compared by reference instead of by value. I'd suggest to learn basic Java as well.