Javascript cannot find hidden Field in ASP.NET? - asp.net

I am trying save the disabled property value of a hidden field to track the disabled state of a button between postbacks, with the following javascript
function TrackState(buttonID)
{
var trackingField = document.getElementById("_tracking" + buttonID);
return false; // prevent default action
}
HTML
<input type="hidden" name="_trackingButton1" value="true" />
but trackingField seems to be null each time, what is going wrong here

You need to assign the id property of your element (not just name) and it should work like this:
<input type="hidden" id="_trackingButton1" name="_trackingButton1" value="true" />
I hope this helps.

In your function
function TrackState(buttonID)
{
}
what is the buttonID value exactly. I hope it is "Button1".
And as the function says getElementById the input has the property id with the same value.

The getElementById() method specifically looks for id values:
<input type="hidden" id="_trackingButton1" name="_trackingButton1" value="true" />

Related

bind data to an element on an ng-if condition angular js

I am new to angular js. and I have a situation.
I want to bind data to an input element from another input element based on a condition (checking a checkbox). I am trying to achieve this using the
ng-if angular directive. But it's not working,
So when I type something into input1 and check the checkbox, I want the value of input1 to be reflected in input2.
<input ng-model="value.one" type="text" />
<input ng-model="checked" ng-change="isChecked(value.one)" type="checkbox" />
<input ng-model="value.selected" />
in your controller:
$scope.isChecked(val) {
if($scope.checked) {
$scope.value.selected = val;
} else {
$scope.value.selected = null;
}
}

How to redirect #Url.Action

I have link with parameter:
[http://localhost:8545/Admin/Agent/ManageUser?agentId=3230][1]
After change language new link:
[http://localhost:8545/Admin/Agent/ManageUser][2]
have error beacause haven't ?agentId=3230
i use :<input type="hidden" name="ReturnUrl" value="#Url.Action(null)" />
i don't know edit #Url.Action(null), please help me. thanks!
try this input
<input type="hidden" name="ReturnUrl" value="#Url.Action(ViewContext.RouteData.Values["controller"].ToString(), ViewContext.RouteData.Values["action"].ToString(), new { agentId = Request.QueryString["agentId"] })" />
Firs parameter if Url.Action is controller name, second parameter is action name and last one the routeValues can you add query strings in URL.
If you just need to return the user to the same URL he was before changing the language, just keep the full URL in your hidden input as follows:
<input type="hidden" name="ReturnUrl" value="#Request.Url.AbsoluteUri" />
Also (assuming your ChangeLanguage method is accessibly using the same host-name), you could simply do:
public ActionResult ChangeLanguage(string lang)
{
// something like...
// Session["Lang"] = lang;
return Redirect(Request.UrlReferrer.ToString());
}

How can I check in asp.net if checkbox was chcked?

How can I check in asp.net if checkbox was chcked?
Here the CheckBox:
<input type="checkbox" id="FootBallManager2013CheckBox" />
Since your markup is not using a CheckBox server control, I'll assume you want to check the input manually.
You have to give the input a name, otherwise it can't be posted. Call it, for example, name="FootBallManager2013CheckBox".
In the code-behind, look in Request.Form["FootBallManager2013CheckBox"] and see if it is non-blank. If the form element is present, it was checked; if it's not, it wasn't.
Example:
var footBallManager2013CheckBoxChecked = !string.IsNullOrEmpty(Request.Form["FootBallManager2013CheckBox"]);
Or, you could just use the server control, which is easier.
<asp:CheckBox runat="server" id="FootBallManager2013CheckBox" />
Code-behind:
if (FootBallManager2013CheckBox.Checked)
{
}
change html as
<input type="checkbox" id="FootBallManager2013CheckBox" RunAt="Server"/>
then on server side in code behind
if(FootBallManager2013CheckBox.Checked)
{
}
if(FootBallManager2013CheckBox.Checked)
{
}
you can tell by the checked property on the checkbox:
input type="checkbox" id="FootBallManager2013CheckBox" checked="checked"
or code wise (vb)
IF FootBallManager2013CheckBox.checked = checked then
[write what you want to do]
End IF

knockout.js boolean data-binding issues with radio buttons

I'm currently doing the following to compensate for boolean's not mapping well to radio buttons. I am stuck binding 1 and 0 to the value (instead of true and false) because of how the fields are read out of the observables. The value of Pref1/Pref2 come as true/false boolean values from the server. The key here is I want to not only data-bind the checked value of the radio button to match the true/false in the object, but I also want the boolean value of true/false to be written back into the GraduationClass object. My compensation code is not only ugly, but not scalable.
<input type="radio" value="1" name="radioGroup" data-bind="checked: Pref1" />Yes
<input type="radio" value="0" name="radioGroup" data-bind="checked: Pref2" />No
Save
function SiteSettingsViewModel() {
var self = this;
this.saveGraduationClass = function(graduationClass) {
// hack until i get a custom radio button binding
if (graduationClass.Pref1() == 1) {
graduationClass.Pref1(true);
} else {
graduationClass.Pref1(false);
}
if (graduationClass.Pref2() == 1) {
graduationClass.Pref2(true);
} else {
graduationClass.Pref2(false);
}
// ...ajax call to save graduationClass to the server
}
function GraduationClass(data) {
var self = this;
ko.mapping.fromJS(data, {}, this);
}
Here is example from knockoutJs website, that demonstrate how to use radio buttons with
"checked" attribute:
<p>Send me spam: <input type="checkbox" data-bind="checked: wantsSpam" /></p>
<div data-bind="visible: wantsSpam">
Preferred flavor of spam:
<div><input type="radio" name="flavorGroup" value="cherry" data-bind="checked: spamFlavor" /> Cherry</div>
<div><input type="radio" name="flavorGroup" value="almond" data-bind="checked: spamFlavor" /> Almond</div>
<div><input type="radio" name="flavorGroup" value="msg" data-bind="checked: spamFlavor" /> Monosodium Glutamate</div>
</div>
<script type="text/javascript">
var viewModel = {
wantsSpam: ko.observable(true),
spamFlavor: ko.observable("almond") // Initially selects only the Almond radio button
};
// ... then later ...
viewModel.spamFlavor("msg"); // Now only Monosodium Glutamate is checked
</script>
But I dont understand why you use two objects - "Pref1" and "Pref2" fro one radiobutton group "radioGroup"? In this case you just could use one object as in an example used "spamFlavor".
So, please, describe more ditaily what you want to bind: one radiobuttons group by one selected value, or something else.
Also you could use computed observables to calculate different values, please see example.

Form with many checkboxes, how to get a list of id's?

I have a form, each row has a checkbox on it like:
<input type=checkbox id="cb-<%= o.ID %>" name="mycheckboxes" />
When the form is posted to an action, how can I get a list of the checked checkbox's id values?
Is it possible since they all have the same name?
Store the value in the value attribute of the checkbox:
<input type="checkbox" value="<%= o.ID %>" name="mycheckboxes" />
In your action, tell ASP.NET MVC to look for an int[] with the name "mycheckboxes":
public ActionResult MyAction(int[] mycheckboxes)
{
// do stuff here
}
I believe you are looking in javascript
then Yes, use document.getElementsByName('') to get the list of all the checkbox and iterate thru them in the loop to find if its checked and get the ID.

Resources