Issue with setting hidden field in asp.net winforms using jquery - c#

I have some code to set the value of a hidden field so I can access it in the code behind but the value is always empty in the code behind. The value for the effectiveDate is being set but I doesn't look like the hidden field property Value is being set.
<input id="appEffectiveDate" type="text" />
<label id="effectiveDateLabel" for="appEffectiveDate">App Effective Date</label>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<asp:HiddenField ID="appEffectiveDateToSetForUserRole" runat="server" Value="" Visible="false" />
<script>
$(function () {
var SelectedDates = {};
$('#appEffectiveDate').datepicker({
beforeShowDay: function (date) {
var Highlight = SelectedDates[date];
if (Highlight) {
return [true, "Highlighted", Highlight];
}
else {
return [true, '', ''];
}
}
});
$("#effectiveDateLabel").hide();
$("#appEffectiveDate").hide();
$('input[value="85"]').click(function () {
if($(this).is(':checked'))
{
$("#effectiveDateLabel").show();
$("#appEffectiveDate").show();
}
});
$("#appEffectiveDate").change(function () {
var effectiveDate = $("#appEffectiveDate").val();
$(":asp(appEffectiveDateToSetForUserRole)").prop('value', effectiveDate);
});
});
</script>
In the code behind the value is empty for the hidden field:
if (!string.IsNullOrEmpty(appEffectiveDateToSetForUserRole.Value))
{
// this is never called because .Value is empty
}

If Visible is set to false, the control will not be rendered by ASP.NET in the markup at all, which means that jQuery won't be able to find it because it doesn't exist. Just remove the visible=false part. It'll stay hidden.

Related

onmouseover change text based on value from database

My code is in C#. I have a span with id change. I need to change the text onmouseover to a value from the database. I got the value and assign it to a label and I made it hidden. Now on mouseover I want to get the value of the hidden label.
Here is my script.
<script>
$(document).ready(function () {
$("#change").mouseover(function () {
$('#change').text("value of label");
});
$("#change").mouseout(function () {
$('#change').text("Investor");
});
});
</script>
How can I do it?
Solved By Me :)
I have solved the issue. It was because I had visible=false in the label properties and I should replace it with style="display:none;" .
.Regarding my Script. It is as below .
$(document).ready(function () {
var originalText = $('#change').text();
$('#change').mouseover(function () {
var hiddenVar = $('[id$="NewAccountsLabel"]').html();
$('#change').text(hiddenVar);
});
$('#change').mouseleave(function () {
$('#change').text(originalText);
});
});
Why don't you use the label value , your label should have the id
$(document).ready(function () {
Var lblvalue = $('#label').val();
$("#change").mouseover(function () {
$('#change').text(lblvalue);
});
$("#change").mouseout(function () {
$('#change').text("Investor");
});
The jQuery events are mouseover and mouseleave events. More on this here
// save the previous value in javascript variable
var originalText = $('#change').text();
//mouse event on mouseover the span
$('#change').mouseover(function() {
// read the value from the hidden label
var hiddenVar = $('#NewAccountsLabel').text();
// assign the new value from the hidden var
$('#change').text(hiddenVar);
});
//mouse event when mouse leaves the span
$('#change').mouseleave(function() {
// assign the original value when it leaves
$('#change').text(originalText);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.1/jquery.min.js"></script>
<span id="change"> Investor</span>
<label id='NewAccountsLabel' hidden> value from DB </label>

Access jquery variable from the code behind

This is my html:
<input type="hidden" id="HiddenIndex" name="HiddenIndex" runat="server" />
Some labels and textbox
<asp:Button runat="server" ID="btnGetCoordinates"
Text="Get Coordinates" OnClick="btnGetCoordinates_Click" />
<asp:Label ID="info" runat="server" Text="Waiting...." />
So when the button Get Coordinates is clicked, it will call the Web Services to return some json results from code behind. A Dialog will popup a listbox with these results. It works perfectly until this point. My goal is when a client select an item in the list and click on "Select" button, it will return the selected item's Index, store in a hidden field and manipulate later from the code behind.
This is my jquery function
function ShowPopup()
{
$("#parentForm").fadeTo(500, .2);
$("#C1Dialog1").dialog({
open: function () {
$(".ui-dialog-titlebar-close").hide();
},
buttons: [{
text: "Select",
click: function () {
var value = " ";
storedIndex = " ";
var selected = $("[id*=lstCandidates] option:selected");
selected.each(function () {
value = $(this).val();
storedIndex = $(this).index();
$("#HiddenIndex").val(storedIndex);
});
alert(value + " and index is " + storedIndex); //Show value and index
alert("html hidden value " + $("#HiddenIndex").val()); //show value
$(this).dialog("close");
$("#parentForm").fadeTo(500, 1);
},
style: "margin-right: 40px;"
},
{
text: "Cancel",
click: function () {
$(this).dialog("close");
$("#parentForm").fadeTo(500, 1);
},
style: "margin-left:0px;"
}]
});
}
</script>
As you can see the alert show the value of the hidden field
This is my code behind
protected void btnGetCoordinates_Click(object sender, EventArgs e)
{
//Show the Dialog
if (count > 0)
{
ClientScript.RegisterStartupScript(this.GetType(), "Popup", "ShowPopup();", true);
//never stop here --PROBLEM RIGHT HERE**, there is NO value for Hidden field**
var indexValue = Request.Form["HiddenIndex"];
info.Text = "HiddenIndex is " + indexValue;
}
}
The Info label show nothing when I click on Dialog's select button
Any help would be appreciated , thank you very much.
Probably an issue with the client ID. ASP.NET will not necessarily use the client ID in your markup when emitting the HTML.
There are several ways to fix this, but the easiest is to make the hidden field a plain HTML control. That way ASP.NET won't monkey with it. So change this
<input type="hidden" id="HiddenIndex" name="HiddenIndex" runat="server" />
to this
<input type="hidden" id="HiddenIndex" name="HiddenIndex"/>
Other options:
Set your clientIDmode to static
Modify your jquery selector to use id$='HiddenIndex' so that it ignores any prefix added by ASP.NET.

KnockoutJs Checkbox Binding

I have been stuck for a couple days now and I have tried a few things in regards to pre-populating my checkbox. I have a view model and 2 functions called "callServiceget" which calls a webservice to get the value of my checkbox field (true or false) and "retVendor" which updates the value of the checkbox.
if the value of the checkbox in the database is true, I want it to be checked when i load the page, unfortunately, I am unable to accomplish this. Any help or advice is much appreciated. Thanks in advance.
<input class="input" type="checkbox" data-bind="checked: vendor_inventory" />
<script type="text/javascript">
function VendorViewModel() {
var self = this;
self.vendor_inventory = ko.observable("");
self.callServiceGet = function (id) {
vendors.get("accounting", 2, id, self.retVendor);
}
self.retVendor = function (results) {
self.vendor_inventory(results.inventory);
}
//self.vendor_inventory.subscribe(function (value) {
// alert(value);
//},self);
}
</script>
<script type="text/javascript">
var id = 122;
vm = new VendorViewModel();
vm.callServiceGet(id);
ko.applyBindings(vm);
</script>

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 );
});
});

