Thursday, December 16, 2010

JavaScript Basics

javascript to check opened window is closed or not

var win = window.open('http://www.yahoo.com');
alert(win.closed);

return boolean

javascript view webpages' HTML source

<input type="button" value="View" onclick="window.location = 'view-source:'+ 'http://www.royh.cn/'" />
Firefox only?

javascript get user selected text

IE version:
document.selection.createRange().duplicate().text;

Firefox version:
document.getSelection();

For all browser selected():
<input type="button" onclick="alert(selected())" />
<script type="text/javascript">
    function selected(){
        if (document.selection) return document.selection.createRange().duplicate().text;
        else return document.getSelection();
    }
</script>


 

Clipboard(Copy and Paste) in ASP.NET

javascript clipboardData Description

 

The clipboardData object (accessible as a property of a window or frame object) is a temporary container that scripts in IE 5 and later for Windows can use to transfer text data, particularly during script-controlled operations that simulate cutting, copying, and pasting, or that control dragging. Your script controls what data is stored in the clipboardData object, such as just the text of an element, an element's entire HTML, or the URL of an image. For example, a page for children could display simple icon images of several different kinds of animals. If the user starts dragging the dog icon, the script initiated by the img element's onDragStart event handler stores a custom attribute value of that element (perhaps the URL of a pretty dog photo) into the clipboardData object. When the user drops the icon into the designated area, the onDrop event handler's function reads the clipboardData object's data and loads the photo image into position on the page.
Data stored in this object survives navigation to other pages within the same domain and protocol. Therefore, you can use it to pass text data (including arrays that have been converted to strings by the Array.join( ) method) from one page to another without using cookies or location.search strings. But this is not the system clipboard (for security reasons).
For more information on transferring data via this object and the event.dataTransfer object, visit http://msdn.microsoft.com/workshop/author/datatransfer/overview.asp.
HTML Equivalent
None.
Object Model Reference
[window.]clipboardData
Object-Specific Properties
dropEffecteffectAllowed
Object-Specific Methods
clearData( )getData( )setData( )
Object-Specific Event Handler Properties
None.

 


javascript clipboardData setData( )

 

setData(dataFormat, stringData)
  
Stores string data in the clipboardData object. Returns Boolean true if the assignment is successful
Parameters

dataFormat
A string specifying the format for the data to be read. Earlier plans to allow multiple data types appear to have fallen through. As of IE 6, the only reliable format is Text. While the method accepts URL as a format, reading a set value in that format is not successful.
stringData
Any string value, including strings that contain HTML tags.
Returned Value
Boolean value: true | false.

javascript clipboardData getData( )

getData(dataFormat)
  
Returns a copy of data from the clipboardData object. The clipboardData contents remain intact for subsequent reading in other script statements.
Parameters

dataFormat
A string specifying the format for the data to be read. Earlier plans to allow multiple data types appear to have fallen through. As of IE 6, the only reliable format is Text.
Returned Value
String.

javascript clipboardData clearData( )

clearData([dataFormat])
  
Removes data from the clipboardData object.
Parameters

dataFormat
An optional string specifying a single format for the data to be removed. Earlier plans to allow multiple data types appear to have fallen through. As of IE 6, the only reliable format is Text. Omitting the parameter removes all data of all types.
Returned Value
None.

javascript clipboardData dropEffect, effectAllowed

These two properties belong to the clipboardData object by inheritance from the dataTransfer object, to which they genuinely apply. Ignore these properties for the clipboardData object.

 Example :

