Solved

itemTemplate within a Gridview not displaying in run mode

Posted on 2008-06-12
6
648 Views
Last Modified: 2013-11-26
I could not figure out why the itemtemplate for chkbox ID = "Lunch"  column is not displaying.The chkbox Id = "Select"  column is displaying.
thanks >
Here's the source

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="592px" style="left: 7px; position: relative; top: 5px">
        <Columns>
        <asp:TemplateField HeaderText="Select">
            <ItemTemplate>
            <asp:CheckBox ID ="chkSelect" runat="server" />
            </ItemTemplate>
       </asp:TemplateField>
            <asp:CommandField ButtonType="Button" CausesValidation="False" ShowSelectButton="True" />
            <asp:TemplateField HeaderText="Lunch">
            <ItemTemplate>
            <asp:CheckBox ID ="chkLunch" runat="server" Enabled="true" />
            </ItemTemplate>
         </asp:TemplateField>
                <asp:BoundField DataField="MASTERID" HeaderText="ID" />
                <asp:BoundField DataField="FULLNAME" HeaderText="FullName" />
                <asp:BoundField DataField="CATEGORY" HeaderText="Category" />
      </Columns>
        <AlternatingRowStyle BackColor="Azure" />
    </asp:GridView></form>
0
Comment
Question by:zachvaldez
  • 3
  • 3
6 Comments
 
LVL 22

Accepted Solution

by:
prairiedog earned 400 total points
ID: 21772795
Well, I don't see anything wrong in your code. Besides, I made a page with your code above, and everything is displaying fine. I added a SqlDataScource connecting to Northwind database so that I can display those BoundField. Please see the attached screen shot and attached code snippet.

There must be something else that hides the check box.
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="GridViewDemo.aspx.vb" Inherits="WebApplication1.GridViewDemo" %>
 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>Untitled Page</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="592px" style="left: 7px; position: relative; top: 5px" DataSourceID="SqlDataSource1">

        <Columns>

        <asp:TemplateField HeaderText="Select">

            <ItemTemplate>

            <asp:CheckBox ID ="chkSelect" runat="server" />

            </ItemTemplate>

       </asp:TemplateField>

            <asp:CommandField ButtonType="Button" CausesValidation="False" ShowSelectButton="True" />

            <asp:TemplateField HeaderText="Lunch">

            <ItemTemplate>

            <asp:CheckBox ID ="chkLunch" runat="server" Enabled="true" />

            </ItemTemplate>

         </asp:TemplateField>

                <asp:BoundField DataField="EmployeeID" HeaderText="ID" />

                <asp:BoundField DataField="FirstName" HeaderText="FullName" />

                <asp:BoundField DataField="LastName" HeaderText="Category" />

      </Columns>

        <AlternatingRowStyle BackColor="Azure" />

    </asp:GridView>

        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=myServer;Initial Catalog=Northwind;Integrated Security=True"

            ProviderName="System.Data.SqlClient" SelectCommand="SELECT EmployeeID,  FirstName, LastName FROM Employees">

        </asp:SqlDataSource>

    </div>

    </form>

</body>

</html>

Open in new window

2008-06-12-141627.jpg
0
 

Author Comment