How to get hidden field value code behind updated by jquery/javascript

I have an <asp:menu/> control and a hidden field.Now i am using jQuery to change value of hidden field.
Code is:-
$(function() {
$(".primaryStaticMenu tr,td").each(function(index) {
$(this).click(function() {
if ($(this).attr("title") != "undefined"
&& $(this).attr("title").length > 0) {
document.getElementById('ctl00_Hidden_Master_Location').value = $(this).attr("title");
alert(document.getElementById('ctl00_Hidden_Master_Location').value);
//return false;
}
});
});
});
Server side code to get updated value is:-
string Get_cng_value = Hidden_Master_Location.Value;
But Hidden_Master_Location.Value shows null every time.
can any one tell me how to get updated value of hidden field from code behind.
Let say your hidden field is as..
<asp:HiddenField ID="Hidden_Master_Location" runat="server" />
you can get the value of hidden filed in jquery as
var locationValue= $("#<%= Hidden_Master_Location.ClientID %>").val();
Do this, it works for me.the trick is to save your hidden field precious id in another hidden input field then build it back using that hidden value.
Markup
<asp:HiddenField ID="HiddenFieldMaster" runat="server" />
<input type="hidden" id="inputHidden" value='<%= HiddenFieldMaster.ClientID%>' />
Javascript
$(function() {
$(".primaryStaticMenu tr,td").each(function(index) {
$(this).click(function() {
if ($(this).attr("title") != "undefined"
&& $(this).attr("title").length > 0) {
var inputHidden = document.getElementById('inputHidden');
$("#" + inputHidden.value).val($(this).attr("title"));
alert(inputHidden.value);
//return false;
}
});
});
});
Code Behind
String Get_cng_value = HiddenFieldMaster.Value;

Categories