Gridview template field datasource

Cindy Brozyno picture Cindy Brozyno · Mar 18, 2013 · Viewed 32.8k times · Source

I've placed a gridview on my asp.net page, and am using a sql call to obtain the datasource. In the sql call, it brings the data for all the fields(name, address, phone, type) On the gridview, there are 2 fields (phone, type) that need to be template fields rather than bound field, during edit, and textboxes or labels (whichever) in the Item template.

When I run the code, the datagrid completes and fills in - minus the information for the phone or type fields (template fields). How do I databind the fields so the information will show up properly. I've included my code.

Any help would be greatly appreciated. Thanks in advance!

GRIDVIEW:

  <asp:GridView ID="gvMTM" runat="server" AllowPaging="True" DataKeyNames="MTMID"
    AutoGenerateColumns="False" AutoGenerateEditButton="True"
    OnRowEditing="gvMTM_RowEditing" 
    OnRowUpdating="gvMTM_UpdateRowEditing" 
    OnRowCancelingEdit="gvMTM_CancelRowEditing" >
    <Columns>
        <asp:BoundField DataField="MTMID" HeaderText="ID" />
        <asp:BoundField DataField="MTMName" HeaderText="Name" />
        <asp:BoundField DataField="Add1" HeaderText="Add1" />
        <asp:BoundField DataField="Add2" HeaderText="Add2" />
        <asp:BoundField DataField="City" HeaderText="City" />
        <asp:BoundField DataField="ST" HeaderText="ST" />
        <asp:BoundField DataField="Zip" HeaderText="Zip" />
        <asp:BoundField DataField="Email" HeaderText="Email" />
        <asp:TemplateField HeaderText="Phone#">
            <EditItemTemplate>
                <asp:TextBox ID="txtPhoneEdit" runat="server"></asp:TextBox>
                <asp:MaskedEditExtender ID="TemptxtPhone_MaskedEditExtender" runat="server" 
                    CultureAMPMPlaceholder="" CultureCurrencySymbolPlaceholder="" 
                    CultureDateFormat="" CultureDatePlaceholder="" CultureDecimalPlaceholder="" 
                    CultureThousandsPlaceholder="" CultureTimePlaceholder="" Enabled="True" 
                    TargetControlID="TemptxtPhone">
                </asp:MaskedEditExtender>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:TextBox ID="txtPhoneRead" runat="server" BorderStyle="None"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="RR">
            <EditItemTemplate>
                <asp:DropDownList ID="ddRREdit" runat="server" Height="16px" Width="100px" DataTextField="Railroad" DataValueField="RRID">
                </asp:DropDownList>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:TextBox ID="txtRRRead" runat="server" BorderStyle="None"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

on .CS page:

     gvMTM.DataSource = code.GetMTMList();
            gvMTM.DataBind();

SQL call

    public DataTable GetMTMList()
    {
        try
        {
            SQLCON = new SqlConnection(connectionString);
            SQLCON.Open();
            SQLCmd = new SqlCommand("spGetMTMList", SQLCON);
            SQLCmd.CommandType = CommandType.StoredProcedure;

            SqlDataAdapter adapter = new SqlDataAdapter(SQLCmd);
            DataTable Detailtable = new DataTable();
            adapter.Fill(Detailtable);

            return Detailtable;

        }
        catch (Exception ex)
        {
            HttpContext.Current.Response.Redirect("~/ErrorRedirect.aspx?" +    ex.Message, false);
            return null;
        }
        finally
        {
            SQLCON.Close();
        }
    }

Answer

Silagy picture Silagy · Mar 18, 2013

Use this:

Text='<%# Eval("phone") %>'

You need to add this:

 <ItemTemplate>
      <asp:TextBox ID="txtPhoneRead" runat="server" Text='<%# Eval("phone") %>'>
      </asp:TextBox>
 </ItemTemplate>

The Eval() method bind the data inside your data source to the Text property

You can take a look at his post for more information

Note Consider changing the TextBox control within the <ItemTemplate> to asp:Label

it should look like this:

<ItemTemplate>
       <asp:Label ID="txtPhoneRead" runat="server" Text='<%# Eval("Phone") %>'>
       </asp:Label>              
 </ItemTemplate>