How to set focus to h:inputText inside rich:dataTable as one column after pressing a4j:commandButton like (+) in footer of rich:dataTable? - jsf-1.2

I am using JSF 1.2 and Richfaces 3.3.3 in my sample JSF Application.
I have a form with rich:dataTable that having single column with h:inputText and footer having a4j:commandButton with value "add".
when I click "add" button then new row adding in rich:dataTable and that is working fine.
My need is after pressing "add" button the focus have to go newly created row to the h:inputText field...
sample code :
<rich:dataTable value="" var="">
<rich:column>
<f:facet name="header">
<h:outputText value="name" />
</f:facet>
<h:inputText id="name" value="#{obj.name}">
</rich:column>
<f:facet name="footer">
<a4j:commandButton value="add" action="{actionMethod}" />
</f:facet>
</rich:dataTable>
when I click "add" button then new row adding in rich:dataTable and that is working fine.
My need is after pressing "add" button the focus have to go newly created row to the h:inputText field...
How to do this using JQuery or java script, any idea?

Please find below the javascript for setting focus.
function setFocus() {
var displayedRowsCountValue = document
.getElementById("exampleManageBean:displayedRowsCountValue").value;
var firstRowIndexValue = document
.getElementById("exampleManageBean:firstRowIndexValueId").value;
displayedRowsCountValue = parseInt(firstRowIndexValue - 1)
+ parseInt(displayedRowsCountValue);
for (i = parseInt(firstRowIndexValue - 1); i < parseInt(displayedRowsCountValue); i++) {
var tmpComponent = "selectedId[" + i + "]";
var temp = (document.getElementsByName(tmpComponent)[1]);
if (temp != null && temp.checked == true) {
var temp1 = document
.getElementById("exampleManageBean:exampleTable:"
+ i + ":exampleTextBox");
temp1.focus();
}
}
}
The displayedRowsCountValue & firstRowIndexValueId should be taken from t:dataScroller

Related

How to set value to telerik:RadAutoCompleteBox from javascript?

I am using telerik:RadAutoCompleteBox as a input element.I want to set its value from javascript on the click event of the another button.The code for the element is as follows:
<telerik:RadAutoCompleteBox
runat="server"
ID="costCodeTxt"
Width="400"
OnClientRequesting="requesting"
DropDownWidth="300"
InputType="Text"
TextSettings-SelectionMode="Single"
OnClientTextChanged="VerifyJobCostCode">
<WebServiceSettings Path="~/QuantitySurvey/CostCode.asmx" Method="GetCostCodeList" />
</telerik:RadAutoCompleteBox>
This should work to get you to the dom element, allowing you to set the value.
function setText(text){
var domElement = $find("<%= costCodeTxt.ClientID %>").get_inputElement();
domElement.value = text;
}
You probably also want to set this to allow custom text
AllowCustomEntry = "true"

How do I avoid an asp postback when adding code dynamically via asp:Literal?

