Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Merge Three Fields in Gridview Itemtemplate Field into one ASP.NET 4.0

Posted on 2013-01-25
6
Medium Priority
?
785 Views
Last Modified: 2013-01-25
Hello Experts,

I have a Field phone, where I need to bind three fields into one.
    <asp:TemplateField ItemStyle-HorizontalAlign="Left">
 <HeaderTemplate>
   Phone</HeaderTemplate>
  <HeaderStyle HorizontalAlign="Left" Width="175px" />
   <ItemTemplate>
 <asp:Label ID="lblPhnNbr" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "PHN_NUMBER") !="" ? String.Format("{0:000-0000}",Convert.ToInt64(DataBinder.Eval(Container.DataItem, "NUMBER"))) : ""%>'
Font-Underline="false" ForeColor="Blue" style="display:none" />
</ItemTemplate>
</asp:TemplateField>

Open in new window


To the above code I need to Add Area Code bind field("AREA") and EXTN all equal to just one field like this:

212-222-2222 0000
0
Comment
Question by:ASPDEV
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 7

Expert Comment

by:Element1910
ID: 38820143
You are close.

Try this:
<asp:TemplateField HeaderText="PhoneNum">
            <ItemTemplate>
                <asp:Label ID="lblPhnNbr" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "PHN_NUMBER") !="" ? String.Format("{0:000-0000}",Convert.ToInt64(DataBinder.Eval(Container.DataItem, "AREA") + DataBinder.Eval(Container.DataItem, "NUMBER") + " " + DataBinder.Eval(Container.DataItem, "EXTN"))) : ""%>' Font-Underline="false" ForeColor="Blue" style="display:none" />
            </ItemTemplate>
            </asp:TemplateField>

Open in new window


Wasn't able to test it, but it should work.
0
 

Author Comment

by:ASPDEV
ID: 38820173
I was getting error:
"operator '+' cannot be applied to operands of type object and object.
0
 
LVL 7

Expert Comment

by:Element1910
ID: 38820217
Ok, then try to do this then:
<asp:TemplateField HeaderText="PhoneNum">
            <ItemTemplate>
                <asp:Label ID="lblPhnNbr" runat="server" Text='Label' Font-Underline="false" ForeColor="Blue" style="display:none">
                <%# Eval("AREA") + "-" + (String.Format("{0:000-0000}", Eval("NUMBER"))) + " " + Eval("EXTN")%>
                </asp:Label>
            </ItemTemplate>
            </asp:TemplateField>

Open in new window

0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 7

Expert Comment

by:Element1910
ID: 38820244
That didn't work either...Ok, got a running visual studio test project now. I'll get this working for you shortly :) That's what I get for trying to do things from memory :P
0
 
LVL 7

Accepted Solution

by:
Element1910 earned 2000 total points
ID: 38820376
Ok, I got it working with this in my ASPX page from my test database:

<asp:GridView ID="GridView1" runat="server">
        <Columns>
            <asp:BoundField DataField="AREA" HeaderText="AREA" visible="false"/>
            <asp:BoundField DataField="PHONE" HeaderText="PHONE" visible="false"/>
            <asp:BoundField DataField="EXTN" HeaderText="EXTN" visible="false"/>
            <asp:TemplateField HeaderText="Phone">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text="">
                        <%# Eval("AREA") + "-" + (String.Format("{0:000-0000}", Eval("PHONE"))) + " Ex:" + Eval("EXTN")%>                    
                    </asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

Open in new window


then in my code-behind file in the page load event, I did this:


protected void Page_Load(object sender, EventArgs e)
        {
            string con = ConfigurationManager.ConnectionStrings["TestConnectionStringFromWebConfig"].ConnectionString;

            string sql = "SELECT * FROM [Test]";

            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            da.Fill(ds);

            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();
        }

Open in new window

0
 

Author Closing Comment

by:ASPDEV
ID: 38820590
Excellent post, except with the format.

The result was like this:
212-2222111 ext 113 instead of 212-2222-2111 ext 113

I tried this and worked
 <asp:Label ID="Label1" runat="server" Text="">
                        <%# Eval("AREA") + "-" + (String.Format("{0:000-0000}", Convert.ToInt64(DataBinder.Eval(Container.DataItem,"PHONE")))) + " Ex:" + Eval("EXTN")%>                    
                    </asp:Label>

Thanks,
ASPDEV
0

Featured Post

Docker-Compose to Simplify Multi-Container Builds

Our veteran DevOps Author takes you through how to build a multi-container environment, managed with a single utility in order to simplify your deployments.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question