在gridview中调用后端方法 [英] Calling a backend method within a gridview

查看:81
本文介绍了在gridview中调用后端方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含EmployeeID,Designation,Basicsallery,coveyanceRate和Housereant字段的数据库。现在在gridview中我必须显示指定,EmployeeId和Totalsallery字段。为了计算总sallery字段,我在aspx.cs文件中编写了一个方法并在gridview中调用它。我的代码显示异常,说DataBinding:'System.Data.DataRowView'不包含名称为



的属性我在aspx中的代码:

 <%@     Page    语言  =   C#    AutoEventWireup  < span class =code-keyword> =  true    CodeBehind   =  ViewPage.aspx.cs   继承  =  labTest02.ViewPage   %>  

< !DOCTYPE html >

< html xmlns = http://www.w3.org/1999/xhtml >
< head runat = 服务器 >
< title > <跨班=code-keyword>< / title >
< span class =code-keyword>< / head >
< body >
< 表格 id = form1 runat = 服务器 >
< div >
< asp:GridView ID = GridView1 对齐 = center runat = server ForeColor = #333333 ShowFooter = True GridLines < span class =code-keyword> = AutoGenerateColumns = False DataSourceID = < span class =code-keyword> SqlDataSource1 >
< AlternatingRowStyle BackColor = 白色 ForeColor = #284775 / >
< Col umns >
< asp:BoundField DataField = EmployeeID HeaderText = EmployeeID SortExpression = EmployeeID / >
< asp:BoundField DataField = EmployeeName HeaderText = EmployeeName SortExpression = EmployeeName / >
< asp:BoundField DataField = 名称 HeaderText = 名称 SortExpression = 指定 / >
< asp:TemplateField HeaderText = 总计 >
< ItemTemplate >
< asp:Label runat = server 文字 =' <%#GetTotalSallery(Convert.ToDouble(Eval( BasicSallery)),Convert.ToDouble(Eval( convRate)) ,Convert.ToDouble(Eval( houseRent)))%> ' > < / asp:Label >
< < span class =code-leadattribute> / ItemTemplate >
< / asp:TemplateField >


< / Columns >
< FooterStyle BackColor = #5D7B9D Font-Bold = < span class =code-attribute> ForeColor = 白色 / >
< HeaderStyle BackColor = #5D7B9D Font-Bold = True ForeColor = 白色 / >
< PagerStyle BackColor = #284775 ForeColor = 白色 Horizo​​ntalAlign = 中心 / >
< RowStyle BackColor = #F7F6F3 ForeColor = #333333 Horizo​​ntalAlign = 中心 / >
< SelectedRowStyle BackColor = #E2DED6 字体粗体 = ForeColor = #333333 / >
< SortedAscendingCellStyle BackColor = #E9E7E2 / >
< SortedAscendingHeaderStyle BackColor = #506C8C < span class =code-keyword> / >
< SortedDescendingCellStyle BackColor = #FFFDF8 / >
< SortedDescendingHeaderStyle BackColor = < span class =code-keyword>#6F8DAE / >
< / asp:GridView < span class =code-keyword>>
< asp:Label runat = server 文本 = 标签 ID = lebelforTotal > < / asp:Label < span class =code-keyword>>
< asp:SqlDataSource ID = SqlDataSource1 runat = server ConnectionString = <% $ ConnectionStrings:labtest02_dbConnectionString %> SelectCommand = SELECT [EmployeeID],[EmployeeName],[Designation] FROM [EmployeeSallery] > < / asp:SqlDataSource >
< / div > ;
< / form >
< / body >
< / html >









aspx.cs代码:

 使用系统; 
使用 System.Collections.Generic;
使用 System.Data.SqlClient;
使用 System.Linq;
使用 System.Web;
使用 System.Web.Configuration;
使用 System.Web.Services.Description;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;