by:zachvaldez
ID: 21773458
I use this methid to pupolate it. It may have something to do with it?
        If dr.HasRows Then
            Dim bf As New BoundField
            bf.HeaderText = "ID"
            bf.HeaderStyle.BackColor = Drawing.Color.PowderBlue
            bf.DataField = "MASTERID"
            bf.ItemStyle.Width = "80"
            GridView1.Columns.Add(bf)
            Dim bf2 As New BoundField
            bf2.HeaderText = "Name"
            bf2.HeaderStyle.BackColor = Drawing.Color.PowderBlue
            bf2.DataField = "FULLNAME"
            bf2.ItemStyle.Width = "200"
            GridView1.Columns.Add(bf2)
            Dim bf3 As New BoundField
            bf3.HeaderText = "Category"
            bf3.HeaderStyle.BackColor = Drawing.Color.PowderBlue
            bf3.DataField = "CATEGORY"
            bf3.ItemStyle.Width = "300"
            GridView1.Columns.Add(bf3)
            GridView1.DataSource = dr
            GridView1.DataBind()
        End If
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 21773910
Just a quick guess (haven't tested it yet). What if you do:
If dr.HasRows Then
            GridView1.DataSource = dr
            GridView1.DataBind()
 End If

I am heading off home and will check back tomorrow if you still have trouble.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:zachvaldez
ID: 21774916
First, because of the datasource, there is conflict with this...
   <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=myServer;Initial Catalog=Northwind;Integrated Security=True"
            ProviderName="System.Data.SqlClient" SelectCommand="SELECT EmployeeID,  FirstName, LastName FROM Employees">
        </asp:SqlDataSource>

MY main concern is why can't i display the lunch itemtemplate in the grid...
thanks for your help.
0
 
LVL 22

Assisted Solution

by:prairiedog
prairiedog earned 400 total points
ID: 21779006
It does not conflict. The only difference between my example and yours is the way to retrieve data for the GridView. I changed my code to use SqlDataReader and got the same result.
Also, in your example, I don't understand why you have to manually add those fields again if you have those fields already defined in your GridView. Your code will append duplicated columns to the GridView. See the attached screen shot.
Since I still can't reproduce the problem you mentioned in your question, I am thinking the problem resides in a different location. Can you post a screen shot of your results? Do you do anything else after you bind the GridView that might cause the Lunch field be invisible?
Imports System.Configuration

Imports System.Data.SqlClient
 

Partial Public Class GridViewDemo

    Inherits System.Web.UI.Page
 

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim strConn As String

        strConn = ConfigurationManager.ConnectionStrings("NorthwindConnectionString").ToString()
 

        Dim conn As SqlConnection = New SqlConnection(strConn)

        Dim cmd As SqlCommand = New SqlCommand()

        cmd.Connection = conn

        cmd.CommandText = "SELECT EmployeeID,  FirstName, LastName FROM Employees"

        cmd.CommandType = CommandType.Text

        Dim dr As SqlDataReader

        Try

            conn.Open()

            dr = cmd.ExecuteReader()

            If dr.HasRows Then

                Dim bf As New BoundField

                bf.HeaderText = "ID"

                bf.HeaderStyle.BackColor = Drawing.Color.PowderBlue

                bf.DataField = "EmployeeID"

                bf.ItemStyle.Width = "80"

                GridView1.Columns.Add(bf)

                Dim bf2 As New BoundField

                bf2.HeaderText = "First Name"

                bf2.HeaderStyle.BackColor = Drawing.Color.PowderBlue

                bf2.DataField = "FirstName"

                bf2.ItemStyle.Width = "200"

                GridView1.Columns.Add(bf2)

                Dim bf3 As New BoundField

                bf3.HeaderText = "Last Name"

                bf3.HeaderStyle.BackColor = Drawing.Color.PowderBlue

                bf3.DataField = "LastName"

                bf3.ItemStyle.Width = "300"

                GridView1.Columns.Add(bf3)

                GridView1.DataSource = dr

                GridView1.DataBind()

            End If

        Catch ex As Exception
 

        Finally

            conn.Close()

        End Try

    End Sub

End Class

Open in new window

2008-06-13-090756.jpg
0
 

Author Comment

by:zachvaldez
ID: 21783730
I don't understand why you have to manually add those fields again if you have those fields already defined in your GridView. Your code will append duplicated columns to the GridView. See the attached screen shot.>>>> YOU"RE ABSOLUTELY CORRECT!!
Also, there was this code that removes the columns and repopulate it. Between those two, I am losing the column for the "lunch"
I appreciate your help. Now I'll try to figure out how to insert the rows selected to  the " Event" table.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
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…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

929 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now