Search code examples
c#htmlasp.netaspxgridview

Adding HTML line break in a aspx view dynamically


I am new to C# and I am stuck at a point where I would like to add a line break in a .aspx view based on the data being populated.

Please find below my code below:

    <asp:GridView ID="SupGridView" runat="server" AutoGenerateColumns="False" CssClass="gridView" AllowPaging="true" PageSize="10" 
                        OnRowDataBound="OnRowDataBound" OnPageIndexChanging="OnPageIndexChanging" OnSelectedIndexChanged="OnSelectedIndexChanged">
                        <Columns>
                            <asp:TemplateField HeaderText="Last Name, First Name" ItemStyle-CssClass="wide" HeaderStyle-CssClass="wide">
                                <ItemTemplate>
                                    <asp:LinkButton ID="btbLink3" runat="server" Text='<%# Bind("full_name_reverse_with_suffix") %>' CommandArgument='<%# Bind("eds_account_id") %>'
                                        OnClick="getUserDetails"></asp:LinkButton>
                                    <asp:Label ID="phonetic"   Style="word-wrap: normal; word-break: break-all;" runat="server" Text='<%# Eval("name_phonetic") %>'></asp:Label>
                                </ItemTemplate>
                                <ItemStyle HorizontalAlign="Left" />
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>

Here, I would like to check if label <%# Eval("name_phonetic") %> has any value then add a line break
.

I would really appreciate your guidance.

Thanks


Solution

  • Ok, we actually have "several" choices here.

    However, for things like say change the color of a box based on some data, or highlight the row, or concatenate a few values, change bold fonts etc. based on the "data" for the grid view?

    I would use the row data bound event. You could as noted use in-line code, and say even a in-line iff statement.

    So, you could for example, say use this:

    <asp:Label ID="phonetic"   
       Style="word-wrap: normal; word-break: break-all;" 
       runat="server" 
       
       Text='<%# Eval("name_phonetic").ToString() == "" ? "" 
       : Eval("name_phonetic").ToString() + @"<br/><br/>"   %>'>
     </asp:Label>
     
    

    Now, really, the above expression is quite much enough to send developers to some hospital with padded walls.

    So, as I noted, adopt the "standard" that your formatting code such has highlighting a text box based on some value, bolding text, or in this case adding a extra line break?

    Put all such code in "one place", "one spot", and in the code behind as opposed to the "messy jungle" of markup code.

    So, use the row data bound event, and do this:

    Your label now becomes this:

    <asp:Label ID="phonetic" 
        Style="word-wrap: normal; word-break: break-all;" 
        runat="server" >
    </asp:Label>
    

    And then in row data bound, then this:

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow) 
            {
                DataRowView rData = e.Row.DataItem as DataRowView;
                Label lPhon = e.Row.FindControl("phonetic") as Label;
                lPhon.Text = rData["name_phonetic"].ToString();
                if (lPhon.Text != "") 
                    lPhon.Text += @"<br/>";
            }
        }
    

    Now, that is "more" code, but it is code you can grasp, and read, and go back to.

    And in that code, you now have the full data row used for binding, so EVEN columns you don't display in the GV can be accessed and used. (so say for some discount rate, you can then color controls, or do whatever). And as noted, you have use of values from that data row NOT displayed or shown in the GV.

    So, either of the 2 approaches will work, but you might as well "start" a code stub for special formatting, and all that code can "always" be placed in one spot, and one location that you as a developer will now "know" to look for in the future.