I've got a rather simple aspx page that fetches an html filename from a database, finds that file on a network share and adds it to a content area within the page. I've got a select element with an onchange attribute in the html file, but when I change the value of the select element the page is doing a postback, and of course once done the value reverts to it's initial state.
How can I prevent the server side code from firing when controls in the html have events that need to fire?
relevant aspx code:
<asp:Literal ID="htmlbody" runat="server"></asp:Literal>
<!--Jquery -->
<script src="ClientResources/vendor/js/jquery-1.10.2.js"></script>
<script src="ClientResources/vendor/jquery-ui-1.11.4.custom/jquery-ui.min.js"></script>
<!-- My javascript -->
<script src="ClientResources/lib/CoordSheet_Shared.js" type='text/javascript'></script>
C# code behind
...
using using HtmlAgilityPack;
...
protected void Page_Load(object sender, EventArgs e)
{
// In my actual code I get this filename from a database, but that doesn't matter to the problem
string fileName = "SomeFile.html";
if (fileName != null)
{
string filepath = System.IO.Path.Combine("\\\\Network\\Path\\", fileName);
var doc = new HtmlDocument();
doc.Load(filepath);
string bodyhtml = doc.DocumentNode.SelectSingleNode("//body").WriteContentTo();
htmlbody.Text = bodyhtml;
}
}
EDIT TO SHOW JAVASCRIPT CODE
The event is an inline onchange attribute
<select id="Coordsheet_PageHeight" onchange="Coordsheet_PaperSizeSelected(); Coordsheet_MakeLayoutPrintable();">
<option value="10" data-numslices="1"></option>
<option value="7.5" data-numslices="25">8.5" x 11" Landscape</option>
<option value="10" data-numslices="30">8.5" x 11" Portrait</option>
<option value="10" data-numslices="30" selected="">11" x 17" Landscape</option>
<option value="16" data-numslices="30">11" x 17" Portrait</option>
<option selected="selected" value="10" data-numslices="1">11" Plotter</option>
<option value="24" data-numslices="1">24" Plotter</option>
<option value="36" data-numslices="1">36" Plotter"</option>
<option value="Custom">Custom Height</option>
</select>
Here is the javascript code. Essentially it's splitting a large svg element into a series of slices that will word wrap when printed.
function Coordsheet_MakeLayoutPrintable(){
// This function divides the SVGMaster into slices that may be printed
// The new elements will be placed in the same div as the SVGMaster div
// This function resizes the layout SVG
var pageHeight = $('#Coordsheet_CustomPageHeight').val();
var numSlices = $('#Coordsheet_CustomSliceCount').val();
// Determine where to put the child divs
var holderDiv = document.getElementById('SVGSlicesDiv');
holderDiv.innerHTML = "";
// Get the attributes saved when the SVGMaster was created
var attribObj = document.getElementById('SVGAttribObj');
var width = parseFloat(attribObj.getAttribute('data-canvasWidth'))
var height = parseFloat(attribObj.getAttribute('data-canvasHeight'))
// Make a series of containers for the svg element in the result holder div
var blockWidth = width / numSlices;
var pageWidth = blockWidth * pageHeight/height;
for (var i = 0; i < numSlices; i++) {
x = i * blockWidth;
var s = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
s.innerHTML = '<use xlink:href="#SVGMaster">';
s.setAttribute("viewBox", x + " " + 0 + " " + (blockWidth) + " " + (height));
s.setAttribute("width", blockWidth);
s.setAttribute("height", height);
// Viewport size (size visible)
s.setAttribute("height", pageHeight + "in");
s.setAttribute("width", pageWidth + "in");
holderDiv.appendChild(s);
}
}
function Coordsheet_PaperSizeSelected(){
// This function is hit when the coordsheet size dropdown value is changed
var pageHeightSelection = $('#Coordsheet_PageHeight').val();
var numSlices = $('#Coordsheet_PageHeight option:selected').attr('data-numSlices');
if (pageHeightSelection == 'Custom'){
$('#Coordsheet_PaperSizeControlTable').show();
} else {
$('#Coordsheet_CustomPageHeight').val(pageHeightSelection);
$('#Coordsheet_CustomSliceCount').val(numSlices);
$('#Coordsheet_PaperSizeControlTable').hide();
}
}

Web Controls Re-Enabling After Being Disabled

Like the title says, there is code in place that disables a few Web Controls that are placed in forms that take user input. The input is disabled in the case that I am working on as there are only viewing the data and not able to edit it.
Here is the code:
//Disable Debtor Fields
//dpDebtorDOB.Enabled = false;
txtDebtorDOBYear.Enabled = false;
ddlDebtorDOBMonth.Enabled = false;
txtDebtorDOBDay.Enabled = false;
txtDebtorFirstName.Enabled = false;
txtDebtorLastName.Enabled = false;
txtDebtorAddress1.Enabled = false;
txtDebtorAddress2.Enabled = false;
txtDebtorCity.Enabled = false;
ddlDebtorProvince.Enabled = false;
txtDebtorPostalCode.Enabled = false;
txtDebtorPhoneNumber.Enabled = false;
// Disable Co-Debtor Fields
btnCopyDebtor.Visible = false;
btnClearCoDebtor.Visible = false;
//dpCoDebtorDOB.Enabled = false;
txtCoDebtorDOBYear.Enabled = false;
ddlCoDebtorDOBMonth.Enabled = false;
txtCoDebtorDOBDay.Enabled = false;
txtCoDebtorFirstName.Enabled = false;
txtCoDebtorLastName.Enabled = false;
txtCoDebtorAddress1.Enabled = false;
txtCoDebtorAddress2.Enabled = false;
txtCoDebtorCity.Enabled = false;
ddlCoDebtorProvince.Enabled = false;
txtCoDebtorPostalCode.Enabled = false;
txtCoDebtorPhoneNumber.Enabled = false;
All of the code that is supposed to disable the Co-Debtor fields does not take effect. When I enter the worksheet that has these fields disabled, they appear to be disabled but when the page finishes loading, the fields will become editable again. Also, if I un-comment the Debtor field code above, the Co-Debtor fields are disabled and stay disabled.
If anyone has any suggestions as to what could be the problem that'd be appreciated. So far I've looked within the asp designer to see if the text boxes are sharing properties, to see if on page loads the fields are getting re-enabled in other methods and have taken a look in the JavaScript as well to see if that might be the cause of it but I have found nothing.
To do it codewise you should do like you do. Example of disabling a textbox.
TextBox t = (TextBox)ctrl;
t.Enabled = false;
Have you looked at how the rendred page looks like in the pagesource AFTER BEING LOADED are there any attributes that say disabled?
The following elements support the disabled attribute:
button
input
optgroup
option
select
textarea
To disable a form control, add disabled="disabled" to the element. For example:
<input type="text" name="foo" value="bar" disabled="disabled" />
To re-enable the element, remove the disabled="disabled" attribute:
<input type="text" name="foo" value="bar" />
The following elements support the read-only attribute:
input
textarea
To make a form control read-only, add readonly="readonly" to the element. For example:
<input type="text" name="foo" value="bar" readonly="readonly" />
To allow the element to be written to, remove the readonly="readonly" attribute:
<input type="text" name="foo" value="bar" />

