I'm using AjaxControlToolkit TabContainer and dynamically setting TabPanel visiblility. When active tab is is hidden (dynamically) the whole Tab Control is hidden, so to avoid this I've written small method like this (setting first visible tab as Active Tab)
private void SetActiveTab()
{
if (tabControl1.Tabs[0].Visible)
{
tabControl1.ActiveTabIndex =0;
return;
}
if (tabControl1.Tabs[1].Visible)
{
tabControl1.ActiveTabIndex = 1;
return;
}
...
}
But this feels like but inefficient/ugly code for me..., Is there a better way of doing this...?
Choose any
for (int tabIndex = 0; tabIndex < TabContainer1.Tabs.Count; tabIndex++)
{
if(TabContainer1.Tabs[tabIndex].Visible)
{
TabContainer1.ActiveTabIndex = tabIndex;
break;
}
}
foreach (TabPanel tab in TabContainer1.Tabs)
{
if (tab.Visible)
{
TabContainer1.ActiveTab = tab;
break;
}
}
var firstVisibleTab = TabContainer1.Tabs.OfType<TabPanel>().FirstOrDefault(tab => tab.Visible);
if (firstVisibleTab != null)
{
TabContainer1.ActiveTab = firstVisibleTab;
}
BTW for such quetions better use Code Review site: Code Review
Related
Actually I am checking the excel values whether they are displayed on the web page Mouse hover menu. The menu includes titles and the menu item links. The menu items in main menu and the sub links are in child menu. When I tried to collect all the links, I am getting them separately by finding with CSS. I want to get all the links in one array. I have used the below commands. Please help me how to get all the elements in one array. The developers code is attached as screen shots :
String AdminRoleAccessfilepath = "D:\\PMC
Automation\\AdminRoleAccess.xls";
FileInputStream AdminRoleFile=new
FileInputStream(AdminRoleAccessfilepath);
Workbook rwb=Workbook.getWorkbook(AdminRoleFile);
Sheet AdminRolesheet=rwb.getSheet("AdminRole");
int submenucolumnsize=AdminRolesheet.getRows();
int menucolumnsize=AdminRolesheet.getRows();
String[] arraysubmenu=new String[submenucolumnsize];
String[] arraymenu=new String[menucolumnsize];
List<WebElement>SubTitlelist =
driver.findElements(By.cssSelector(".child_menu>a "));
List<String> allSubTitleslist=new ArrayList<>();
List<WebElement>Titleslist =
driver.findElements(By.cssSelector("#menu_nav>ul>li>a "));
int submenuui = 0;
for (int a=1;a<submenucolumnsize;a++)
{
arraysubmenu[a] = AdminRolesheet.getCell(1,a).getContents();
//System.out.println(arraymenu[a]);
boolean isthere = true;
for(submenuui=0; submenuui<SubTitlelist.size(); submenuui++)
{
if
(!(arraysubmenu[a]).equalsIgnoreCase
(SubTitlelist.get(submenuui).getText()))
{
isthere = false;
}
else
{
isthere = true;
break;
}
}
if(isthere)
System.out.println("\nThe Sub Menu item in the UI-> "
+arraysubmenu[a]+ " existing on the Web Page" );
else
System.out.println("\nThe Sub Menu item in the UI-> "
+arraysubmenu[a]+ " not existing on the Web Page");
}
int menuui = 0;
for (int b=1;b<menucolumnsize;b++)
{
arraymenu[b] = AdminRolesheet.getCell(1,b).getContents();
//System.out.println(arraymenu[a]);
boolean isthere = true;
for(menuui=0; menuui<Titleslist.size(); menuui++)
{
if (!(arraymenu[b]).equalsIgnoreCase
(Titleslist.get(menuui).getText()))
{
isthere = false;
}
else
{
isthere = true;
break;
}
}
if(isthere)
System.out.println("\nThe Title in the Menu in the UI-> "
+arraymenu[b]+ " existing on the Web Page" );
else
System.out.println("\nThe Title in the Menu in the UI-> "
+arraymenu[b]+ " not existing on the Web Page");
}
Combine the selectors into one CSS using a comma.
'.child_menu>a , #menu_nav>ul>li>a'
I hope someone knows the answer to this as I am very new to using Telerik controls. Here is the problem, I have a requirement that says that I have to set the background color for a control attached to a validation control if the IsValid flag was set on the validation control. An earlier requirement that I had may be affecting this as well, it was set focus to the control based on the SetFocusOnError="true".
All of the controls are contained in a asp:UpdatePanel and the page has a master page set.
So what I have done is the following to set focus I overrode the Validate function on the System.Web.UI.Page class as so:
public override void Validate(string group)
{
base.Validate(group);
// get the first validator that failed
var validator = GetValidators(group)
.OfType<BaseValidator>()
.FirstOrDefault(v => !v.IsValid);
// set the focus to the control
// that the validator targets
if (validator != null)
{
//Check to see if SetFocusOnError was set.
if (validator.SetFocusOnError == true)
{
Control target = validator
.NamingContainer
.FindControl(validator.ControlToValidate);
if (target != null)
target.Focus();
}
}
}
This works and sets focus to the control. The next thing that I did was the following inline in my webpage:
var OriginalValidatorUpdateDisplay = null;
if (typeof (ValidatorUpdateDisplay) == 'function') {
OriginalValidatorUpdateDisplay = ValidatorUpdateDisplay;
ValidatorUpdateDisplay = NewValidatorUpdateDisplay;
}
function NewValidatorUpdateDisplay(val) {
OriginalValidatorUpdateDisplay(val);
ValidateControls();
}
function ValidateControls() {
if (window.Page_Validators) {
for (var vI = 0; vI < Page_Validators.length; vI++) {
var vValidator = Page_Validators[vI];
if (vValidator.isvalid) {
$("#" + vValidator.controltovalidate).removeClass("error");
if ($("#" + vValidator.controltovalidate).parent().find('.riTextBox').length > 0) {
$("#" + vValidator.controltovalidate).parent().removeClass("error");
}
}
else {
$("#" + vValidator.controltovalidate).addClass("error");
if ($("#" + vValidator.controltovalidate).parent().find('.riTextBox').length > 0) {
$("#" + vValidator.controltovalidate).parent().addClass("error");
}
}
}
}
}
However, when you load the page and submit the page for validation the first time the css doesn't change, but if you do it again it does. Oh and the AddClass is just adding
.error .riTextBox
{
background-color: lightpink !important;
z-index:6001;
}
to the control.
Has anyone worked encountered this before?
Oh I should also note that if I remove the Telerik controls and use asp textbox controls everything works as it should but I can't remove the Telerik controls from the project.
I used a javascript FocusChange() in my aspx page. I have couple of controls and I need Hit enter key need to move next control based on tab index. It is working good in IE7 but not working in IE8... Please help me on this..
Thanks for your help in advance. The java script is given below.
function FocusChange() {
if (window.event.keyCode == 13) {
var formLength = document.form1.length; // Get number of elements in the form
var src = window.event.srcElement; // Gets the field having focus
var currentTabIndex = src.getAttribute('tabindex'); // Gets its tabindex
// scroll through all form elements and set focus in field having next tabindex
for (var i = 0; i < formLength; i++) {
if (document.form1.elements[i].getAttribute('tabindex') == currentTabIndex + 1) {
for (var j = i; j <= formLength; j++) {
if (document.form1.elements[j].disabled == false) {
document.form1.elements[j].focus();
event.returnValue = false;
event.cancel = true;
return;
}
}
}
}
}
}
I've got the same request as you, but solved it in a different manner, just replacing the Enter for Tab
<script language="JavaScript">
document.onkeydown = myOnkeydown;
function myOnkeydown()
{
var key = window.event.keyCode;
if (key == 13) //13 is the keycode of the 'Enter' key
{window.event.keyCode = 9; //9 is the code for the 'Tab' key. Essentially replacing the Enter with the Tab.
}
}
</script>
Warning: Works in IE only.
I have four controls in a page with update panel. Initially mouse focus is set to first control. When I partially post back the page to server the focus automatically moves to first control from the last focused control from the control I have tabbed down to. Is there any way to maintain the last focus?
Take a look at Restoring Lost Focus in the Update Panel with Auto Post-Back Controls:
The basic idea behind the solution is to save the ID of the control
with input focus before the update panel is updated and set input
focus back to that control after the update panel is updated.
I come with the following JavaScript which restores the lost focus in
the update panel.
var lastFocusedControlId = "";
function focusHandler(e) {
document.activeElement = e.originalTarget;
}
function appInit() {
if (typeof(window.addEventListener) !== "undefined") {
window.addEventListener("focus", focusHandler, true);
}
Sys.WebForms.PageRequestManager.getInstance().add_pageLoading(pageLoadingHandler);
Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(pageLoadedHandler);
}
function pageLoadingHandler(sender, args) {
lastFocusedControlId = typeof(document.activeElement) === "undefined"
? "" : document.activeElement.id;
}
function focusControl(targetControl) {
if (Sys.Browser.agent === Sys.Browser.InternetExplorer) {
var focusTarget = targetControl;
if (focusTarget && (typeof(focusTarget.contentEditable) !== "undefined")) {
oldContentEditableSetting = focusTarget.contentEditable;
focusTarget.contentEditable = false;
}
else {
focusTarget = null;
}
targetControl.focus();
if (focusTarget) {
focusTarget.contentEditable = oldContentEditableSetting;
}
}
else {
targetControl.focus();
}
}
function pageLoadedHandler(sender, args) {
if (typeof(lastFocusedControlId) !== "undefined" && lastFocusedControlId != "") {
var newFocused = $get(lastFocusedControlId);
if (newFocused) {
focusControl(newFocused);
}
}
}
Sys.Application.add_init(appInit);
I find this more elegant:
(function(){
var focusElement;
function restoreFocus(){
if(focusElement){
if(focusElement.id){
$('#'+focusElement.id).focus();
} else {
$(focusElement).focus();
}
}
}
$(document).ready(function () {
$(document).on('focusin', function(objectData){
focusElement = objectData.currentTarget.activeElement;
});
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(restoreFocus);
});
})();
I am using ASP.NET with C# 2.0 and Visual Studio 2005. I am using a Master page and content pages. I have a treeview menu in the master page and when a user selects any menu item I redirect to that content page.
My problem is that after a user navigates to the content page all the treenodes refresh and the structure is collapsed. I want the selected treenode to stay expanded.
Can anybody help me out?
Are you using the treeview inside any UpdatePanel? Actually UpdatePanel does not support TreeView. I however managed the same using a lot of additional codes. You can see most of them on http://www.geekays.net/post/Using-TreeView-inside-AJAX-UpdatePanel.aspx and another post on the same site: http://www.geekays.net/post/TreeView-control-postbacks-on-check-and-uncheck-of-the-nodes-Checkbox.aspx
I added javascripts like the following also to scroll to a tree node that was selected, but the success was poor:
function scrollSelectedTviewNodeToDisplay(){
try{
var inpSelectedNode = document.getElementById("ctl00_contRMSMaster_TViewDeviceHeirarchy_SelectedNode");
var divTree = document.getElementById("ctl00_contRMSMaster_TViewDeviceHeirarchy");
if (inpSelectedNode.value != "")
{
var objScroll = document.getElementById(inpSelectedNode.value);
//my treeview is contained in a scrollable div element
var posY =findPosY(objScroll);
//alert(posY);
if (divTree){
divTree.scrollTop = posY;
//alert(divTree.nodeType);
}
//this works as well bu, but there is not as much control over the y position
//document.all(inpSelectedNode.value).scrollIntoView(true);
}
}
catch(oException)
{
//alert(document.getElementById("ctl00_contRMSMaster_divTree"));
}
}
function findPosX(obj){
var curleft = 0;
if (obj.offsetParent)
{
while (obj.offsetParent)
{
curleft += obj.offsetLeft
obj = obj.offsetParent;
}
}
else if (obj.x)
curleft += obj.x;
return curleft;
}
function findPosY(obj){
var curtop = 0;
if (obj.offsetParent)
{
while (obj.offsetParent)
{
curtop += obj.offsetTop
obj = obj.offsetParent;
}
}
else if (obj.y)
curtop += obj.y;
return curtop;
}