GridView - Переход на новую строку

Dark_Scorpion
Дата: 23.04.2007 08:06:15
В GridView есть одна колонка с текстом. В режиме редактирования появляется многострочная область для редактирования (TextBox). В ней текст отображается правильно, со всеми раставленными переходами на новую строчку. В режиме просмотра (Label) текст склеивается в одну длинную строчку.
1) Как сделать так чтобы этого склеивания не происходило?
2) Как сделать выравнивание текста в столбце "по ширине"?

    <asp:gridview ID="grid_news" runat="server" SkinID="cold_gridview"
        DataSourceID="sql_news" 
        DataKeyNames="id"
        AutoGenerateColumns="False">
        <Columns>
            <asp:CommandField
                ShowEditButton="True"
                EditText="Изменить" CancelText="Отмена" UpdateText="Принять" />

            <asp:TemplateField HeaderText="Новости">
                <ItemTemplate>
                        <asp:Label runat="server" ID="label_news_text" 
                            Width="98%" 
                            Text='<%# Bind("news_text") %>' />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="tx_news_text" runat="server" 
                        Wrap="true"
                        TextMode="MultiLine"
                        Rows="10"
                        Text='<%# Bind("news_text") %>' />
                </EditItemTemplate>
                <HeaderStyle Width="100%" />
            </asp:TemplateField>                
        </Columns>
    </asp:gridview>
C...R...a...S...H
Дата: 23.04.2007 09:41:57
Необходимо заменять символы перевода строки на br
или использовать тег pre
----------------------------------------
Knowledge is P...O...w...E...R!
My site
Dark_Scorpion
Дата: 23.04.2007 09:54:55
а в какой момент времени необходимо делать подмену символов конца строки на тэг <br>?
vladgrig
Дата: 23.04.2007 10:36:05
<asp:gridview ID="grid_news" runat="server" SkinID="cold_gridview"
        DataSourceID="sql_news" 
        DataKeyNames="id"
        AutoGenerateColumns="False">
        <Columns>
            <asp:CommandField
                ShowEditButton="True"
                EditText="Изменить" CancelText="Отмена" UpdateText="Принять" />

            <asp:TemplateField HeaderText="Новости">
                <ItemTemplate>
                        <asp:Label runat="server" ID="label_news_text" 
                            Width="98%" 
                            Text='<pre><%# Bind("news_text") %></pre>' />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="tx_news_text" runat="server" 
                        Wrap="true"
                        TextMode="MultiLine"
                        Rows="10"
                        Text='<%# Bind("news_text") %>' />
                </EditItemTemplate>
                <HeaderStyle Width="100%" />
            </asp:TemplateField>                
        </Columns>
</asp:gridview>
честно сказать не пробовал сам - но, ИМХО - должно работать... ;)
Dark_Scorpion
Дата: 23.04.2007 13:21:22
vladgrig
Text='<pre><%# Bind("news_text") %></pre>'

Не работает :(
В этом случае содержимого этого текстового поля вообще не отображается
vladgrig
Дата: 23.04.2007 17:34:11
автор
1) Как сделать так чтобы этого склеивания не происходило?

.......
.......
<style type="text/css">
    .myStyle
    {
    white-space:pre;
    font-size:12px;
    width:98%;
    }  
</style>
.......
.......
<ItemTemplate>
     <asp:Label runat="server" ID="label_news_text"                             
                                                 CssClass="myStyle"
                                                       Text='<%# Bind("news_text") %>' />
</ItemTemplate>
Dark_Scorpion
Дата: 24.04.2007 08:23:47
vladgrig
<style type="text/css">
.myStyle
{
white-space:pre;
font-size:12px;
width:98%;
}
</style>

Переходы на новую строчку появляются между абзацами, зато каждый абзац отображается в одну строчку, из-за чего текстовую область распёрло на несколько экранов вправо.

Наверно правильнее прописать команду замены символа перехода строки на тэг <br>. Тока я не знаю в каком событии надо это сделать - либо в одном из событий в SqlDataSource, либо в GridView. Допустим что-то типа:
    protected void grid_news_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        ((Label)e.Row.FindControl("label_news_text")).Text.Replace(chr(13),"<br/>");
    }
Только команда записана некорректно - "chr(13)" выдаёт ошибку
vladgrig
Дата: 24.04.2007 10:26:56
тады сюда - парик вообще написал свой класс для этого ;)
Dark_Scorpion
Дата: 24.04.2007 10:42:33
У метода Replace есть 2 набора параметров:
- либо меняем CHAR на CHAR
- либо STRING на STRING

Сделал так:
    protected void grid_news_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        char c = (char)13;
        string s = Convert.ToString(c);
        ((Label)e.Row.FindControl("label_news_text")).Text.Replace(s,"<br>");
    }
Возникает ошибка:
Object reference not set to an instance of an object.


А как использовать класс в своём проекте?
Создал новый класс, добавил в него приведёный код. При компиляции пишутся ошибки:
The type or namespace name 'Match' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'Bindable' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'Category' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'DefaultValue' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'Localizable' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'DefaultProperty' could not be found (are you missing a using directive or an assembly reference?)


Видимо нужно подсоединить какие-то нэймспэйсы. А какие?
Dark_Scorpion
Дата: 24.04.2007 10:51:22
protected void grid_news_RowDataBound(object sender, GridViewRowEventArgs e)
{
((Label)e.Row.FindControl("label_news_text")).Text.Replace("\r\n", "<br/>");
}
Снова та же ошибка
Object reference not set to an instance of an object.