如何调用OnpreRender方法? [英] How to call OnpreRender method?

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

问题描述

亲爱的,



我正在尝试创建一个功能,它会在文章的内容中显示标签的名称作为超链接。



我试图创建一个onPreRender方法,它计算上述任务。但是,下面的代码无法在客户端的文章内容中输出标记名称作为超链接。

Dear all,

I am trying to create a functionality,which will display tag's name as hyperlinks, within the content of the article.

I have attempted to create a onPreRender method, which computes the above task. However, the code below, is failing to output tag names, as hyperlinks, in the article content, on the client-side.

protected override void OnPreRender(EventArgs e)
 {
     var ctrl = new HtmlGenericControl("div");
   string html = this.GetArticleText();
   string link = this.CreateLinkHtml();
   string tags = this.LoadAllTags();

          html.Replace(tags, link);

      ctrl.InnerHtml = html;
      this.txtDescription.Controls.Add(ctrl);
     base.OnPreRender(e);
 }



GetSArticleText(),CreateLinkHTML()和LoadAllTags()是返回字符串的方法,作为输出。我不太确定,我可能会出错,在调试代码时,我应该寻找什么参数?



GetSArticleText(),CreateLinkHTML()和LoadAllTags() 方法:


GetSArticleText(), CreateLinkHTML() and LoadAllTags(), are method which return a string, as an output. I am little unsure, where I may be going wrong and when debugging the code, what parameter should i be looking for?

GetSArticleText(), CreateLinkHTML() and LoadAllTags() Methods:

  public string CreateLinkHtml()
        {
            string tag = "";
            tag = @"###########";
            return tag;
        }
		
-----------------------------------------
   public string GetArticleText()
        {
        string htmlStr = "";
        string strConnectionString = ConfigurationManager.ConnectionStrings["######"].ConnectionString;
        SqlConnection thisConnection = new SqlConnection(strConnectionString);
        SqlCommand thisCommand = thisConnection.CreateCommand();
        thisCommand.CommandText = "SELECT top 1 title, body from [article] order by UploadDate desc";
        thisConnection.Open();
        SqlDataReader reader = thisCommand.ExecuteReader();
        while (reader.Read())
        {          
            string Name = reader.GetString(0);
            string Pass = reader.GetString(1);

            Pass = Pass.Replace("<p>", "");
            Pass = Pass.Replace("<p>", "");
            Pass = Pass.Replace("</p>", "");
            htmlStr += "<tr><h2>" + Name + "</h2></tr><tr>" + Pass + "</tr>";
        }
        thisConnection.Close();
        return htmlStr;
        }

-------------------------------------------
  public string LoadAllTags()
        {

            string table = "";
            try
            {
                string tag = "";=          
                string strConnectionString = ConfigurationManager.ConnectionStrings["#######"].ConnectionString;
                SqlConnection myConnect = new SqlConnection(strConnectionString);
                string strCommandText = "select [fullname] from [dbo].[tag_Library]";
                SqlCommand cmd = new SqlCommand(strCommandText, myConnect);
                cmd.Parameters.AddWithValue("fullname", tag);
                myConnect.Open();             
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    string result = reader.GetString(0);

                    table += "<tr>" + result + "</tr>";
                 
                }
                myConnect.Close();                
            }
            catch (Exception ex)
            {
                throw ex;

            }
            return table;
            }



客户端HTML代码:


Client-side HTML code:

<asp:TextBox

                    ID="txtDescription"

                    runat="server"

                    TextMode="MultiLine"

                    Height="150px"

                    OnClientClick= "tinyMCE.triggerSave(false,true);"

                    Width="400px"></asp:TextBox>
    <br />
 <table width="100%" align="center" cellpadding="2" cellspacing="2" border="0" >                      
        <%=GetArticleText()%>
    </table>







htmlStr += "<tr><td><h2>" + Name + "</h2></td></tr><tr><td>" + Pass + "</td></tr>";





如果可能请提供建议。



谢谢你



Please advice, if possible.

Thank you

推荐答案

你的 OnPreRender 方法调用;它只是没有做你认为它正在做的事情。



String.Replace 只会替换一个子字符串精确匹配要替换的字符串。由于您的文章正文不太可能包含所有标记名称,按顺序包围< tr> ...< / tr> 标记,因此不会找到一个匹配。



另外,正如Sinisa Hajnal指出的那样,你不能在一个内部添加 HtmlGenericControl TextBox control。



修复此问题的最简单方法可能是构建常规表达式 [ ^ ]:

Your OnPreRender method is being called; it's just not doing what you think it's doing.

String.Replace will only replace a sub-string which precisely matches the string to replace. Since it's unlikely that your article body contains all of your tag names, in order, surrounded by <tr>...</tr> tags, it's not going to find a match.

Also, as Sinisa Hajnal pointed out, you can't add a HtmlGenericControl inside a TextBox control.

The easiest option to fix this is probably to build a Regular Expression[^] for the tag names:
public string GetArticleText()
{
    string strConnectionString = ConfigurationManager.ConnectionStrings["######"].ConnectionString;
    const string commandText = "SELECT top 1 title, body from [article] order by UploadDate desc";
    
    // Wrap disposable objects in a "using" block:
    using (SqlConnection thisConnection = new SqlConnection(strConnectionString))
    using (SqlCommand thisCommand = new SqlCommand(commandText, thisConnection))
    {
        thisConnection.Open();
        
        using (SqlDataReader reader = thisCommand.ExecuteReader(CommandBehavior.CloseConnection | CommandBehavior.SingleResult | CommandBehavior.SingleRow))
        {
            // No need for a "while" loop, as you're only returning a single row:
            if (!reader.Read()) return string.Empty;
            
            string title = reader.GetString(0);
            string body = reader.GetString(1);
            
            // This regular expression replicates the three "Replace" calls from the current code.
            body = Regex.Replace(body, "</?p(>|&gt;)", string.Empty);
            
            return "<tr><td><h2>" + title + "</h2></tr><tr><td>" + body + "</td></tr>";
        }
    }
}

public Regex LoadAllTags()
{
    string strConnectionString = ConfigurationManager.ConnectionStrings["######"].ConnectionString;
    const string commandText = "select [fullname] from [dbo].[tag_Library]";
    
    using (SqlConnection thisConnection = new SqlConnection(strConnectionString))
    using (SqlCommand thisCommand = new SqlCommand(commandText, thisConnection))
    {
        thisConnection.Open();
        
        using (SqlDataReader reader = thisCommand.ExecuteReader(CommandBehavior.CloseConnection | CommandBehavior.SingleResult))
        {
            List<string> result = new List<string>();
            while (reader.Read())
            {
                string tagName = reader.GetString(0);
                
                // Escape any "special" characters which could break the regular expression:
                tagName = Regex.Escape(tagName);
                
                result.Add(tagName);
            }
            
            if (result.Count == 0)
            {
                return null;
            }
            
            // Create a regular expression to match any of the tag names:
            string pattern = string.Join("|", result);
            return new Regex(pattern, RegexOptions.IgnoreCase);
        }
    }
}

public string CreateLinkHtml(string tagName)
{
    // By using a MatchEvaluator (below), you can now pass the matched tag name to this method.
    ...
}

protected override void OnPreRender(EventArgs e)
{     
    string html = GetArticleText();
    
    Regex tags = LoadAllTags();
    if (tags != null)
    {
        // The MatchEvaluator lets you examine the matched value:
        html = tags.Replace(html, match => match.Value);
    }
    
    // Can't add controls under a TextBox; set the Text property instead:
    txtDescription.Text = html;
    
    base.OnPreRender(e);
}


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

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