English 中文(简体)
自定义转换函数投掷错误 。
原标题:Custom transformation function throwing error.
  • 时间:2012-05-23 13:33:30
  •  标签:
  • kentico

这是我的变换函数呼叫:

<p><%# MyFunctions.getDocumentCategory(Eval("DocumentID"))%></p>

这就是这个函数:

public static string getDocumentCategory(int documentID)
{

    string category;

    StringBuilder sb = new StringBuilder();
    // Get document categories 
    var ds = CategoryInfoProvider.GetDocumentCategories(documentID, "CategoryEnabled = 1", null);
    // Check whether exists at least one category
    if (!DataHelper.DataSourceIsEmpty(ds))
    {

        // Loop thru all categories
        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            sb.Append(Convert.ToString(dr["CategoryDisplayName"]) + ",");
        }
    }
    string content = sb.ToString();

    category = content.Split( , )[0];

    return category;
}
}

这是错误 :

MyFunctions.getDocumentCategory(int) has some invalid arguments. 

我尝试过一种接受字符串而不是整数的函数的替代形式,但它投出同样的错误。我已经核实了 Eval (“DocumentID”) 本身的位置是否正确。 有什么想法吗?

最佳回答

Eval 返回 ob object 。 您要么需要将其转换为 int , 要么更改函数以接受 object , 将 object 转换为 int

<p><%# MyFunctions.getDocumentCategory( Convert.ToInt32( Eval("DocumentID") ) )%></p>

public static string getDocumentCategory(object document)
{
     int documentID = Convert.ToInt32( document );

     etc...
}
问题回答

多亏了杜泽的好解释和榜样

第二种方法 -- -- 接受对象并在您自定义函数内进行转换 -- -- 可能更适合保持转换代码清洁。结果相等。

仅添加一点点 - 您可以在转换时使用 Kentico s validation Helper , 例如 :

变换 :

<%# MyFunctions.getDocumentCategory(Eval("DocumentID"))%>

代码 :

public static string getDocumentCategory(object docID)
{
   int documentID = ValidationHelper.GetInteger(docID, 0); //0 is the default value
   ...




相关问题
Kenitico CMS not getting configured

I am configuring the Kentico CMS to my local machine but getting the following error: The CISM115cis account is not granted with Modify permission on folder C:inetpubwwwrootKenticoCMS. ...

Kentico: How to redirect based on document type field

I have a certain document type in Kentico that has a boolean field that when true i need the page to redirect to another URL (in this case a 404 page). Where is the best place to do this? and how ...

Kentico CMS & Campaign Monitor

Just wondering if anyone has any pointers on how to integrate campaign monitor with Kentico CMS. Kentico has its own built in newsletter signup but I want campaign monitor to be updated when a user ...

Would developing in different CMS systems be beneficial?

At this point we are developing Sitecore websites and we are gaining experience every day. This means that we know how to adjust our approach to different types of customers and that we are able to ...

How to set up 301 redirects in Kentico CMS

How would you approach setting up 301 redirects within Kentico CMS (v5.0)? I want to provide a client with an easy way (ideally through the CMS Desk interface) to set up 301 redirects in a website ...

Kentico CMS search results

How do I change the Kentico CMS search settings so as to display a part of text from search results as in Google? Presently it shows only the path in the results.

热门标签