命名空间 labTest02
{
public partial class ViewPage:System.Web.UI.Page
{
protected void Page_Load( object sender,EventArgs e)
{
}

public double GetTotalSallery( double BasicSallery, double convRate, double houseRent)
{
string connectionString = WebConfigurationManager.ConnectionStrings [ EmployeeConnectionString]的ConnectionString。
SqlConnection connection = new SqlConnection(connectionString);

string query = SELECT BasicSallery,convRate,houseRent FROM EmployeeSallery;

SqlCommand command = new SqlCommand(查询,连接);

connection.Open();



SqlDataReader sdr = command.ExecuteReader();

while (sdr.Read())
{
string basic = sdr [ BasicSallery]。ToString();
string conv =(sdr [ convRate ]的ToString());
string houserent =(sdr [ houseRent ]的ToString());


EmployeeSallery aSallery = new EmployeeSallery();
aSallery.BasicSallery = Convert.ToDouble(basic);
aSallery.ConvRate = Convert.ToDouble(conv);
aSallery.HouseRent = Convert.ToDouble(houserent);
aSallery.GetTotalSallery();

}
sdr.Close();
connection.Close();
返回 BasicSallery +((BasicSallery * convRate)/ 100 )+((BasicSallery * houseRent )/ 100 );

}
}
}







我的班级文件:



 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;

命名空间 labTest02
{
public class EmployeeSallery
{
public string EmployeeId { get ; set ; }
public string EmployeeName { get ; set ; }
public string 名称{ get ; set ; }
public string 电子邮件{ get ; set ; }
public double BasicSallery { get ; set ; }
public double ConvRate { get ; set ; }
public double HouseRent { get ; set ; }

public double GetConveyance()
{
return (BasicSallery * ConvRate)/ 100 ;
}

public double GetHouseRent()
{
return (BasicSallery * HouseRent)/ 100 ;
}

public double GetTotalSallery()
{
return BasicSallery + GetConveyance()+ GetHouseRent();
}

}
}







这是一个紧急的问题。我有截止日期提前工作..Thnx提前

解决方案

ConnectionStrings:labtest02_dbConnectionString %> SelectCommand = SELECT [EmployeeID],[EmployeeName],[Designation] FROM [EmployeeSallery] > < / asp:SqlDataSource >
< span class =code-keyword>< / div >
< / form >
< / body >
< / html >









aspx.cs代码:

< pre lang =c#> 使用系统;
使用 System.Collections.Generic;
使用 System.Data.SqlClient;
使用 System.Linq;
使用 System.Web;
使用 System.Web.Configuration;
使用 System.Web.Services.Description;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;

命名空间 labTest02
{
public partial class ViewPage:System.Web.UI.Page
{
protected void Page_Load( object sender,EventArgs e)
{
}

public double GetTotalSallery( double BasicSallery, double convRate, double houseRent)
{
string connectionString = WebConfigurationManager.ConnectionStrings [ EmployeeConnectionString]的ConnectionString。
SqlConnection connection = new SqlConnection(connectionString);

string query = SELECT BasicSallery,convRate,houseRent FROM EmployeeSallery;

SqlCommand command = new SqlCommand(查询,连接);

connection.Open();



SqlDataReader sdr = command.ExecuteReader();

while (sdr.Read())
{
string basic = sdr [ BasicSallery]。ToString();
string conv =(sdr [ convRate ]的ToString());
string houserent =(sdr [ houseRent ]的ToString());


EmployeeSallery aSallery = new EmployeeSallery();
aSallery.BasicSallery = Convert.ToDouble(basic);
aSallery.ConvRate = Convert.ToDouble(conv);
aSallery.HouseRent = Convert.ToDouble(houserent);
aSallery.GetTotalSallery();

}
sdr.Close();
connection.Close();
返回 BasicSallery +((BasicSallery * convRate)/ 100 )+((BasicSallery * houseRent )/ 100 );

}
}
}







我的班级文件:



 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;