function SaveClipboardData(con) //Copy Clipboard Data from control... con is the control
        {
           var data = con.innerText ? con.innerText : con.textContent;
           window.clipboardData.setData("Text", data);
           alert(data);
        }
        function CopyToTextBox() //Inserting ClipBoard Data into TextBox
        {
            var data = window.clipboardData.getData("Text");
            alert("Clipboard Paste = \n\n" + data);
            var txt = document.getElementById('<%= txtEmailBody.ClientID %>');//TextBox
            var mosPos = 0;
            if (document.selection)
            {
                txt.focus();
                var range = document.selection.createRange();
                range.text = data;
            }
            else if (txt.selectionStart != null)
            {
                mosPos = txt.selectionStart;
                var strFirst = txt.value.substring(0, mosPos);
                var strLast = txt.value.substring(mosPos);
                if (txt.value == "")
                {
                    txt.value = data;
                    txt.focus();
                    con.className = "variable_selected";
                }
                else
                {
                    txt.value = strFirst + data + strLast;
                    txt.focus();
                    con.className = "variable_selected";
                }
            }
        }


 

 

 


Tuesday, December 14, 2010

Datalist Customization by using Radiobuttons and Hyperlink by emailid requirement

here i need to design the following manner

Radiobutton To CC EmpRole-EmpFirsname EmpLastname(empemail@emp.com)
check the following image

Test.aspx:
==========
 <asp:DataList runat="server" ID="emailaddress" Width="433px">
                            <ItemTemplate>
                                <asp:CheckBox ID="ToEmailCheck" runat="server" GroupName="rdbEmailAddress" AutoPostBack="true" OnCheckedChanged="EmailCheck_CheckedChanged"
                                    EnableViewState="False" />
                                <asp:Label ID="lblRoleCode" runat="server"
                                    Text='<%# Eval("cpx_cpo_code","-")%>' />
                                <asp:Label ID="lblFirstname" runat="server" Text='<%#Eval("ind_first_name") %>' />
                                <asp:Label ID="lblLastname" runat="server" Text='<%#Eval("ind_last_name") %>' />
                                <asp:HyperLink ID="TomailAddress" runat="server" Font-Underline="True" NavigateUrl='<%# Eval("cst_eml_address_dn", "mailto:{0}") %>'
                                    Text='<%# Eval("cst_eml_address_dn", "({0})") %>' />
                            </ItemTemplate>
                        </asp:DataList>

here my requirement is i need to add the email address of the employees to the TOTextbox but it not fall in the CCTextbox


Test.aspx.cs:
=============
protected void EmailCheck_CheckedChanged(object sender, EventArgs e)
        {
            //txtCC.Text = string.Empty;
            //txtTo.Text = string.Empty;
            for (int i = 0; i < this.emailaddress.Items.Count; i++)
            {
                if (((CheckBox)emailaddress.Items[i].FindControl("CCEmailCheck")).Checked == true &&
                    ((CheckBox)emailaddress.Items[i].FindControl("ToEmailCheck")).Checked == false)
                {
                    // add to list
                    if (txtCC.Text.Length > 0)
                    {
                        string remove = txtTo.Text.Replace(((HyperLink)emailaddress.Items[i].FindControl("TomailAddress")).Text.ToString(), "");
                        txtTo.Text = remove;
                        txtCC.Text += "," + ((HyperLink)emailaddress.Items[i].FindControl("TomailAddress")).Text.ToString().TrimStart('(');
                        txtCC.Text = txtCC.Text.TrimEnd(')');
                        txtCC.Text = txtCC.Text.ToString();
                    }
                    else
                    {
                        string remove = txtTo.Text.Replace(((HyperLink)emailaddress.Items[i].FindControl("TomailAddress")).Text.ToString(), "");
                        txtTo.Text = remove;
                        txtCC.Text += ((HyperLink)emailaddress.Items[i].FindControl("TomailAddress")).Text.ToString();
                        txtCC.Text = txtCC.Text.TrimStart('(');
                        txtCC.Text = txtCC.Text.TrimEnd(')');
                        txtCC.Text = txtCC.Text.ToString();
                    }
                }
                else if (((CheckBox)emailaddress.Items[i].FindControl("ToEmailCheck")).Checked == true &&
                    ((CheckBox)emailaddress.Items[i].FindControl("CCEmailCheck")).Checked == false)
                {
                    if (txtTo.Text.Length > 0)
                    {
                        string remove = txtCC.Text.Replace(((HyperLink)emailaddress.Items[i].FindControl("TomailAddress")).Text.ToString(), "");
                        txtCC.Text = remove;
                        txtTo.Text += "," + ((HyperLink)emailaddress.Items[i].FindControl("TomailAddress")).Text.ToString().TrimStart('(');
                        txtTo.Text = txtTo.Text.TrimEnd(')');
                        txtTo.Text = txtTo.Text.ToString();
                    }
                    else
                    {
                        string remove = txtCC.Text.Replace(((HyperLink)emailaddress.Items[i].FindControl("TomailAddress")).Text.ToString(), "");
                        txtCC.Text = remove;
                        txtTo.Text += ((HyperLink)emailaddress.Items[i].FindControl("TomailAddress")).Text.ToString();
                        txtTo.Text = txtTo.Text.TrimStart('(');
                        txtTo.Text = txtTo.Text.TrimEnd(')');
                        txtTo.Text = txtTo.Text.ToString();
                    }
                }
            }

        }

