本文介绍了数据绑定方法(如Eval(),XPath()和Bind())只能在数据绑定控件的上下文中使用。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友,

我在网格视图中实现代码时遇到了一些问题。当我通过编码对gridview控件进行数据绑定时产生错误。产生以下错误:



数据绑定方法,如Eval(),XPath()和Bind()只能在数据绑定控件的上下文中使用。



.cs代码是: -



Dear friend,
I have some problem when implementing the code in grid view.The error produce when i am databind the gridview control by coding.The following error are produced :

"Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control."

.cs code is :-

double total = 0;
     db = new MCTAdminDataContext();
     var advance = (from t in db.Transactions
                    where t.IsChecked && t.IsConfirmed == true
                    select new
                    {
                        TransactionID = t.TransactionID,
                        RecordingDate = t.CreationDate,
                        TransactionDate = t.TransactionDate,
                        CreditAccount = t.CreditAccount,
                        DebitAccount = t.DebitAccount,
                        AccountNo = getAccountNo(t.AccountNo),
                        Type = t.TransactionType,
                        From = getName(t.DebitAccount, t.SpecifiedName, t.ClientOrCompanyName),
                        To = getName(t.CreditAccount, t.SpecifiedName, ""),
                        Amount = t.Amount,
                        For = t.Description,
                        Project = getProject(t.ProjectID),
                        Head = getHead(t.HeadID),
                        CompanyID = t.CompanyID
                    }).ToList();

     advance = (advance.Where(a => a.CompanyID.Equals(Session[SessionController.Session_CompanyId]))).ToList();

     if (!string.IsNullOrEmpty(drpEmployee.SelectedValue) && drpEmployee.SelectedValue != "0" && drpEmployee.SelectedValue != "--Select--")
     {
         advance = (from a in advance
                    where a.CreditAccount == Convert.ToInt64(drpEmployee.SelectedValue)
                    select a).ToList();

         total = advance.Sum(a => Convert.ToDouble(Convert.ToString(a.Amount).Replace(",", string.Empty)));

     }
     else
     {
         advance = (from a in advance
                    where a.Type.Contains("Receipt") || a.Type.Contains("Advance")
                    select a).ToList();
     }

     grdAdvance.DataSource = advance;
     grdAdvance.DataBind();->error find here


     return total;







.aspx代码: -










.aspx code :-



<asp:LinkButton runat="server" ID="Delete" OnClientClick="return confirm('Are you sure?');"

    Text='Delete' CommandArgument='<%#Eval("TransactionID" %>'---->Error produced here
 CommandName="lnkDelete"></asp:LinkButton>





请尽快解决这个问题..



感谢先进!!



Please solve this problem as soon as possible..

Thanks in advanced !!

推荐答案

.gv_hide_column
{
    display: none;
}

<pre lang="HTML"><pre lang="xml">&lt;asp:BoundField DataField=&quot;<pre lang="xml">TransactionID&amp;quot; HeaderText=&amp;quot;<pre lang="xml">TransactionID&amp;amp;quot; ItemStyle-CssClass=&amp;amp;quot;gv_hide_column&amp;amp;quot;
                                    HeaderStyle-CssClass=&amp;amp;quot;gv_hide_column&amp;amp;quot; /&amp;amp;gt;

&amp;amp;lt;pre lang=&amp;amp;quot;xml&amp;amp;quot;&amp;amp;gt;&amp;amp;amp;lt;asp:ImageButton ID=&amp;amp;amp;quot;imgbtnDelete&amp;amp;amp;quot; Text=&amp;amp;amp;quot;Delete&amp;amp;amp;quot; runat=&amp;amp;amp;quot;server&amp;amp;amp;quot; OnClick=&amp;amp;amp;quot;imgbtnDelete_Click&amp;amp;amp;quot;
                                            &amp;amp;lt;b&amp;amp;gt;RowIndex=&amp;amp;amp;#39;&amp;amp;amp;lt;%# Container.DisplayIndex %&amp;amp;amp;gt;&amp;amp;amp;#39;&amp;amp;lt;/b&amp;amp;gt; ImageUrl=&amp;amp;amp;quot;~/Images/DeleteIcon.png&amp;amp;amp;quot; ToolTip=&amp;amp;amp;quot;Delete this user&amp;amp;amp;quot;
                                            Height=&amp;amp;amp;quot;16px&amp;amp;amp;quot; Width=&amp;amp;amp;quot;16px&amp;amp;amp;quot;  /&amp;amp;amp;gt;&amp;amp;lt;/pre&amp;amp;gt;&amp;lt;/pre&amp;gt;&lt;/pre&gt;</pre>













<pre lang="cs">protected void imgbtnDelete_Click(object sender, EventArgs e)
    {
        try
        {
            ImageButton vImgbtnDeleteToGetRowIndex = sender as ImageButton;
            int vRowIndex = Convert.ToInt32(vImgbtnDeleteToGetRowIndex.Attributes["RowIndex"]);
            int vUserId = Convert.ToInt32(gvTestEditSequence.DataKeys[vRowIndex].Values["TransactionID"].ToString());


这篇关于数据绑定方法(如Eval(),XPath()和Bind())只能在数据绑定控件的上下文中使用。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-20 22:24