Programatically altering text before rendering

J

James R. Davis

A complete newbie here (to ASP.NET) though lots of years of ASP programming.
Still, some things are not yet very obvious to me.

I have a SQL table that includes a TEXT field that often exceeds 20,000
bytes in size. Each such field contains an HTML page that I want to render
for my users via a Formview.

The problem is that within that field some of the tags are in forum format
instead of HTML format. For example, an IMG tage is "" instead of
"<IMG src ..." format.

So, I believe what I must do is create a control that is given control at
databound time (FormView1_DataBound) and in that control I will do so
replace string manipulations.

Of all the silly things to get in the way I cannot seem to find a way to get
to that field.

The formview looks like this:

--------------------------------------------
<%@ Page Language="VB" MasterPageFile="~/Default.master" Title="Tip Article"
%>

<script runat="server">


Protected Sub FormView1_DataBound(ByVal sender As Object, ByVal e As
System.EventArgs)

End Sub

</script>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
Runat="Server">
<BR />
<div class="fullwidth">
<h2>
<%
Dim Num As Integer

Num = Request.QueryString("Num")
%>
&nbsp;<asp:FormView ID="FormView1" runat="server"
DataSourceID="SqlDataSource1" OnDataBound="FormView1_DataBound">
<ItemTemplate>
<asp:Label ID="T_MESSAGELabel" runat="server"
Text='<%# Bind("T_MESSAGE") %>'></asp:Label><br />
</ItemTemplate>
</asp:FormView>
&nbsp; &nbsp;
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:MTT.mdfConnectionString %>"
SelectCommand="SELECT [T_MESSAGE] FROM [forum_topics]
WHERE ([T_SUBJECT] LIKE @T_SUBJECT + '%')">
<SelectParameters>
<asp:QueryStringParameter DefaultValue="001"
Name="T_SUBJECT" QueryStringField="Num"
Type="String" />
</SelectParameters>
</asp:SqlDataSource>
</h2>
<p>

</div>
</asp:Content>
----------
As you can see, the field name is "T_MESSAGE".

How do I get to it within the FormView1_DataBound Sub?

Thank you for any assistance.
 
K

Kevin Frey

It sounds to me like what you want to do is create your own
BoundColumn-derived class which provides you with the necessary conversion
behaviours.

In your particular case I would derive a new class off BoundField and
perhaps override the FormatDataValue method. OnDataBindField is an
alternative place for overloading the behaviour (perhaps a tad more work).

I have used this technique to produce a DateTimeBoundField which had the
special ability to perhap time-zone conversions to the client's local time
zone prior to display (all data from the database was stored in UTC).

Hope this helps.
 
D

Dave Bush

This situation is exactly why I use the ObjectDataSource for ALL of my
development. The SqlDataSource, while relatively easy to use, has it's
limitations. If you structure your development so that you have a View
(aspx and aspx.cs or aspx.vb combination) that calls a Business Logic
Layer aka BLL (cs or vb file in app_code) that calls your DataSet code
you can do all of your search and replace in the BLL and your View can
databind to it without any fancy hocus pocus.

Unfortunately, the exact HOW of doing this would consume an entire
article, which really isn't the purpose of a news group. I'm sure if
you search ObjectDataSource and n-tier architecture in ASP.NET you'll
find plenty of examples as this is pretty standard Microsoft designated
architecture and there should be tons of articles already written about
how to do this.

As I'm consulting with new ASP.NET programmers, I don't even show them
the SqlDataSource any more. While it seems like it ought to be easy to
use, it ends up being more difficult to use than an ODS. This is true
even for relatively straight forward pages and once you need to do
something just a bit out of the ordinary, you find that it is quite
inflexible as well.

If you insist on using the SqlDataSource instead, a hack would be to do
the search and replace in a stored proc and return it from there. But,
in my opinion that puts functionality that SHOULD be in your code down
in the database where is doesn't belong.


Dave Bush
http://blog.dmbcllc.com




-----Original Message-----
From: James R. Davis [mailto:[email protected]]
Posted At: Thursday, November 29, 2007 12:25 PM
Posted To: microsoft.public.dotnet.framework.aspnet
Conversation: Programatically altering text before rendering
Subject: Programatically altering text before rendering

A complete newbie here (to ASP.NET) though lots of years of ASP
programming.
Still, some things are not yet very obvious to me.

I have a SQL table that includes a TEXT field that often exceeds 20,000
bytes in size. Each such field contains an HTML page that I want to
render
for my users via a Formview.

The problem is that within that field some of the tags are in forum
format
instead of HTML format. For example, an IMG tage is "" instead of
"<IMG src ..." format.

So, I believe what I must do is create a control that is given control
at
databound time (FormView1_DataBound) and in that control I will do so
replace string manipulations.

Of all the silly things to get in the way I cannot seem to find a way to
get
to that field.

The formview looks like this:

--------------------------------------------
<%@ Page Language="VB" MasterPageFile="~/Default.master" Title="Tip
Article"
%>

<script runat="server">


Protected Sub FormView1_DataBound(ByVal sender As Object, ByVal e As
System.EventArgs)

End Sub

</script>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
Runat="Server">
<BR />
<div class="fullwidth">
<h2>
<%
Dim Num As Integer

