Hi akhter,
First you need to calculate the absent then apply the if else condition. Since you are calculating absent after condition the absent is always zero for the condition.
Refer below modified sample.
HTML
<asp:TextBox ID="txttotalday" runat="server"></asp:TextBox>
<asp:GridView ID="gvv" runat="server" AutoGenerateSelectButton="false" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="Emp_ID" runat="server" Text='<%#Bind("Emp_ID")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="EMp_Name" runat="server" Text='<%#Bind("EMp_Name")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="salary">
<ItemTemplate>
<asp:Label ID="EMP_Salary" runat="server" Text='<%#Bind("EMP_Salary")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Allowance_Status">
<ItemTemplate>
<asp:Label ID="emp_allowance" runat="server" Text='<%#Bind("emp_allowance")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Allowance">
<ItemTemplate>
<asp:Label ID="lballowance" runat="server" Text='<%#Bind("lballowance")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Deduction">
<ItemTemplate>
<asp:Label ID="Emp_Deduction" runat="server" Text='<%#Bind("Emp_Deduction")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Present">
<ItemTemplate>
<asp:TextBox ID="txtpresent" runat="server" Text='0' AutoPostBack="true" OnTextChanged="OnCalculate"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Absent">
<ItemTemplate>
<asp:Label ID="txtabsent" runat="server" Text='0'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Salary">
<ItemTemplate>
<asp:Label ID="txtsalary" runat="server" Text='0'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btn_gen" runat="server" Text="Generate" OnClick="btn_gen_Click" />
Namespaces
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] {
new DataColumn("Emp_ID"), new DataColumn("EMp_Name"), new DataColumn("EMP_Salary"),
new DataColumn("emp_allowance"), new DataColumn("lballowance"), new DataColumn("Emp_Deduction") });
dt.Rows.Add("10001", "akhter", "10000", "Yes", "1000", "500");
dt.Rows.Add("10002", "Rais", "5000", "No", "0", "4111");
dt.Rows.Add("20003", "Najam", "60000", "Yes", "0", "1500");
ViewState["dt"] = dt;
gvv.DataSource = dt;
gvv.DataBind();
}
}
protected void OnCalculate(object sender, EventArgs e)
{
TextBox present = sender as TextBox;
GridViewRow row = present.NamingContainer as GridViewRow;
Label absent = row.FindControl("txtabsent") as Label;
Label gsalary = row.FindControl("EMP_Salary") as Label;
Label deduction = row.FindControl("Emp_Deduction") as Label;
Label statusallowance = row.FindControl("Emp_Allowance") as Label;
Label nsalary = row.FindControl("txtsalary") as Label;
Label allowance = row.FindControl("lballowance") as Label;
Calculate(txttotalday, present, absent, gsalary, nsalary, deduction, allowance, statusallowance);
}
private void Calculate(TextBox total, TextBox present, Label absent, Label gsalary, Label nsalary,
Label deduction, Label allowance, Label statusallowance)
{
absent.Text = (Convert.ToDecimal(total.Text) - Convert.ToDecimal(present.Text)).ToString();
if (absent.Text == "0" && statusallowance.Text == "Yes")
{
allowance.Text = "1000";
}
else if (absent.Text == "1" && statusallowance.Text == "Yes")
{
allowance.Text = "500";
}
else if (absent.Text == "0.5" && statusallowance.Text == "Yes")
{
allowance.Text = "500";
}
else
{
allowance.Text = "0";
}
nsalary.Text = ((Convert.ToDecimal(gsalary.Text) / Convert.ToDecimal(total.Text) * Convert.ToDecimal(present.Text)
+ Convert.ToDecimal(allowance.Text) - Convert.ToDecimal(deduction.Text)).ToString());
nsalary.Text = Math.Round(double.Parse(nsalary.Text), 0).ToString();
}
protected void btn_gen_Click(object sender, EventArgs e)
{
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {
New DataColumn("Emp_ID"), New DataColumn("EMp_Name"), New DataColumn("EMP_Salary"),
New DataColumn("emp_allowance"), New DataColumn("lballowance"), New DataColumn("Emp_Deduction")})
dt.Rows.Add("10001", "akhter", "10000", "Yes", "1000", "500")
dt.Rows.Add("10002", "Rais", "5000", "No", "0", "4111")
dt.Rows.Add("20003", "Najam", "60000", "Yes", "0", "1500")
ViewState("dt") = dt
gvv.DataSource = dt
gvv.DataBind()
End If
End Sub
Protected Sub OnCalculate(ByVal sender As Object, ByVal e As EventArgs)
Dim present As TextBox = TryCast(sender, TextBox)
Dim row As GridViewRow = TryCast(present.NamingContainer, GridViewRow)
Dim absent As Label = TryCast(row.FindControl("txtabsent"), Label)
Dim gsalary As Label = TryCast(row.FindControl("EMP_Salary"), Label)
Dim deduction As Label = TryCast(row.FindControl("Emp_Deduction"), Label)
Dim statusallowance As Label = TryCast(row.FindControl("Emp_Allowance"), Label)
Dim nsalary As Label = TryCast(row.FindControl("txtsalary"), Label)
Dim allowance As Label = TryCast(row.FindControl("lballowance"), Label)
Calculate(txttotalday, present, absent, gsalary, nsalary, deduction, allowance, statusallowance)
End Sub
Private Sub Calculate(ByVal total As TextBox, ByVal present As TextBox, ByVal absent As Label, ByVal gsalary As Label,
ByVal nsalary As Label, ByVal deduction As Label, ByVal allowance As Label, ByVal statusallowance As Label)
absent.Text = (Convert.ToDecimal(total.Text) - Convert.ToDecimal(present.Text)).ToString()
If absent.Text = "0" AndAlso statusallowance.Text = "Yes" Then
allowance.Text = "1000"
ElseIf absent.Text = "1" AndAlso statusallowance.Text = "Yes" Then
allowance.Text = "500"
ElseIf absent.Text = "0.5" AndAlso statusallowance.Text = "Yes" Then
allowance.Text = "500"
Else
allowance.Text = "0"
End If
nsalary.Text = ((Convert.ToDecimal(gsalary.Text) / Convert.ToDecimal(total.Text) * Convert.ToDecimal(present.Text) _
+ Convert.ToDecimal(allowance.Text) - Convert.ToDecimal(deduction.Text)).ToString())
nsalary.Text = Math.Round(Double.Parse(nsalary.Text), 0).ToString()
End Sub
Protected Sub btn_gen_Click(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Screenshot