We help IT Professionals succeed at work.

Display Data in Textbox - in a single column

Cyberzones
Cyberzones asked
on
190 Views
Last Modified: 2012-05-05
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!!
Comment
Watch Question

Author

Commented:
note the separation of each item is a semicolon

Commented:
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";
}

Commented:
string[] rows = inputString.Split(';');
should be
string[] rows = inputString.Split(new char[]{';'});

I guess

Author

Commented:
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?
CERTIFIED EXPERT
Top Expert 2007

Commented:
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

Author

Commented:
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
CERTIFIED EXPERT
Top Expert 2007

Commented:
did you use Eval instead of Bind? I tested that before I posted...
CERTIFIED EXPERT
Top Expert 2007

Commented:
hang on a bit, was testing in a VB project, let me fire up my c# test project

Author

Commented:
I took all of your textbox code and replaced mine.
CERTIFIED EXPERT
Top Expert 2007

Commented:
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

CERTIFIED EXPERT
Top Expert 2007

Commented:
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

Author

Commented:
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'
CERTIFIED EXPERT
Top Expert 2007

Commented:
You mean a formview control?

Author

Commented:
Yes, in a FormView control.
CERTIFIED EXPERT
Top Expert 2007
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
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!

Author

Commented:
samtran0331 is a great help! Thanks!!
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.