Num = Request.QueryString("Num")
%>
&nbsp;<asp:FormView ID="FormView1" runat="server"
DataSourceID="SqlDataSource1" OnDataBound="FormView1_DataBound">
<ItemTemplate>
<asp:Label ID="T_MESSAGELabel" runat="server"
Text='<%# Bind("T_MESSAGE") %>'></asp:Label><br />
</ItemTemplate>
</asp:FormView>
&nbsp; &nbsp;
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:MTT.mdfConnectionString %>"
SelectCommand="SELECT [T_MESSAGE] FROM
[forum_topics]
WHERE ([T_SUBJECT] LIKE @T_SUBJECT + '%')">
<SelectParameters>
<asp:QueryStringParameter DefaultValue="001"
Name="T_SUBJECT" QueryStringField="Num"
Type="String" />
</SelectParameters>
</asp:SqlDataSource>
</h2>
<p>

</div>
</asp:Content>
----------
As you can see, the field name is "T_MESSAGE".

How do I get to it within the FormView1_DataBound Sub?

Thank you for any assistance.
 
J

James R. Davis

I'm not at all sure I understand why the negativism to my, what I thought,
was a simple question, but I'll probably get a dose of it sooner or later as
I get deeper into coding.

In the meantime, I tried something simple and it worked. I wrote a trivial
function (FormatMsg) as follows:

Public Function FormatMsg(ByVal msg As Object) As String
Return Replace(msg,...)
End Function

And changed the Item Template that rendered the field I was interested so
that it now calls the function as follows:

....<%# FormatMsg(EVAL("T_MESSAGE")) #%> ...

And since the function is called before the field is rendered it does
exactly what I wanted it to do.

But, please, I still need to know how I would have referred to that field in
a Formview DataBound event subroutine. Any help would really be
appreciated.

Dave Bush said:
This situation is exactly why I use the ObjectDataSource for ALL of my
development. The SqlDataSource, while relatively easy to use, has it's
limitations. If you structure your development so that you have a View
(aspx and aspx.cs or aspx.vb combination) that calls a Business Logic
Layer aka BLL (cs or vb file in app_code) that calls your DataSet code
you can do all of your search and replace in the BLL and your View can
databind to it without any fancy hocus pocus.

Unfortunately, the exact HOW of doing this would consume an entire
article, which really isn't the purpose of a news group. I'm sure if
you search ObjectDataSource and n-tier architecture in ASP.NET you'll
find plenty of examples as this is pretty standard Microsoft designated
architecture and there should be tons of articles already written about
how to do this.

As I'm consulting with new ASP.NET programmers, I don't even show them
the SqlDataSource any more. While it seems like it ought to be easy to
use, it ends up being more difficult to use than an ODS. This is true
even for relatively straight forward pages and once you need to do
something just a bit out of the ordinary, you find that it is quite
inflexible as well.

If you insist on using the SqlDataSource instead, a hack would be to do
the search and replace in a stored proc and return it from there. But,
in my opinion that puts functionality that SHOULD be in your code down
in the database where is doesn't belong.


Dave Bush
http://blog.dmbcllc.com




-----Original Message-----
From: James R. Davis [mailto:[email protected]]
Posted At: Thursday, November 29, 2007 12:25 PM
Posted To: microsoft.public.dotnet.framework.aspnet
Conversation: Programatically altering text before rendering
Subject: Programatically altering text before rendering

A complete newbie here (to ASP.NET) though lots of years of ASP
programming.
Still, some things are not yet very obvious to me.

I have a SQL table that includes a TEXT field that often exceeds 20,000
bytes in size. Each such field contains an HTML page that I want to
render
for my users via a Formview.

The problem is that within that field some of the tags are in forum
format
instead of HTML format. For example, an IMG tage is "" instead of
"<IMG src ..." format.

So, I believe what I must do is create a control that is given control
at
databound time (FormView1_DataBound) and in that control I will do so
replace string manipulations.

Of all the silly things to get in the way I cannot seem to find a way to
get
to that field.

The formview looks like this:

--------------------------------------------
<%@ Page Language="VB" MasterPageFile="~/Default.master" Title="Tip
Article"
%>

<script runat="server">


Protected Sub FormView1_DataBound(ByVal sender As Object, ByVal e As
System.EventArgs)

End Sub

</script>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
Runat="Server">
<BR />
<div class="fullwidth">
<h2>
<%
Dim Num As Integer

Num = Request.QueryString("Num")
%>
&nbsp;<asp:FormView ID="FormView1" runat="server"
DataSourceID="SqlDataSource1" OnDataBound="FormView1_DataBound">
<ItemTemplate>
<asp:Label ID="T_MESSAGELabel" runat="server"
Text='<%# Bind("T_MESSAGE") %>'></asp:Label><br />
</ItemTemplate>
</asp:FormView>
&nbsp; &nbsp;
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:MTT.mdfConnectionString %>"
SelectCommand="SELECT [T_MESSAGE] FROM
[forum_topics]
WHERE ([T_SUBJECT] LIKE @T_SUBJECT + '%')">
<SelectParameters>
<asp:QueryStringParameter DefaultValue="001"
Name="T_SUBJECT" QueryStringField="Num"
Type="String" />
</SelectParameters>
</asp:SqlDataSource>
</h2>
<p>

</div>
</asp:Content>
----------
As you can see, the field name is "T_MESSAGE".

How do I get to it within the FormView1_DataBound Sub?

Thank you for any assistance.
[/QUOTE]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top