asp.net treeview by avoiding collapse and expandall - asp.net

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

Related

dynamically created controls within a usercontrol is not recognized

In the ascx.cs file I'm dynamically generating buttons. In .aspx file I add the control to the form. The control itself renders well, but when the buttons are clicked I get this error
An error has occurred because a control with id 'ctl03' could not be
located or a different control is assigned to the same ID after
postback.
DestopControl.ascx.cs
public partial class DesktopControl : PlaceHolder
{
public void Build()
{
for (int i = 0; i < 10; i++)
{
Button button = new Button()
{
Width = 50,
Height = 50,
ID = string.Format("button{0}", i),
Text = i.ToString()
};
button.Click+=new EventHandler(button_Click);
}
}
}
Default.aspx.cs
DesktopControl desktop = new DesktopControl();
desktop.Build();
MainContent.Controls.Add(desktop);
After reading the comments (little hard to read the code-part of the comments) it appears that yes, you are generating your controls inside an if(!isPostBack){}; well, looks like it's in the else part of that if statement.
You have to generate your controls every time the page posts back, as the page_load gets fired before your button click. So once the controls have been re-created the code will continue on to your button click handler, where the controls should be available to handle.
Essentially, take ReloadUI(Session["ui"]); OUT of the if(!isPostBack){}else{} statement. Put it after your if statement.
Like this:
if (!isPostBack){
// my first load code
}else{
// my postback code
}
// load all my dynamic controls here
ReloadUI(Session["ui"]);
Found a solution:
Every time there is a new UI I call this ClearScreen() which does the trick.
The error on 'ctl03' was a menu control which was generating it's own ID and somehow wasn't available on postback. I assigned an ID to it. But I guess all the issue went away with this ClearScreen() method.
private void ClearScreen()
{
try
{
List<Control> controls = new List<Control>();
foreach (Control control in MainContent.Controls)
{
controls.Add(control);
}
for (int i = 0; i < controls.Count; i++)
{
if (!(controls[i].GetType() == typeof(LiteralControl) || controls[i].GetType() == typeof(ScriptManager)))
{
MainContent.Controls.Remove(controls[i]);
}
}
}
catch (Exception ex)
{
}
}

Setting TabContainer Active Tab dynamically by visibility

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

how to find ID of the control which is present in defualt.aspx in different page defualt2.aspx

I have a web form which load 100 000 of data from the database.I Have 50 dropdown which is populated with respect to selectedindex change of dropdown .so to bind dropdown i am using ajax code .
I have written nearly about 200 line of ajax code in a separate js file.I am using 3 tier artitecture .I am not returning dataset from the bal class, am returning generic class to bind gridview.also i have created a class to bind the gridview.Also I am not using any update panel.
Is this approach will improve my performance.??
But there is a problem for me,i have to write code in js file to bind dropdown like this.
function GetAppStoreLnk(id) {
var txtnameid = document.getElementById(id);
CreateXmlHttp();
var requestUrl = "Default2.aspx?id="+txtnameid+"";
if (XmlHttp) {
XmlHttp.onreadystatechange = function() { getschemename(txtnameid) };
XmlHttp.open("GET", requestUrl, true);
XmlHttp.send(null);
}
}
function getschemename(id)
{
// To make sure receiving response data from server is completed
if(XmlHttp.readyState == 4) {
// To make sure valid response is received from the server, 200 means response received is OK
if(XmlHttp.status == 200) {
var strData = XmlHttp.responseText;
if(strData != "") {
var arrscheme = strData.split("|");
id.length = 0;
for(i=0; i<arrscheme.length-1; i++) {
var strscheme = arrscheme[i];
var arrschnm = strscheme.split("~");
id.options[i] = new Option();
id.options[i].value = arrschnm[0];
id.options[i].text = arrschnm[1];
}
} else {
id.length = 0;
id.options[0] = new Option();
id.options[0].value = "";
id.options[0].text = "Scheme Name is not available";
}
document.body.style.cursor = "auto";
}
else {
id.length = 0;
id.options[0] = new Option();
id.options[0].value = "";
id.options[0].text = "server is not ready";
document.body.style.cursor = "auto";
}
}
}
but if i make class to bind the dropdown this will reduce my js file code line .How will i find the ID of the dropdown in the different page ie Default2.aspx .
Please help me .
How will i find the ID of the dropdown in the different page ie Default2.aspx .??Also i want dont want to use usercontrol or masterpage.
I don't understand your question. You are trying to access the Asp.net drop down in page Default.aspx in the page Default2.aspx right?
Could you please clarify your requirement?

asp.net user control scroolIntoView is scrolling entire page

I there
I'm using an asp.net user control with a tree view.
When I load the page I want to scrool the user control to the selected node in the tree view.
I'm using js function .ScrollIntoView(true).
But this is scrolling the entire page (not only what is inside the user control)
here's my code
//js
function ScroolToFirstSelectedCheckBox(ctrlId) {
Event.observe(window, 'load', function() {
var tree = document.getElementById(ctrlId + '_MyTreeView');
var checkBoxes = tree.getElementsByTagName("input");
var checkBoxesCount = checkBoxes.length;
for (var i = 0; i < checkBoxesCount; i++) {
if (checkBoxes[i].checked) {
checkBoxes[i].scrollIntoView(true);
break;
}
}
}
);
}
//aspx.cs
Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
"ScrollToSelectedCheckBox",
string.Format("ScroolToFirstSelectedCheckBox('{0}')",
this.UniqueID),
true);
How can I keep the parent page scroll position but continue to set user controll position where I want?
Tks
ok, simply changed
checkBoxes[i].scrollIntoView(true);
with
checkBoxes[i].scrollIntoView(false);
That's it!!!
=P

auto Focus (Hit Enter) Javascript function is working good in IE7 but not working in IE8

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.

Resources