JAVASCRIPT

how to show the confirmationbox using javascript in asp.net

Introduction

Here I will explain how to show the confirmation box using JavaScript in asp.net.

Description

I have on Button and when i click on the Button it will going to ask me about the confirmation whether to proceed the execution further or Not when I click on Ok the post back event occurs and goes further otherwise if you click on Cancel it stops from there and post back will not occur.
Design your aspx page like this 


<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Confirmation Box Example</title>
<script type="text/javascript">
//Confirmation box sample
function ConfirmationBox() {
var result = confirm("Are you sure you want to Submit the Data?");
if (result == true) {
return true;
}
else {
var lbl = document.getElementById('lbltxtmsg');
lbl.innerHTML = "";
return false;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnSubmit" runat="server" Text="Submit"
OnClientClick="return ConfirmationBox()" onclick="btnSubmit_Click" />
<asp:Label ID="lbltxtmsg" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
In code behind button click event write the following code 

protected void btnSubmit_Click(object sender, EventArgs e)
{
lbltxt.Text = "Welcome to Confirmationbox page";
}



Redirect Page From IFrame button Click Event using Javascript.

Introduction:

           Before some days back I had one Problem in that I have to redirect Page to another Page in Parent page from IFrame when the button is in IFrame at that time I got one solution where I am redirect it using JavaScript.it is as Follows.

.aspx.cs button Click Event..

  

  ClientScriptManager cs = Page.ClientScript;
  string myscript = @"<script language='javascript'>window.top.location.href=""/thankyou.aspx"";</script>";
  cs.RegisterClientScriptBlock (GetType(), "s", myscript);


Find Current Ajax Tab Index using Javascript.

 var index = $find("TabContainer1").get_activeTabIndex();
where TabContainer1 is the ID of TabContainer.


How to get URL name from iFrame using Javascript...


For Example we have textbox like <asp:TextBox Id="MnForm_txt" runat="server"/>

 <script type="text/javascript" language="javascript">
     document.getElementById("MnForm_txt").value = top.document.location;
 </script>


how to change textbox background color whenever validation fails using asp.net



Introduction:

In this article I will explain how to change textbox background whenever validation fails using asp.net.

Description:

I have a one requirement like changing the textboxes background whenever validation fails at that time I used the custom validator and JavaScript function to validate textboxes and change the textboxes background color.The code is as Follows


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>

    <script language="javascript" type="text/javascript">
function changeColor(source, args) {
var txtuser = document.getElementById('txtUsername');
var txtpwd = document.getElementById('txtPwd');

var strimg = new Array();
strimg = [txtuser, txtpwd];
if (args.Value == "") {
args.IsValid = false;
document.getElementById(source.id.replace('cv','txt')).style.background = 'orange';
}
else {
args.IsValid = true;
document.getElementById(source.id.replace('cv', 'txt')).style.background = 'white';
}
}
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table>
            <tr>
                <td colspan="2">
                    <b>Change Background of Textbox If you entered nothing in TextBox</b>
                </td>
            </tr>
            <tr>
                <td>
                    UserName:
                </td>
                <td>
                    <asp:TextBox ID="txtUsername" runat="server"></asp:TextBox>
                    <asp:CustomValidator ID="cvUsername" runat="server" SetFocusOnError="true" Display="Dynamic"
                        ValidateEmptyText="true" ControlToValidate="txtUsername" ClientValidationFunction="changeColor"> 
<img src="Images/error.gif" align="middle" alt="Image Not Found"/>Please enter UserName
                    </asp:CustomValidator>
                </td>
            </tr>
            <tr>
                <td>
                    Password:
                </td>
                <td>
                    <asp:TextBox ID="txtPwd" runat="server"></asp:TextBox>
                    <asp:CustomValidator ID="cvPwd" runat="server" SetFocusOnError="true" Display="Dynamic"
                        ValidateEmptyText="true" ControlToValidate="txtPwd" ClientValidationFunction="changeColor"> 
<img src="Images/error.gif" align="middle" alt="Image Not Found"/>Please enter Password
                    </asp:CustomValidator>
                </td>
            </tr>
                <tr>
                    <td>
                    </td>
                    <td>
                        <asp:Button ID="Button1" runat="server" Text="Submit" />
                    </td>
                </tr>
            
        </table>
    </div>
    </form>
</body>
</html>