0

I'm creating an ASP.NET web form application that searches PDFs for text, if the PDF has that text, it returns the name of the file and the path in a text box. So far everything works but now I'm ready to take that full path and put it inside a link.

So for example, a result would be:

https:\\www.mysite.com\example1.PDF

I want to show a hyper link in the text box that is clickable and simply labeled as:

Example 1

I wonder if I made a mistake in going with ASP.NET Web Forms to do this since it only has a Text Box form control and not a rich text box. The thing is my results are being stored in a string builder so it's not like I just need one static label to be a URL.

Here is what I have:

foreach (var f in files)
            {
                string pdfSearchMatch = ReadPdfFile(f.File, txtBoxSearchString.Text);
                if (pdfSearchMatch != null)
                {

                    string fileNameOnly = Regex.Replace(pdfSearchMatch, @"\\\\my\.test\.site@SSL\\Home\\Sections\\PDFs\\Courses\\.+?\\.+?\\.+?\\", "");
                    myCommand.CommandText = "select cc.pk1 as 'pid', f.file_name as 'xid', f.link_name as 'linkname' from course_contents cc join course_main cm on cc.crsmain_pk1 = cm.pk1 join course_contents_files ccf on cc.pk1 = ccf.course_contents_pk1 join files f on ccf.files_pk1 = f.pk1 where cm.course_id = 'COM-Syllabi' and f.link_name = '"+fileNameOnly+"'";
                    using (SqlDataReader sqlReader = myCommand.ExecuteReader())
                    {
                        while (sqlReader.Read())
                        {
                            string rid = Regex.Replace(sqlReader["xid"].ToString(), "/xid", "rid");
                            string pdfHyperlink = @"https://my.test.site/Home/pid-"+sqlReader["pid"].ToString()+"-dt-content-"+rid+sqlReader["xid"].ToString();
                            sb.AppendLine(pdfHyperlink);
                        }
                    }
                }
            }
            myConnection.Close();
            txtBoxResults.Text = sb.ToString();

I want the results that get assigned to txtBoxResults.Text to be clickable links.

Thanks.

Christopher Bruce
  • 661
  • 3
  • 10
  • 24
  • So instead of using a TextBox control, you want to use a HyperLink control? – mason Jul 01 '14 at 20:54
  • Can I use a single HyperLink control to display 0 to many results dynamically and in a top down list?(each "row" would be a different link) – Christopher Bruce Jul 01 '14 at 20:54
  • Yes, inline c# in your aspx will make it easier – Rameez Ahmed Sayad Jul 01 '14 at 20:55
  • You cant have clickable links inside text boxes and its not a limitation in asp.net, just HTML. Why do you need to output the result in a textbox? – HaukurHaf Jul 01 '14 at 20:56
  • There are several ways to go about this. The easiest would be to have a panel, and in the results create dynamic hyperlinks and add them to the children collection of the panel. Or you can use a literal control and just output pure html. – Smeegs Jul 01 '14 at 20:58
  • Any good examples on using panels? – Christopher Bruce Jul 01 '14 at 21:00
  • @jadarnel27: Just a question , how different would inline c# be to HtmlRender in MVC using Model lets say even in my ASP.NET , I have my model classes defined. – Rameez Ahmed Sayad Jul 01 '14 at 21:01
  • So is anyone going to try and answer the question? It's great and all that you can recommend panels or literal controls but I'm not finding any great examples on how to use them with string arrays, lists, or string builders, just static text objects...an example would be helpful. – Christopher Bruce Jul 01 '14 at 21:17

1 Answers1

1

This is the smallest and quickest fix for your code , but it's inappropriate whereever you have your textbox , just create an

<asp:Literal ID="Literal1" runat="server" ></asp:Literal>

and the exact same line

txtBoxResults.Text = sb.ToString();

replace with

Literal1.Text = sb.ToString();

See how it shows , haven't tested . Still would suggest to resolve the SQL Injection and extra load on the server connection keeping it open.

UPDATE:- Add the hyperlink part try replace with this the same line , ay face issues with the escape

string pdfHyperlink = @"<a href='https://my.test.site/Home/pid-" + sqlReader["pid"].ToString() + "-dt-content-" + rid + sqlReader["xid"].ToString()+"'>Test</a>";
Rameez Ahmed Sayad
  • 1,300
  • 6
  • 16
  • 29
  • I'll give it a shot, yes I am going to resolve the SQL Injection probably tomorrow, the page isn't live so there's no threat at the moment. Also, I'm closing the SQL Server connection it's just in the code I put above in the question. – Christopher Bruce Jul 01 '14 at 21:22
  • Cool . What I meant by the extra load , was doing all the processing inside the `Read()` , just assign the `SQL` values to a `List<>` (basically disconnected object) and then close the SqlConnection and do any processing(making the links etc.) after that – Rameez Ahmed Sayad Jul 01 '14 at 21:25
  • All of the results from the string builder are running on together even though I used appendLine, is there a property that will make it display in a top to bottom format instead of word wrapping? – Christopher Bruce Jul 01 '14 at 21:28
  • I just went ahead and added a
    to the stringbuilder each time it runs through the sqlReader.
    – Christopher Bruce Jul 01 '14 at 21:34