命名空间 labTest02
{
public class EmployeeSallery
{
public string EmployeeId { get ; set ; }
public string EmployeeName { get ; set ; }
public string 名称{ get ; set ; }
public string 电子邮件{ get ; set ; }
public double BasicSallery { get ; set ; }
public double ConvRate { get ; set ; }
public double HouseRent { get ; set ; }

public double GetConveyance()
{
return (BasicSallery * ConvRate)/ 100 ;
}

public double GetHouseRent()
{
return (BasicSallery * HouseRent)/ 100 ;
}

public double GetTotalSallery()
{
return BasicSallery + GetConveyance()+ GetHouseRent();
}

}
}







这是一个紧急的问题。我有截止日期提前工作..Thnx提前


你的大多数错误是由于大写和小写拼写和不匹配:



 Text ='<%#GetTotalSallery(Convert.ToDouble(Eval(BasicSallery)),Convert.ToDouble(Eval(convRate)),Convert.ToDouble(Eval(houseRent )))%>'





遵循风格和命名惯例非常重要。

For例如:所有变量都以小写字母开头,方法以大写字母开头。



此外,调用UI页面内的方法并不好,而是使用后面的代码。这将消除错误。



SQL查询应该在Object类中或在控制器类中,而不是在UI中。


< blockquote>< asp:SqlDataSource ID =SqlDataSource1runat =serverConnectionString =<%


I have a db which has EmployeeID,Designation,Basicsallery,coveyanceRate and Housereant field is available. Now within a gridview i have to show designation,EmployeeId and Totalsallery field.To calculate total sallery field i have written a method in the aspx.cs file and called it within the gridview. My code is showing an exception saying "DataBinding: 'System.Data.DataRowView' does not contain a property with the name"


My code in aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ViewPage.aspx.cs" Inherits="labTest02.ViewPage" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:GridView ID="GridView1" align="center" runat="server" ForeColor="#333333" ShowFooter="True" GridLines="None" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                <Columns>
                    <asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" SortExpression="EmployeeID" />
                    <asp:BoundField DataField="EmployeeName" HeaderText="EmployeeName" SortExpression="EmployeeName" />
                    <asp:BoundField DataField="Designation" HeaderText="Designation" SortExpression="Designation" />
                    <asp:TemplateField HeaderText=" Total sallery">
                        <ItemTemplate>
                            <asp:Label runat="server" Text='<%# GetTotalSallery(Convert.ToDouble(Eval("BasicSallery")),Convert.ToDouble(Eval("convRate")),Convert.ToDouble(Eval("houseRent"))) %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>


                </Columns>
                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                <SortedAscendingCellStyle BackColor="#E9E7E2" />
                <SortedAscendingHeaderStyle BackColor="#506C8C" />
                <SortedDescendingCellStyle BackColor="#FFFDF8" />
                <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
            </asp:GridView>
            <asp:Label runat="server" Text="Label" ID="lebelforTotal"></asp:Label>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:labtest02_dbConnectionString %>" SelectCommand="SELECT [EmployeeID], [EmployeeName], [Designation] FROM [EmployeeSallery]"></asp:SqlDataSource>
        </div>
    </form>
</body>
</html>





aspx.cs code:

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.Services.Description;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace labTest02
{
    public partial class ViewPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {            
       }

        public double GetTotalSallery(double BasicSallery, double convRate, double houseRent)
        {
            string connectionString = WebConfigurationManager.ConnectionStrings["EmployeeConnectionString"].ConnectionString;
            SqlConnection connection = new SqlConnection(connectionString);

            string query = "SELECT BasicSallery, convRate, houseRent FROM EmployeeSallery";

            SqlCommand command = new SqlCommand(query, connection);

            connection.Open();



            SqlDataReader sdr = command.ExecuteReader();

            while (sdr.Read())
            {
                string basic = sdr["BasicSallery"].ToString();
                string conv = (sdr["convRate"].ToString());
                string houserent = (sdr["houseRent"].ToString());


                EmployeeSallery aSallery = new EmployeeSallery();
                aSallery.BasicSallery = Convert.ToDouble(basic);
                aSallery.ConvRate = Convert.ToDouble(conv);
                aSallery.HouseRent = Convert.ToDouble(houserent);
                aSallery.GetTotalSallery();

            }
            sdr.Close();
            connection.Close();
            return BasicSallery + ((BasicSallery * convRate) / 100) + ((BasicSallery * houseRent) / 100);

        }
    }
}




