We help IT Professionals succeed at work.

remove comma from repeater when city is null

-Dman100-
-Dman100- asked
on
I have a repeater that displays job postings by category, like the following:

Sales
 
Account Executive
 Austin, TX, US

The city field is not required and sometimes it is not input, so the display looks like this:

Sales
 
Account Executive
 , TX, US

It has the extra comma when the city is not entered and looks weird.

How can I remove that comma?

See below for the repeater code:


<asp:Repeater ID="Repeater2" runat="server">
                        <ItemTemplate>
                            <h4><%# Eval("CategoryName") %></h4>
                            <asp:Repeater ID="Repeater1" runat="server" DataSource='<%# GetJobsByCategory((int)(Eval("CategoryID"))) %>'>
                                <HeaderTemplate>
                                    <ul>
                                </HeaderTemplate>
                                <ItemTemplate>
                                    <li><a href='<%# Eval("URL") %>' target="myIframe" onclick="javascript:document.location.href='#desc'"><%# Eval("JobTitle") %></a></li>
                                    <p style="text-indent:15px;font-size:small;"><%# Eval("City") %>, <%# Eval("State") %>, <%# Eval("Country") %></p>
                                </ItemTemplate>
                                <FooterTemplate>
                                    </ul>
                                </FooterTemplate>
                            </asp:Repeater>
                        </ItemTemplate>
                     </asp:Repeater>



code-behind:

private ADP.JobsDataTable allJobs = null;

protected void Page_Load(object sender, EventArgs e)
    {
        
        JobCategoriesBLL categoriesAPI = new JobCategoriesBLL();
        Repeater2.DataSource = categoriesAPI.GetCategories();
        Repeater2.DataBind();
    }

protected ADP.JobsDataTable GetJobsByCategory(int categoryID)
    {
        if (allJobs == null)
        {
            JobBLL jobsAPI = new JobBLL();

            return jobsAPI.GetJobsByCategory(categoryID);
        }

        allJobs.DefaultView.RowFilter = "CategoryID = " + categoryID;
        return allJobs;
    }

Open in new window

Comment
Watch Question

Can suggest you one way around. Combine all three in single column of query result as

Select city + ',' + state + ',' + country as 'add' from table

Now in repeater control user it as
<%#Eval("add").ToString().TrimEnd(',').TrimStart(',') %>

This will solve your problem
-Dman100-Software Consultant

Author

Commented:
Unfortunately, they do not want to modify the query, but rather, handle this programmitically in code.

Is there another option?
Check that attached sample.
Hope this will help you
<%# Convert.ToString("," + Eval("recordId").ToString() + "," + Eval("tholderName").ToString()).TrimStart(',') %>'

Open in new window

-Dman100-Software Consultant

Author

Commented:
That fixed the problem with the leading comma when no city is entered, but if state is entered here is how it looks:

Houston, Austin, Dallas, , US

There are two commas after the state when there is no state is entered.  Is there a way to fix that one?

Here is my code based on your last example:


<p style="text-indent:15px;font-size:small;"><%# Convert.ToString("," + Eval("City").ToString() + ", " + Eval("State").ToString() + ", " + Eval("Country").ToString()).TrimStart(',')%></p>

Open in new window

-Dman100-Software Consultant

Author

Commented:
Hi AmanBhullar,

I ended up using a function in the code-behind:

Thanks for your help!

protected string GetCityStateZip(string city, string state, string country)
    {
        string str = null;

        if (city.Length > 0 && state.Length > 0)
        {
            str = city + ", " + state + ", " + country;
        }
        else if (city.Length == 0 && state.Length == 0)
        {
            str = country;
        }
        else if (city.Length == 0)
        {
            str = state + ", " + country;
        }
        else
        {
            str = city + ", " + country;
        }

        return str;
    }


<%# GetCityStateZip(Eval("City").ToString(),Eval("State").ToString(),Eval("Country").ToString()) %>

Open in new window