Friday, 3 August 2012

ASP.Net Listbox with Checkbox - Get and Set Selected Value

I would recommend using a div styled like a listbox then we can have whatever we want in there.

<div style="width:200px; height:150px; padding:2px; overflow:auto; border: 1px solid #ccc;">
    <asp:CheckBoxList class="BodyTxt" ID="ChkServicestopitch" runat="server">
          <asp:ListItem Text="ERP" Value="1"></asp:ListItem>
          <asp:ListItem Text="CRM" Value="2"></asp:ListItem>
          <asp:ListItem Text="E-Commerce" Value="3"></asp:ListItem>
          <asp:ListItem Text="Business Intelligence" Value="4"></asp:ListItem>
          <asp:ListItem Text="HRMS" Value="5"></asp:ListItem>
          <asp:ListItem Text="Mobile Application" Value="6"></asp:ListItem>
          <asp:ListItem Text="Web Application" Value="7"></asp:ListItem>         

The div containing the checkbox is shown as follows,


Getting the selected value and text from checkbox list,

string strText = "";
string strValue = "";
for (int i = 0; i < ChkServicestopitch.Items.Count; i++)
if (ChkServicestopitch.Items[i].Selected)
            if (strText.Length == 0)
                  strText = ChkServicestopitch.Items[i].Text;
                  strValue = ChkServicestopitch.Items[i].Value;       
                  strText += "," + ChkServicestopitch.Items[i].Text;
                  strValue += "," + ChkServicestopitch.Items[i].Value;
lblResult.Text = "Checkbox Selected Text : " + strText + "</br> Checkbox Selected Value : " + strValue;

Output :

Set the selected value in Checkbox list based on passing the value,

string strValue = "1,3,4,5";
string[] splitStrValue = strValue.Split(',');
for (int i = 0; i < splitStrValue.Length; i++)
for (int j = 0; j < ChkServicestopitch.Items.Count; j++)
if (splitStrValue[i].ToString() == ChkServicestopitch.Items[j].Value.ToString())
ChkServicestopitch.Items[j].Selected = true;

Here i would pass the value as 1,3,4,5.

Output : 

Clear the selected checkbox value,

for (int j = 0; j < ChkServicestopitch.Items.Count; j++)
ChkServicestopitch.Items[j].Selected = false;


Disable back button in Browser Using Javascript

Declare a script:

    function noBack() { window.history.forward(); }

Inside the body add the below lines like below

<body onload="noBack();" onpageshow="if(event.persisted)noBack();" onunload="">

Output for Firefox and IE are as follows,


Wednesday, 1 August 2012

Difference between OnBlur and OnChange in Javascript

onblur fires when a field loses focus, while onchange fires when that field's value changes.
These events will not always occur in the same order, however.

In Firefox, tabbing out of a changed field will fire onchange then onblur, and it will normally do the same in IE.

However, if you press the enter key instead of tab, in Firefox it will fire onblur then onchange, while IE will usually fire in the original order.

However, I've seen cases where IE will also fire blur first, so be careful.
You can't assume that either the onblur or the onchange will happen before the other one.

For eg,

I have created the textbox in aspx page and called these two events like onblur and onchange from my code behind file as follows,

In .aspx page,
        <asp:TextBox ID="txtTest" runat="server"></asp:TextBox>   

From Code behind file,
     protected void Page_Load(object sender, EventArgs e)
        txtTest.Attributes.Add("onblur", "fnTestOnBlur();");
        txtTest.Attributes.Add("onchange", "fnTestOnChange();");


Javascript function,
       <script type="text/javascript" language="javascript">
        function fnTestOnBlur() {
            alert("Inside OnBlur");

        function fnTestOnChange() {
            alert("Inside OnChange");