my class file:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace labTest02
{
    public class EmployeeSallery
    {
        public string EmployeeId { get; set; }
        public string EmployeeName { get; set; }
        public string Designation { get; set; }
        public string Email { get; set; }
        public double BasicSallery { get;  set; }
        public double ConvRate { get;  set; }
        public double HouseRent { get; set; }

        public double GetConveyance()
        {
            return (BasicSallery * ConvRate) / 100;
        }

        public double GetHouseRent()
        {
            return (BasicSallery * HouseRent) / 100;
        }

        public double GetTotalSallery()
        {
            return BasicSallery + GetConveyance() + GetHouseRent();
        }

    }
}




its a vry urgent issue. i have deadline to live upto..Thnx in advance

解决方案

ConnectionStrings:labtest02_dbConnectionString %>" SelectCommand="SELECT [EmployeeID], [EmployeeName], [Designation] FROM [EmployeeSallery]"></asp:SqlDataSource> </div> </form> </body> </html>





aspx.cs code:

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.Services.Description;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace labTest02
{
    public partial class ViewPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {            
       }

        public double GetTotalSallery(double BasicSallery, double convRate, double houseRent)
        {
            string connectionString = WebConfigurationManager.ConnectionStrings["EmployeeConnectionString"].ConnectionString;
            SqlConnection connection = new SqlConnection(connectionString);

            string query = "SELECT BasicSallery, convRate, houseRent FROM EmployeeSallery";

            SqlCommand command = new SqlCommand(query, connection);

            connection.Open();



            SqlDataReader sdr = command.ExecuteReader();

            while (sdr.Read())
            {
                string basic = sdr["BasicSallery"].ToString();
                string conv = (sdr["convRate"].ToString());
                string houserent = (sdr["houseRent"].ToString());


                EmployeeSallery aSallery = new EmployeeSallery();
                aSallery.BasicSallery = Convert.ToDouble(basic);
                aSallery.ConvRate = Convert.ToDouble(conv);
                aSallery.HouseRent = Convert.ToDouble(houserent);
                aSallery.GetTotalSallery();

            }
            sdr.Close();
            connection.Close();
            return BasicSallery + ((BasicSallery * convRate) / 100) + ((BasicSallery * houseRent) / 100);

        }
    }
}




my class file:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace labTest02
{
    public class EmployeeSallery
    {
        public string EmployeeId { get; set; }
        public string EmployeeName { get; set; }
        public string Designation { get; set; }
        public string Email { get; set; }
        public double BasicSallery { get;  set; }
        public double ConvRate { get;  set; }
        public double HouseRent { get; set; }

        public double GetConveyance()
        {
            return (BasicSallery * ConvRate) / 100;
        }

        public double GetHouseRent()
        {
            return (BasicSallery * HouseRent) / 100;
        }

        public double GetTotalSallery()
        {
            return BasicSallery + GetConveyance() + GetHouseRent();
        }

    }
}




its a vry urgent issue. i have deadline to live upto..Thnx in advance


Most of your errors are due to spelling and mismatching upper and lower case:

Text='<%# GetTotalSallery(Convert.ToDouble(Eval("BasicSallery")),Convert.ToDouble(Eval("convRate")),Convert.ToDouble(Eval("houseRent"))) %>'



It is important to follow style and naming conventions.
For example: all variables are start with lower case and methods with upper case.

Also, it is not good to call methods inside UI pages, instead use the code behind. That will eliminate errors.

SQL Queries should be either in the Object class or in a controller class, not in the UI.


<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%


这篇关于在gridview中调用后端方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