Link to home
Start Free TrialLog in
Avatar of Cyberzones
Cyberzones

asked on

Display Data in Textbox - in a single column

Hello,

Code: ASP.NET; C# , MS SQL 2005

I have a TextBox setup with multiline code below:

<asp:TextBox ID="CategoryListBox" runat="server" Text='<%# Bind("Categories") %>' TextMode="MultiLine" BorderStyle="None" BorderWidth="0px" ReadOnly="True" Rows="20" Font-Names="Verdana" Font-Size="Small" Width="300px"></asp:TextBox>

I have data in a MS SQL datadase column formatted as this example:

XYZ Name, color blue, XL
WRX Name, Color red, M
RTZ Name, collar blue, SM

I have a form that takes the data above and puts it in a single database cell and is now formatted like this (note ; between entries):

XYZ Name, color blue, XL; WRX Name, Color red, M; WRX Name, Color red, M

Now the question&

I need the TextBox above to display the items from the database cell like this:

XYZ Name, color blue, XL
WRX Name, Color red, M
RTZ Name, collar blue, SM

It should list the items in a single column. Somehow I should be able to designate in the code when it sees ; to move to the next list, display the data until the contents of the cell are displayed.

Thanks for your help!!
Avatar of Cyberzones
Cyberzones

ASKER

note the separation of each item is a semicolon
do you mean something like this?

say inputString = "XYZ Name, color blue, XL; WRX Name, Color red, M; WRX Name, Color red, M";

string[] rows = inputString.Split(';');
foreach(string row in rows)
{
    myTextBox.Text = row + "\r\n";
}
string[] rows = inputString.Split(';');
should be
string[] rows = inputString.Split(new char[]{';'});

I guess
Let me clarify&

There is not input string. The textbox is databound to a field in a table/database.

This page is displaying company information selected by the user.

So when the page loads, I need the data to be displayed as indicated above.

This code obviously gives me an inputString error:

void Page_Load()
    {
        string[] rows = inputString.Split(new char[] { ';' });
        foreach (string row in rows)
        {
            CategoryTextBox.Text = row + "\r\n";
        }

    }

 So what do you think?
try:
                            <asp:TextBox ID="CategoryListBox" runat="server" BorderStyle="None" BorderWidth="0px"
                                Font-Names="Verdana" Font-Size="Small" ReadOnly="True" Rows="20" Text='<%# Eval("Categories").ToString.Replace(";",Environment.NewLine) %>'
                                TextMode="MultiLine" Width="300px"></asp:TextBox>

Open in new window

Hi samtran0331, Thanks for your help.

I tried your code but it is giving the following error:

'object.ToString()' is a 'method', which is not valid in the given context
did you use Eval instead of Bind? I tested that before I posted...
hang on a bit, was testing in a VB project, let me fire up my c# test project
I took all of your textbox code and replaced mine.
I ended up having to do it in code....not sure what type of data control you're using, but the sample below uses a gridview:
<%@ Page Language="C#" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<script runat="server">
 
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            TextBox tb = (TextBox)e.Row.FindControl("CategoryListBox");
            tb.Text = tb.Text.Replace(";", Environment.NewLine);
        } 
    }
</script>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
                SelectCommand="SELECT 'XYZ Name, color blue, XL; WRX Name, Color red, M; WRX Name, Color red, M' AS Categories
 FROM [Customers]"></asp:SqlDataSource>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
                OnRowDataBound="GridView1_RowDataBound">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:TextBox ID="CategoryListBox" runat="server" BorderStyle="None" BorderWidth="0px"
                                Font-Names="Verdana" Font-Size="Small" ReadOnly="True" Rows="20" Text='<%# Bind("Categories") %>'
                                TextMode="MultiLine" Width="300px"></asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </div>
    </form>
</body>
</html>

Open in new window

If there is always going to be a space after the semi-colon, a quick way to line up each line is if you "replace" using "; "


but the key to this is just replacing a semi-colon with Environment.NewLine

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            TextBox tb = (TextBox)e.Row.FindControl("CategoryListBox");
            tb.Text = tb.Text.Replace("; ", Environment.NewLine);
        } 
    }

Open in new window

I have a Form and I am displaying the textbox in the ItemTemplate.

I am trying to replace the grid specific code with the form code but I cannot find the definition for 'Row'
You mean a formview control?
Yes, in a FormView control.
ASKER CERTIFIED SOLUTION
Avatar of samtran0331
samtran0331
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Great! Got it working!

Here is what I had to change to get it to work:

protected void Page_Load(object sender, EventArgs e)
    {
        TextBox tb = (TextBox)FormView1.Row.FindControl("CategoryListBox");
        tb.Text = tb.Text.Replace(";", Environment.NewLine);
    }


Thanks for all of your help samtran0331!
samtran0331 is a great help! Thanks!!