Solved

itemTemplate within a Gridview not displaying in run mode

Posted on 2008-06-12
6
650 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 

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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

861 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