Wednesday, November 17, 2010

Detect Browser Capabilities in ASP.NET

    protected void Button1_Click(object sender, System.EventArgs e)
    {
        System.Web.HttpBrowserCapabilities browser = Request.Browser;
        string s = "Browser Capabilities\n"
            + "Type = " + browser.Type + "\n"
            + "Name = " + browser.Browser + "\n"
            + "Version = " + browser.Version + "\n"
            + "Major Version = " + browser.MajorVersion + "\n"
            + "Minor Version = " + browser.MinorVersion + "\n"
            + "Platform = " + browser.Platform + "\n"
            + "Is Beta = " + browser.Beta + "\n"
            + "Is Crawler = " + browser.Crawler + "\n"
            + "Is AOL = " + browser.AOL + "\n"
            + "Is Win16 = " + browser.Win16 + "\n"
            + "Is Win32 = " + browser.Win32 + "\n"
            + "Supports Frames = " + browser.Frames + "\n"
            + "Supports Tables = " + browser.Tables + "\n"
            + "Supports Cookies = " + browser.Cookies + "\n"
            + "Supports VBScript = " + browser.VBScript + "\n"
            + "Supports JavaScript = " +
                browser.EcmaScriptVersion.ToString() + "\n"
            + "Supports Java Applets = " + browser.JavaApplets + "\n"
            + "Supports ActiveX Controls = " + browser.ActiveXControls
                  + "\n"
            + "Supports JavaScript Version = " +
                browser["JavaScriptVersion"] + "\n";

        TextBox1.Text = s;
    }

Creating control at runtime in C#Codebehind

 private DataSet GetTemplatesDs()
        {
            con.Open();
            string sqlToExecute = "select TemplateName,Subject  from Template";
            SqlDataAdapter da = new SqlDataAdapter(sqlToExecute, con);
            DataSet ds = new DataSet();
            da.Fill(ds, "Distribution_Mail_Template");
            con.Close();
            Session["TemplatesDs"] = ds;
            return ds;
        }

private void PopulateUI()//user defined function
{
                  Label[] additionalApplicants;
                  DataSet gettemplatesDs = GetTemplatesDs()
                        if (gettemplatesDs .Tables.Count > 0 && gettemplatesDs .Tables.Count > 0)
                        {
                            for (int i = 0; i < gettemplatesDs .Tables[0].Rows.Count; i++)
                            {
                                additionalApplicants = new Label[gettemplatesDs .Tables[0].Rows.Count];
                                additionalApplicants[i] = new Label();
                                additionalApplicants[i].ID = gettemplatesDs .Tables[0].Rows[0][""].ToString();
                                additionalApplicants[i].Text =gettemplatesDs .Tables[0].Rows[0][""].ToString();
                               this.Page.Controls.Add(additionalApplicants); //Adding controls to webpage
                            }
                        }
     }