Get the selected item of dropdown list from code behind

How to get the selected value of a dropdownlist in code behind?
In my aspx page I have a dropdown list
Using Jquery, I'm adding items
var city = $("#city").val();
if (city.toString() != "") {
var citySelect = $('#cityName');
citySelect.append($('<option></option>').val('0').html('-Select City-'));
for (i = 0; i < City.response_list.length; i++) {
citySelect.append($('<option></option>').val(City.response_list[i].id_city + " - " + City.response_list[i].label).html(City.response_list[i].id_city + " - " + City.response_list[i].label));
}
}
In code behind when I try to get the selected value,
I'm getting "System.NullReferenceException: Object reference not set to an instance of an object"
How to get the selected value?
Your Select input which you add some options with JQuery to it, it is a Client side object and in no circumstances you can have access to it using code behind
To access this object you have to add to property to your select input
ID and runat=server thsese to property let you access your object from the code behind, you final select should be something like this
<select id="select1" runat="server">
<option value="val1">dfgbfbff</option>
</select>
then in your code behind you can check its values with checking the select1

Disable Google Toolbar Autofill

The Google Toolbar's autofill feature has been the bane of my web development existance for the past several years. I have always settled on trying to create a timer control to check for changes since the developers epically failed to fire change events on controls. This has gotten further and further complicated when controls are buried inside nested repeaters, and then trying to tie it to an UpdatePanel is a further complication.
Has anyone succesfully been able to prevent Google Toolbar from filling in form fields without renaming the field to something insignifcant? (note: This doesn't work for a 'State' dropdown, it even goes as far as to check field values).
For as smart as Google employees are supposed to be, this was a grandly moronic oversight.
Update: For those who may be coming here looking for a solution. What I have found to work so far is you have ASP.net, is to use the server control "Timer" and to set this control as a trigger for the UpdatePanel. It helps to loop through and check for changed values.
If you only have access to javascript, or are using another framework, then I found using the following function to work the best (I was trying to monitor state and zip changes. The focusElement is required because when hovering in a dropdownlist, it changes the selectedindex):
function MonitorChanges(sStateDropdownID, sZipCodeID, sHiddenStateFieldId, sHiddenZipFieldId, bDoPostback) {
var state = $('#' + sStateDropdownID).val();
var zip = $('#' + sZipCodeID).val();
var hiddenstate = $('#' + sHiddenStateFieldId).val();
var hiddenzip = $('#' + sHiddenZipFieldId).val();
$('#' + sHiddenStateFieldId).val(state);
$('#' + sHiddenZipFieldId).val(zip);
var compareString = state + zip;
var compareHiddenString = hiddenstate + hiddenzip;
var focusElement = getElementWithFocus();
if (compareString != compareHiddenString && isShippingZip(zip)) {
bDoPostback = true
}
if (parseInt(focusElement.id.search('drpState')) == -1 && parseInt(focusElement.id.search('txtZip')) == -1 && bDoPostback) { bDoPostback = false; __doPostBack(sStateDropdownID, ''); }
var f = function() { MonitorChanges(sStateDropdownID, sZipCodeID, sHiddenStateFieldId, sHiddenZipFieldId, bDoPostback); }
setTimeout(f, 1000);
}
According to a recent post by a Google developer, using the autocomplete="off" attribute will disable Google toolbar auto-completion in both IE and Firefox. Note that this attribute must be applied to the <form> tag and not the individual <input> tags:
<form method="post" action="http://example.com" autocomplete="off">
<!-- ... -->
</form>
While this is not an instant fix, it is probably the most reliable solution possible - if it is possible to wait until the next iteration of the Google toolbar.
I once have a problem with autofill in firefox. I did this to prevent it.
<div style="display:none">
<input type="text" name="user" />
<input type="password" name="password" />
</div>
<input type="text" name="user" />
<input type="password" name="password" />
Don't know if it also work with google autofill.
Just out of curiosity, does autofill still fire when you set the AutoCompleteType to disabled?
<asp:TextBox ID="textBox1" runat="server" AutoCompleteType="Disabled" />
I'm not entirely sure if this will work or not, but I recall coming across it as a possible solution.
Try breaking apart the labels of keywords with span tags.
<label for="firstName">Fi<span>rs</span>t N<span>am</span>e:</label>
<input id="firstName" name="firstName">
Also, supposedly Google is planning to support autocomplete="false" in Firefox. No clean IE solution, yet, though.

Resources