Solved

itemTemplate within a Gridview not displaying in run mode

Posted on 2008-06-12
6
647 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:zachvaldez
Comment Utility
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
Comment Utility
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
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

743 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

18 Experts available now in Live!

Get 1:1 Help Now