Retain previously selected values by using Dictionary

 Class1.cs:
=======
 public void SaveSelectedOptions(Dictionary<string, string> selectedoptions)
        {
            Dictionary<string, string> tempDict = (Dictionary<string, string>)System.Web.HttpContext.Current.Session["SelectedValues"];
            if (tempDict != null)
            {
                foreach (var pair in selectedoptions)
                {
                    if (tempDict.ContainsKey(pair.Key)) // True
                    {
                        tempDict.Remove(pair.Key);
                    }

                    tempDict.Add(pair.Key, pair.Value);
                }
               System.Web.HttpContext.Current.Session["SelectedValues"] = tempDict;
            }
            else
            {
                Dictionary<string, string> newdict = new Dictionary<string, string>();
                foreach (var pair in selectedoptions)
                {
                    newdict.Add(pair.Key, pair.Value);
                }
                System.Web.HttpContext.Current.Session["SelectedValues"] = newdict;
            }
        }

Defaulst.aspx.cs
=============
  private void saveSelectedOptions()
        {
            Dictionary<string, string> selectedoptionsDict = new Dictionary<string, string>();
            selectedoptionsDict.Add(txtCompany.ID, txtCompany.Text);
            selectedoptionsDict.Add(txtID.ID, txtID.Text);
            selectedoptionsDict.Add(txtLastName.ID, txtLastName.Text);
            selectedoptionsDict.Add(txtShowDays.ID, txtShowDays.Text);
            selectedoptionsDict.Add(ddlAStatus.ID, ddlAStatus.SelectedIndex.ToString());
            selectedoptionsDict.Add(ddlChapterStatus.ID, ddlChapterStatus.SelectedIndex.ToString());
            selectedoptionsDict.Add(ddlDU.ID, ddlDU.SelectedIndex.ToString());
            selectedoptionsDict.Add(ddlPaid.ID, ddlPaid.SelectedIndex.ToString());
            selectedoptionsDict.Add(ddlRSatus.ID, ddlRSatus.SelectedIndex.ToString());
            Class1.SaveSelectedOptions(selectedoptionsDict);
        }

private void PopulatedPreSelectedOptions()
    {
            Dictionary<string, string> tempDict = (Dictionary<string, string>)Session["SelectedValues"];
            if (tempDict != null)
            {
                foreach (var pair in tempDict)
                {
                    string controlname = pair.Key;
                    Control control = this.FindControl(controlname);
                    if (control is DropDownList)
                    {
                        DropDownList drl = (DropDownList)this.FindControl(controlname);
                        drl.SelectedIndex = Convert.ToInt32(pair.Value);
                    }
                    else if (control is TextBox)
                    {
                        TextBox txt = (TextBox)this.FindControl(controlname);
                        txt.Text = pair.Value;
                    }
                }
                grvFiledsResult.DataSource = Session["SearchResultDS"];
                grvFiledsResult.DataBind();
            }

        }

Monday, November 15, 2010

Creating Table dynamically in codebehind C#

.CS Code:
DataTable table = new DataTable();
table.Columns.Add("Name", typeof(string));
table.Columns.Add("MemberType", typeof(string));
table.Rows.Add(membername, membertype);
table.Rows.Add(memberType);
DataSet myds = new DataSet();
myds.Tables.Add(table);
 test.DataSource = myds;
 test.DataBind();
.ASPX Code:
<table >
                    <tr><td>
                    <asp:GridView ID="test" AutoGenerateColumns="false" runat="server" ShowHeader="false" GridLines="None">
                    <Columns>
                  
                   asp:BoundField DataField="Name" />
                    <asp:BoundField DataField="MemberType" />
                    </Columns>
                   
                    </asp:GridView></td></tr></table>