Tuesday, November 15, 2011

Position cursor after all contents in Asp.Net TextBox

<asp:TextBox ID="TextBox1" runat="server"
onfocus="FocusAtLast();"
 ></asp:TextBox>

    function FocusAtLast () {
       
    var inputField = document.getElementById('<%= TextBox1.ClientID %>');
    if (inputField != null && inputField.value.length> 0)
    {
    if (inputField.createTextRange)
    {
    var FieldRange = inputField.createTextRange();
    FieldRange.moveStart('character',inputField.value.length);
    FieldRange.collapse();
    FieldRange.select();
    }
    }
    }

Reference:

http://msdn.microsoft.com/en-us/library/ms535872%28v=VS.85%29.aspx

Wednesday, November 9, 2011

Sorting in Datatable using C#


DataTable dt = dt.Select("[anycondition]", "[column] desc", DataViewRowState.CurrentRows).CopyToDataTable();

Tuesday, November 8, 2011

Export Tree view into excel without using COM Objects


This code is used to export all tree nodes from telerik tree view. we can also implement to normal tree view.

Tuesday, November 1, 2011

DIV drag drop in JQuery

I come across one scenario that I need to create a pop up which should be draggable.
So I created one popup using JQuery.

Important thing is that i am using update panel in my page. i assigned one css class at design time.
my need is pop up should always open at top of page. if i drag and drop div at page bottom and close the div means , next it is opening only at bottom.

So in order to solve this problem i assign all css properties at run time in javascript on particular control on click event.

you can download jquery files from following links

http://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js
http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/jquery-ui.min.js

Please add script references inside form tag.


Page Design:


function OpenPopup() {
var object = document.getElementById("makeMeDraggable");
object.style.display = "block";
object.style.borderWidth = "2px";
object.style.borderCollapse = "collapse";
object.style.borderStyle = "solid";
object.style.borderColor = "black";
object.style.zIndex = "71";
object.style.position = "fixed";
object.style.width = "75%";
object.style.top = "10px";
object.style.left = "10%";
object.style.cursor = "move";
object.style.backgroundColor = "Red";
}


<form id="form1" runat="server">
<script src="../Scripts/jquery.min.js" type="text/javascript"></script>
<script src="../Scripts/jquery-ui.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(init); function init()
{ $('#makeMeDraggable').draggable(); }
</script>
<a onclick="OpenPopup();">Click here</a>
   
<div id="makeMeDraggable" style="display: none">
<div>
//Add your design here
// add any controls like grid to load dynamic data
</div>
</div>
</form>

Monday, October 31, 2011

Invoking Controls and function in page and iframes in javascript

How to access child window from parent window?

We can access the child window elements by using the window handle that will be returned by the window.open() method, as shown below:
winobj=window.open(..)
...
winobj.document.getElementById("elementname").value=""

The winobj is actually a window object instance of the newly created window


How to access parent window from child window ?
window.opener.document.getElementById('text1').value="Value changed.." ;
window.opener.location.reload

How to call parent page from iframe?

Window.parent.functionName();
window.parent.document.getElementId(...);

var winobj=window.open(..); // this is for opening new child window
winobj.document.getElementById("elementname").value="";

How to call iframe function from parent page?
Assume your iFrame's id is "targetFrame" and the function you want to call is targetFunction():
document.getElementById('targetFrame').contentWindow.targetFunction();
You can also access the frame using window.frames instead of document.getElementById.

or

window.frames.iframeName.functionName();


How to call parent page from user control?

To access function inside parent page

Window.parent.functionName();
To access controls inside parent page

window.parent.document.getElementId(...);

How to open new window inside user control?


ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "windowopen", "window.open('www.google.com', 'newwin');", true);

How to restrict accidential navigation from entry page?(On Before Unload in JavaScript)

One of common scenario is that , if we are entering huge data or important data in particular aspx page there may be a chance that accidentally we click on something which causes to navigate another page. so certainly we will lose entered data. To avoid this we must implement, some concepts. one of the helping concept is explained below.

This must be remedied in some way, if you are creating an application that needs the user to input stuff, especially lengthy articles. One approach is to automate draft saving every a few minutes, the other is to warn the user before he or she either intentionally or inadvertently navigate away from the editor page. You can achieve this by a straightforward snippet of JavaScript:
var warning = false;
window.onbeforeunload = function() {
  if (warning) {
    return 'You have unsaved changes.';
  }
}
The warning variable can be timely altered according to the editing activities by the user. For instance, it is set true if the editing field is changed or does not equal to default value, or if the user triggers the onKeyUp / onChange event of any input fields:
var editor_form = document.getElementById("form_id");
for (var i = 0; i < editor_form.length; i++) {
        editor_form[i].onchange = function() {
               warning = true;
        }
}
This snippet must be put at the end of the page. Also, to avoid the warning when you press the form submit button, add this snippet after that:
document.getElementById("submit_button_id").onclick = function() {
    warning = false;
}

Better avoid anchor tag or link button in the page unless it is important.