Pages

Thursday, February 23, 2012

Math.Round() in java script

Syntax:
Math.round(x)


Example

<script type="text/javascript">

document.write(Math.round(0.75) + "<br />");
document.write(Math.round(0.50) + "<br />");
document.write(Math.round(0.25) + "<br />");
document.write(Math.round(-4.30) + "<br />");
document.write(Math.round(-4.88));

</script>


OUTPUT:


1
1
0
-4
-5

Wednesday, February 22, 2012

How to set default active view index

For making any default active view index . just write folloeing code in code behind

 WayBillMultiView.ActiveViewIndex = 0;

Tuesday, February 21, 2012

how to disable tabindex on controls in asp.net

For disabling tab just set TabIndex of any control to -1.

ex.

 <asp:TextBox ID="tbTotalAmount" runat="server" TabIndex="-1" ></asp:TextBox>

Monday, February 20, 2012

How to add Trigger to an UpdatePanel in code behind

//Creates a new async trigger
AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
            //Sets the control that will trigger a post-back on the UpdatePanel
trigger.ControlID = "btnCommit";
//Sets the event name of the control
trigger.EventName = "Click";
//Adds the trigger to the UpdatePanels' triggers collection
pnlMain.Triggers.Add(trigger);

GridView Update All Rows At Once in asp.net

private void Update()
{
StringBuilder sb = new StringBuilder();
// build the query
foreach (GridViewRow row in GridView1.Rows)
{
sb.Append("UPDATE Users SET FirstName = '");
sb.Append((row.FindControl("txtFirstName") as TextBox).Text);
sb.Append("',");
sb.Append("LastName = '");
sb.Append((row.FindControl("txtLastName") as TextBox).Text);
sb.Append("', ");
sb.Append("ClassCode = '");
sb.Append((row.FindControl("txtClassCode") as TextBox).Text);
sb.Append("'");
sb.Append(" WHERE UserID = ");
sb.Append(Convert.ToInt32((row.FindControl("lblUserID") as Label).Text));
sb.Append(" ");
}
string connectionString =
"Server=HCUBE008;Database=School;Trusted_Connection=true";
SqlConnection myConnection = new SqlConnection(connectionString);
SqlCommand myCommand = new SqlCommand(sb.ToString(), myConnection);
myConnection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
}

Bind Data Dynamically To Drop Down List in gridview in asp.net


 ASPX Code:

 <asp:GridView ID="Gridview1" runat="server" AutoGenerateColumns="False" HeaderStyle-BackColor="#EDEDED" HeaderStyle-ForeColor="#184F46" HeaderStyle-Height="40px" HeaderStyle-HorizontalAlign="Center" OnRowDataBound="Gridview1_RowDataBound" SelectedRowStyle-BackColor="#F9CACA" ShowFooter="True" TabIndex="9" Width="100%">
<asp:TemplateField HeaderStyle-Height="30px" HeaderText="Package Type">
<ItemTemplate>
 <asp:DropDownList ID="ddlPackageType" runat="server"  Width="70px">
  </asp:DropDownList>
  </ItemTemplate>
  <HeaderStyle Height="30px" Width="8%" />
  <ItemStyle HorizontalAlign="Center" />
    </asp:TemplateField>
</GridView>


CODE Behind Code for Binding Data  :

protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
           
            DataSet dsPackageType = new DataSet();
      //Write Code For Getting data from Database and add it in dsPackageType Dataset .        

           if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Control ctrl = e.Row.FindControl("ddlPackageType");
                if (ctrl != null)
                {

                    DropDownList dd = ctrl as DropDownList;
                    dd.DataTextField = "PackageType";
                    dd.DataValueField= "Id";
                    dd.DataSource = dsPackageType;
                    dd.DataBind();
                    dd.Items.Insert(0, new ListItem("- Select -", "0"));

                }

            }
}

How to round numbers in java Script


Option1 : Math.Round()

Jave Script For rounding the numbers to required decimals ...

<script language="javascript" type="text/javascript">
function roundNumber(rnum, rlength) { // Arguments: number to round, number of decimal places
var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
document.roundform.numberfield.value = parseFloat(newnumber); // Output the result to the form field (change for your
purposes)
}
</script>


Simple HTM For for Rounding the numbers 

<form name="roundform">
<table border="0" cellspacing="0" cellpadding="5">
<tr>
<td>Round:</td>
<td><input type="text" name="numberfield" value="">
to
<input name="decimalfield" type="text" value="2" size="3">
decimal places</td>



Option 2: toFixed (beta)

Java Script
<script type="text/javascript">
function roundNumber(number, decimals) { // Arguments: number to round, number of decimal places
var newnumber = new Number(number+'').toFixed(parseInt(decimals));
document.roundform.roundedfield.value = parseFloat(newnumber); // Output the result to the form field (change for your purposes)
}
</script>

Sample HTML Form 

<form name="roundform">
<table border="0" cellspacing="0" cellpadding="5">
<tr>
<td>Round:</td>
<td><input type="text" name="numberfield" value="">
to
<input name="decimalfield" type="text" value="2" size="3">
decimal places</td>