I have 3 ASP.NET textboxes and one HiddenField.
The value of the third textbox (this is disabled) depends on the values of the other two.
The formula is
txtPricepad = txtPrice/txtCarton
<asp:TextBox ID="txtPriceCase" runat="server" onblur="javascript:GetPricePerPad();></asp:TextBox>
<asp:TextBox ID="txtCarton" runat="server"></asp:TextBox>
<asp:TextBox ID="txtPricePad" Enabled="false" runat="server" ></asp:TextBox>
<asp:HiddenField ID="hdPricepad" runat="server"/>
function GetPricePerPad()
{
var priceCase = document.getElementById('ctl00_content_txtPriceCase').value;
var cartons = document.getElementById('ctl00_content_txtCarton').value;
var res = Number(priceCase) / Number(cartons);
document.getElementById('ctl00_content_txtPricePad').value = res;
document.getElementById('ctl00_content_hdPricepad').value = res;
}
Assuming that the initial value of txtPricePad is 0 and txtCarton is 12.
When the value of txtPrice is changed to 1200, GetPricePerPad() will be called, thus txtPricePad will be 100.
Javascript successfully changed the txtPricePad's value to 100 but when I am calling txtPricePad from the codebehind, its value is still 0.
That's why I assigned also the result of the formula to a HiddenField. Are there other ways to do this? I do not want to use HiddenField again.
Liz, is it possible for you to make the text field readonly (ReadOnly=True) as opposed to making it Disabled=True? The reason being that when a text field is disabled is not submitted with the form in the POST request. See this question.
If you want to make it look as if it was disabled, I guess you can apply a CssClass to the button.
I would use one of 2 options
Perform the calculation again server side from the other inputs or
Use javascript to enable the txtPricePad field on form submit, see below
var pricePad = document.getElementById(<%=txtPricePad.ClientID%>);
pricePad.disabled = false;
Related
I have a GridView that is filtered by a TextBox. It filters when I leave the control. How do I fire the filter when the TextBox changes?
I set the OnChange event and looked up the value to see if it was at least 3 characters long. If it was I manually called the PostBack:
<asp:TextBox ID="_txtEquipment" runat="server" AutoPostBack="True"
onkeyup="checkforEquipmentNumber();"/>
JavaScript Code:
function checkforEquipmentNumber() {
var txtEquipmentNumber = document.getElementById("_txtEquipment").value;
if (txtEquipmentNumber.length > 2) {
javascript:__doPostBack("_txtEquipment",'');
}
}
I have added textbox on the page that Jquery to create a datepicker. The problem is that, the textbox doesn't hold the value after a postback. After researching, I found the following solution which works perfectly, i.e. the textbox keeps its value after a postback.
<th>
<asp:CustomValidator ID="customStartDate" runat="server"
ErrorMessage="Start Date" Display = "None" ControlToValidate = "txtStartDate"
ValidationGroup ="HireGroup" ClientValidationFunction ="StartDate_Validate"/>
Start Date:
</th>
<td>
<asp:TextBox ID="txtStartDate" runat="server" Width = "140" ReadOnly = "true"
TabIndex = "5" CssClass = "datepicker" ></asp:TextBox>
<asp:HiddenField ID="hfDatePicker" runat="server"/>
</td>
And this is the Jquery code
//Set datePicker
function SetUpDatePicker() {
var $allDatepickers = $('.datepicker');
$.each($allDatepickers, function () {
$(this).datepicker({
showOn: "button",
buttonImage: "Images/calendar.gif",
buttonImageOnly: true,
minDate: 1,
altField: '[id*="hfDatePicker"]'
});
var $hfDatePicker = $('[id*="hfDatePicker"]');
var val = $($hfDatePicker).attr('Value');
$(this).val(val);
var len = $($hfDatePicker).attr('Value').length;
if (len > 0) {
$(this).datepicker("setDate", new Date($($hfDatePicker).attr("Value")));
}
});
}
Now I have a different type of problem. I can't use a RequiredFieldValidator for a HiddenField as I am getting an error "Hidden Field cannot be validate".
I'm tryind a CustomValidator, but the problem is that this control does acts only when the ControlToValidate is not empty.
I've checked all the property for RequiredFieldValidator and don't see something like ClientValidationFunction property.
Any suggestion on how to solve that problem?
(Based on the comment by #Richard77, I will make this an actual answer.)
You have a several options...
Instead of using a <asp:Hidden>, use a normal <asp:TextBox> but hide it using style='display:none; attribute. This will allow you to use the <asp:RequiredFieldValidator> as per your needs.
Another way to do it is using the <asp:CustomValidator> and add the ValidateEmptyText='true' attribute. This will force the validator to run the code even when the TextBox is empty.
Update - after thinking about this, I would NOT recommend the following, because it's not possible (that I can think of) to override the server-side version of the function, and therefore will leave you open to vulnerabilities. It's fine to do if you're purely using it for say visual reasons, and don't need the actual data to be checked on the server - however, this is an unusual situation.
A final option (but not one that I would necessarily recommend) is to override the function generated by ASP.NET. This would need to be placed on your page somewhere after the script link generated by ASP.NET, something like...
function RequiredFieldValidatorEvaluateIsValid(val) {
if(val.controltovalidate=="myValidatorId"){
// your coding here
} else {
return (ValidatorTrim(ValidatorGetValue(val.controltovalidate)) != ValidatorTrim(val.initialvalue))
}
}
How to get the textbox value on code-behind after the textbox value assigned on a modal-box?
This is how the modal-box appear on the page:
<div id="dialog-form" title="Modal Box">
<asp:TextBox ID="Textbox1" runat="server" ReadOnly="True">
<asp:Button ID="Save_Button" runat="server" Text="Save"></asp:Button>
</div>
In order to assign the value on Textbox1, I attach a Javascript function to the LinkButton on a Gridview (the code not showed here) through code-behind based on the LinkButton clicked:
Dim myLinkButton As LinkButton
For i As Integer = 0 To GV1.Rows.Count - 1
myLinkButton = DirectCast(GV1.Rows(i).Cells(1).FindControl("LinkButton"), LinkButton)
myLinkButton.Attributes.Add("onclick", "shopModalPopup('" + .Rows(i).Cells(0).Text & "'); return false;")
Next
Rows(i).Cells(0) is the first column on the Gridview, it is "ID". This ID will be assigned to the Textbox1 while the Linkbutton clicked.
The Javascript code is on the same page as the Gridview code:
<script>
var grid_modal_options = {
height: 450,
width: 550,
modal: true
};
function shopModalPopup(id) {
var DataField = id;
grid_modal_options.open = function () {
$('#dialog-form #Textbox1').val(DataField);
};
$("#dialog-form").dialog(grid_modal_options);
$("#dialog-form").parent().appendTo('form:first');
}
</script>
When I clicked the Save Button on the modal-box, the value on the Textbox1 can't catch on the code-behind. It always return null value. How to do that? Thank you very much.
please read this
http://www.codeproject.com/Articles/37090/JQuery-UI-Dialog-with-ASP-NET-empty-post-values
i think it might help you the problem might be that the div of dialog is moved outside the form you can solve it by using reference above or just defining another hidden input and update the hidden input with the same value
$('#dialog-form #Textbox1').val(DataField);
$('hiddenfield').val(DataField);
and then read the value from the hidden field
not sure if it the best answer but might work as a quick fix
Does it work without Readonly="true"? If so, in your <asp:textbox>, replace Readonly="true" with Enabled="false".
I haven't tested this, but i recalled Readonly causing me issues like this in the past.
Hey I have a radiobuttonlist and trying to set one of the radiobuttons to selected based on a session variable but proving impossible.
<asp:radiobuttonlist id="radio1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged">
<asp:listitem id="option1" runat="server" value="All"/>
<asp:listitem id="option2" runat="server" value="1" />
<asp:listitem id="option3" runat="server" value="2" />
</asp:radiobuttonlist>
I.e How can I set option2 to selected in code behind ?
The best option, in my opinion, is to use the Value property for the ListItem, which is available in the RadioButtonList.
I must remark that ListItem does NOT have an ID property.
So, in your case, to select the second element (option2) that would be:
// SelectedValue expects a string
radio1.SelectedValue = "1";
Alternatively, yet in very much the same vein you may supply an int to SelectedIndex.
// SelectedIndex expects an int, and are identified in the same order as they are added to the List starting with 0.
radio1.SelectedIndex = 1;
You could do:
radio1.SelectedIndex = 1;
But this is the most simple form and would most likely become problematic as your UI grows. Say, for instance, if a team member inserts an item in the RadioButtonList above option2 but doesn't know we use magic numbers in code-behind to select - now the app selects the wrong index!
Maybe you want to look into using FindControl in order to determine the ListItem actually required, by name, and selecting appropriately. For instance:
//omitting possible null reference checks...
var wantedOption = radio1.FindControl("option2").Selected = true;
Try this option:
radio1.Items.FindByValue("1").Selected = true;
We can change the item by value, here is the trick:
radio1.ClearSelection();
radio1.Items.FindByValue("1").Selected = true;// 1 is the value of option2
var rad_id = document.getElementById('<%=radio_btn_lst.ClientID %>');
var radio = rad_id.getElementsByTagName("input");
radio[0].checked = true;
//this for javascript in asp.net try this in .aspx page
// if you select other radiobutton increase [0] to [1] or [2] like this
For my ASP.NET page, in the code behind I load various items/options into a DropDownList but I do not set a default by assigning SelectedIndex. I notice for the postback SelectedIndex is set to 0, even if I never set focus to or changed the value in the DropDownList.
If not otherwise specified in the code behind or markup, will a default value of 0 be used for the SelectedIndex in a postback with a DropDownList? If yes, is this part of the HTML standard for selection lists, or is this just the way it is implemented for ASP.NET?
This is normal behavior for the HTML SELECT control.
Unless the selected property is used, it will always start at the first item (index of 0).
In many cases, I don't want this in ASP.NET because I specifically want the user to select an option.
So what I do is add a blank item which I can then validate.
i.e.
<asp:DropDownList runat="server" DataSourceID="SomeDS" AppendDataBoundItems="true"> // Notice the last property
<asp:ListItem Text="Please select an option" Value="" />
</asp:DropDownList>
This way, I can set a validator which checks if the value is blank, forcing the user to make a selection.
For anybody looking for a way to do this from the code behind, you can add the extra list item there by using the Insert function and specifying an Index of 0, as mentioned in this SO Question. Just make sure to do so after you've called DataBind()
myDropDownList.DataSource = DataAccess.GetDropDownItems(); // Psuedo Code
myDropDownList.DataTextField = "Value";
myDropDownList.DataValueField = "Id";
myDropDownList.DataBind();
myDropDownList.Items.Insert(0, new ListItem("Please select", ""));