Solved

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

Posted on 2013-01-25
6
665 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
  • 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
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 500 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

860 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