Pass value to Jquery function from code behind - c#

I am using Jquery progress bar control on asp.net form to show the percentage of completed work for inspector. Here is the client side function for progress bar.
<script>
$(function () {
$("#progressbar").progressbar({
value: 37
});
});
</script>
<form id="form1" runat="server">
<div id="progressbar"></div>
</form>
Is there any way to set progress bar value from code behind page? Values are read from database.

You can use registerstartupscript to inject the script from ASP.NET code behind files.
myScript = "\n<script type=\"text/javascript\" language=\"Javascript\" id=\"EventScriptBlock\">\n";
myScript += "ShowProgressBar(35);"; //35 is dynamic value
myScript += "\n\n </script>";
Page.ClientScript.RegisterStartupScript(this.GetType(), "myKey", myScript, false);
JavaScript Code
function ShowProgressBar(value)
{
$("#progressbar").progressbar({
value: value
});
}

Try below steps
First set the DB value to hidden field on page load. like (HiddenFieldID.value="37" // from DB)
Use the hidden field value to set the value to progress bar in js function as below.
$(function () {
$("#progressbar").progressbar({
value: $("#<%= HiddenFieldID.ClientID %>").val();
});
});

You may want to try:
ScriptManager.RegisterClientScriptBlock(Me.Page, Me.Page.GetType(), "progressbar", "ShowProgressBar("+ 35 +");", True)

Related

displaying more information by clicking onlink button more

I have a jquery function where upon clicking on a more link I display more information of a specified summary.
I am relatively new to jQuery and I was hoping for a pointer as to where I am going wrong as it is not working as it is.
$(document).ready(function () {
$('#more').on("click", function () {
"$('#more').hide(); $('#content').show();"
});
});
This is my C# code on code behind
topicGenerator.InnerHtml += summary.Substring(1, 100);
topicGenerator.InnerHtml += "<a href='#' id='more'> more...</a>";
topicGenerator.InnerHtml += "<div id='content' style='display:none;'>"+summary+ </div>";
Kind regards
Try changing
"$('#more').hide(); $('#content').show();"
to
$('#more').hide();
$('#content').show();
You don't need to wrap these statements in "quotations".
You could also condense .hide() and .show() into .toggle():
<script>
$(function(){
$("#more").click(function(){
$("#content").toggle();
});
});
</script>
See fiddle.
this will swap between show and hide and change the <a> to less then again more
$(document).ready(function () {
$('#more').click(function () {
$('#content').toggle();
if($('#more').html()=='more...'){
$('#more').html('less...');
}else{
if($('#more').html()=='less...'){
$('#more').html('more...');
}
}
});
});

How to get a variable value from aspx(jquery) to its codebehind?

I have a jQuery variable like
Default.aspx:
$(function () {
$("#divimgbtnGo").click(function () {
var ServiceNo = $(".ddlService option:selected").val();
});
});
Here I am getting value into ServiceNo. I want to use these value in my codebehind (Default.aspx.cs).
Can anyone please help?
All information in search is about getting codebehind to aspx. SO could not found any useful result and stuck here
Have a hidden feild in your aspx page then pass your variable value to that hidden field like this
$(function () {
$("#divimgbtnGo").click(function () {
$("#<%= yourhiddenfield.ClientID %>").val($(".ddlService option:selected").val());
});
});
In your Code behind get the value of hidden field as yourhiddenfield.Value
You can use, for example, a Hidden field, so ASP.NET will take care of transfering that data to the server and mapping it to CLR datatype after.
You can take hidden field and set ServiceNo value to hiddenField and u can use hiddenfield in server side.
add hidden field in Default.aspx page
<asp:HiddenField ID="hdnServiceNo" runat="server" />
set hidden field value.
$(function () {
$("#divimgbtnGo").click(function () {
var ServiceNo = $(".ddlService option:selected").val();
$('#hdnServiceNo').val(ServiceNo );
});
});

Show a div tag content on entering a character into a textbox?

I am trying to slide down content within a div tag (red rectangle) when a user enters atleast one character in a textbox (green rectangle).
If no character is entered then the div content does not show.
Here is a pic of the aspx (design):
And this is the JavaScript I am using to try and achieve this:
function() {
$("#SubmitSection").hide();
inputOne = document.getElementById("<%= TextBox1.ClientID %>");
if (inputOne.value != "") {
$("#SubmitSection").slideDown('slow');
}
else {
$("#SubmitSection").slideUp('slow');
}
}​
All the backend code works just fine but both the textbox and the div section show up when deployed.
if i got you right :
$("<%= TextBox1.ClientID %>").change(function(){YoursSlidingFunctionhere();})
.change()
You have to hook up to an event on the textbox. The onchange event only fires after the textbox loses focus, so I suggest the onkeyup event.
<asp:TextBox ID="TextBox1" runat="server" [...] onkeyup="updateSubmitSection()" />
You will have to name your function so that you can call it:
updateSubmitSection = function() {
$("#SubmitSection").hide();
inputOne = document.getElementById("<%= TextBox1.ClientID %>");
// ...
}
In addition to that, you need to call the function during the onload event of the page:
<body onload='updateSubmitSection()'>
The same solution using jQuery:
$(document).ready(function() {
updateSubmitSection();
$('#<%= TextBox1.ClientID %>').change(updateSubmitQuery);
}
You'll need to add this to a section of your javascript that is executed on initialization:
`$("#SubmitSection").hide();`
You could use the body onload attribute:
`<body onload="init()">
<script>
function init()
{
$("#SubmitSection").hide();
}
</script> `

jquery alertbox is getting disabled automatically

I have written the code on
ascx script:
<script src="JScripts/jquery.alerts-1.1/jquery.alerts.js" type="text/javascript"></script>
<script>
$(function() {
$('#ImageButton1').click(function() {
jAlert('Please enter a valid Suggestion ID.', 'Case Entry');
});
});
</script>
and on
Code behind:
Page.ClientScript.RegisterStartupScript(this.GetType(), "Window", "callAlert();", true);
the problem is alert box is automatically getting disabled after some time when page load fully
What could be the reason that the alert box is being disable after clicking on OK button and how to call the callAlert function in proper way.
If you are using Master page or pages then you won't get the Client Id of the button as you are declared it should be declared as $('#<%=ImageButton1.ClientID%>') or $('[id$=ImageButton1]') hope it will solve you problem.
$(document).ready(function(){
$('#<%=ImageButton1.ClientID%>').click(function() {
alert('Please enter a valid Suggestion ID.', 'Case Entry');
});
});
You can try to put the following line before the function
$(document).ready(function() {
This will make it:
$(document).ready(function() {
$('#ImageButton1').click(function() {
jAlert('Please enter a valid Suggestion ID.', 'Case Entry');
});
});
});
If you wait till the page is ready, the alert box won't be overwritten (I hope x)).
Also when you check that text box, check if the condition is false, then give the alert.
Is the condition not false? Build in a check to check if the condition is really true. If so? Redirect.
EDIT:
var answer = Confirm: ("This page will now redirect. Are you ready?")
if (answer)
//redirect
else
return
OK, so first it's important to understand that $(function(){... and $(document).ready(function() {... are equivalent, and nothing inside either will execute until the page is fully loaded. In other words, there's no need to use
Page.ClientScript.RegisterStartupScript(this.GetType(), "Window", "callAlert();", true);
That can be removed. Also, I see that you're probably using web forms. Be mindful that the Id attribute that will be rendered is not equal to the Id of the control attribute. In other words, if you have a runat="server" control with an Id of ImageButton1, using the syntax $('#ImageButton1') in your jQuery won't work.
Taking this into account, I've added an example below that uses selectors based on class attributes.
<script type="text/javascript">
$(function () {
$('.ImageButton1').click(function (e) {
var text = $('.TextBox1').val();
var redirect = true;
if (!text) {
redirect = confirm('Empty...are you sure?');
}
if (redirect) {
window.location.href = 'http://your-redirect-here.com';
}
});
});
</script>
<input class="TextBox1" type="text" />
<input class="ImageButton1" type="button" value="Click" />
That should get you where you want to go. Let me know if you have any questions.
var answer = Confirm: ("This page will now redirect. Are you ready?")
if (answer)
{
//redirect
} else
{
return false;
}
Put this after jAlert Box:
return false;
And call the function like this:
return callAlert();

Required textbox in javascript

I have this code
$(document).ready(function () {
$("#<%= chkSpecialIntegration.ClientID %>").click(function () {
if (this.checked) {
document.getElementById('<%=ddlTypeSpecialIntegration.ClientID %>').style.visibility = 'visible'; }
});
});
When this is checked then a textbox is no longer required. How can I do this?
If all you want to do is make ddlTypeSpecialIntegrationvisible when chkSpecialIntegration is checked, you can just do:
$(document).ready(function () {
$("#<%= chkSpecialIntegration.ClientID %>").toggle(function() {
$("#<%= ddlTypeSpecialIntegration.ClientID %>").show();
}, function() {
$("#<%= ddlTypeSpecialIntegration.ClientID %>").hide();
});
});
There are two ways that an html textbox can be forced to be required. You should implement both.
The first is to validate the data prior to form submission. You can accomplish this in javascript by hooking into the onsubmit event. An example is at http://www.w3schools.com/js/js_form_validation.asp
Inside that method you will need to test if your checkbox is selected or not. If it isn't, then see if they typed something in your textbox.
The second is to validate it server side after form submission. For this you could simply provide some validation code in your button's server side onclick method.
I say to implement both because you will want to provide immediate feedback when something is required client side and you want to enforce it server side in case javascript is turned off.
Of course, if JS is turned off then they will probably never see the textbox to begin with.
Why do you need JS for that?
Isn't something like this enough?
<input<% if some_condition %> required="required"<% endif %> name="field" />
Give id for textbox like
<%: Html.TextBoxFor(model => model.FirstName, new { #tabindex = "1", maxlength = "50" ,id="Name"})%>
$(document).ready(function () {
$("#<%= chkSpecialIntegration.ClientID %>").click(function () {
if (this.checked) {
document.getElementById('<%=ddlTypeSpecialIntegration.ClientID %>').style.visibility = 'visible';
$("#Name").hide;
}
});
});

Categories