7 March 2014

Validate Money Currency format in ASP.Net TextBox using Validators

<asp:TextBox ID="txt_Amount" runat="server"></asp:TextBox>
    <asp:RegularExpressionValidator ID="RegularExpressionValidator10" runat="server"
        ControlToValidate="txt_Amount" ErrorMessage="Please enter only numbers like 1000 or 1000.00"
        ValidationExpression="^\d+(\.\d\d)?$"></asp:RegularExpressionValidator>

6 March 2014

Get Find control inside of asp.net gridview in rowdatabound event

protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = (DropDownList)e.Row.FindControl("ddlCity");
int CountryId = Convert.ToInt32(ddl.SelectedItem);
}
}

OR

protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DropDownList ddl = (DropDownList)gvUserInfo.FindControl("ControlName");
string ddlValue = ddl.SelectedItem.Value;
TextBox txtUserInfo = (TextBox) gvUserInfo.FindControl("ControlName");
string strValue = txtUserInfo.Text;
}

session variables in asp.net

Using session in asp.net is one of the server side state management technique. This maintains state specific to the user and all the session gets destroyed after the application is closed.

1. To declare a session in c#
Session ["yourvariablename"]="value"

2. To clear a value holded by any session variable we can use 
Session.clear() or session.removeall()

3. To dispose a session I.e. to destroy the session along with value. Here the session variable will exist no more till it is declared with the same name.
Session.abandon()

4. To remove a specific session Use this 
Session.remove("sessionvariablename")

how to access values of the controls present inside the detailsview control

string EmpName = ((TextBox)DetailsView1.FindControl("Emp_Name")).Text;

or u can use

 string s = DetailsView1.Rows[0].Cells[1].Text;

capture dropdownlist value in javascript | Example : Fetch or get asp.net dropdownlist control value in javascript

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function getValue() {
         
            var ddl = document.getElementById("<%=DropDownList1.ClientID%>");
            var ddlValue = ddl.options[ddl.selectedIndex].value;
            alert(ddlValue);

        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server">
            <asp:ListItem Value="1">IT</asp:ListItem>
            <asp:ListItem Value="2">Accounts</asp:ListItem>
            <asp:ListItem Value="3">Operations</asp:ListItem>
        </asp:DropDownList>
        <br />
        <asp:Button ID="Button1" runat="server" Text="Get DropDown Selected Value" OnClientClick="return getValue();" />
    </div>
    </form>
</body>
</html>

Result:

print a particular div section using JavaScript in asp.net.

Example:

<script type="text/javascript">
    function ClickToPrint() {
        docPrint = window.open("", "Print");
        docPrint.document.open();
        docPrint.document.write('<html><head><title>::PRINT SAMPLE::</title>');
        docPrint.document.write('</head><body onLoad="self.print()">');
        docPrint.document.write('<table width="900px" border=0><tr><td width=100%><center><font face="windings">');
        docPrint.document.write(document.getElementById("PrintDiv").innerHTML);
        docPrint.document.write('</font></center></td></tr></table></body></html>');
        docPrint.document.close();
    }

</script>
<html>
<head runat="server">
    <title></title>
</head>
<body>
    <div id="PrintDiv">
        Hello World ! This is Asp.net.
    </div>
    <div id="DoNotPrintDiv">
        Thanks for visiting this website !!
    </div>
    <input id="Button1" onclick="ClickToPrint();" type="button" value="Print" class="button" />
</body>
</html>

calculating days,months,years difference between two dates in asp.net,C# | calculating date difference using timespan to calculate days,months,years

public void calcDayMonthYear()
{
        DateTime dayStart;
        DateTime dateEnd;

        dayStart = Convert.ToDateTime(frmdate.Text);
        dateEnd = Convert.ToDateTime(todate.Text);
        TimeSpan ts = dateEnd - dayStart;

        double Years = Convert.ToDouble(ts.TotalDays) / 365;
        double Months = Years * 12;
        double Days = Convert.ToDouble(ts.TotalDays); 
}