在浏览器中查看时,Gridview无法看到?

时间:2011-12-07 07:48:17

标签: asp.net gridview

我有一个母版页,然后我创建一个GridViewWithTextBoxes.aspx并选择了母版页 但是当我浏览器时,gridview无法看到 但如果我从不包含母版页,就可以看到gridview 然后它在c#中工作而不是在vb.net中 我认为是导致问题的页面加载部分,但我不知道是什么 我能知道为什么吗?

这是我的代码

 <%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/loggedInMasterPage.Master" CodeBehind="trry.aspx.vb" Inherits="WebRole1.trry" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <asp:GridView ID="GridView1" runat="server" ShowFooter="true" 
            AutoGenerateColumns="false" onrowcreated="Gridview1_RowCreated">
    <Columns>
            <asp:BoundField DataField="RowNumber" HeaderText="Row Number" />
            <asp:TemplateField HeaderText="Header 1">
                <ItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            </Columns> 
    </asp:GridView>

      <asp:gridview ID="Gridview3" runat="server" ShowFooter="true" 
            AutoGenerateColumns="false" onrowcreated="Gridview1_RowCreated">
            <Columns>
            <asp:BoundField DataField="RowNumber" HeaderText="Row Number" />
            <asp:TemplateField HeaderText="Header 1">
                <ItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Header 2">
                <ItemTemplate>
                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Header 3">
                <ItemTemplate>
                     <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                </ItemTemplate>
                <FooterStyle HorizontalAlign="Right" />
                <FooterTemplate>
                 <asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" 
                        onclick="ButtonAdd_Click" />
                </FooterTemplate>
            </asp:TemplateField>
                 <asp:TemplateField HeaderText="Header 3">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text="Click Me"></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            </Columns>
        </asp:gridview>
        <asp:Button ID="Button1" runat="server" Text="Save" onclick="Button1_Click" />

</asp:Content>

这是我的vb代码

   Imports System.Data.SqlClient
Imports System.Data
Imports System.Collections.Specialized

Imports System.Text
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Public Class trry
Inherits System.Web.UI.Page



  Private Sub SetInitialRow()
        Dim dt As New DataTable()
        Dim dr As DataRow = Nothing
        dt.Columns.Add(New DataColumn("RowNumber", GetType(String)))
        dt.Columns.Add(New DataColumn("Column1", GetType(String)))
        dt.Columns.Add(New DataColumn("Column2", GetType(String)))
        dt.Columns.Add(New DataColumn("Column3", GetType(String)))
        dr = dt.NewRow()
        dr("RowNumber") = 1
        dr("Column1") = String.Empty
        dr("Column2") = String.Empty
        dr("Column3") = String.Empty
        dt.Rows.Add(dr)
        'dr = dt.NewRow();

        'Store the DataTable in ViewState
        ViewState("CurrentTable") = dt

        Gridview1.DataSource = dt
        Gridview1.DataBind()
    End Sub

    Private Sub AddNewRowToGrid()
        Dim rowIndex As Integer = 0

        If ViewState("CurrentTable") IsNot Nothing Then
            Dim dtCurrentTable As DataTable = DirectCast(ViewState("CurrentTable"), DataTable)
            Dim drCurrentRow As DataRow = Nothing
            If dtCurrentTable.Rows.Count > 0 Then
                For i As Integer = 1 To dtCurrentTable.Rows.Count
                    'extract the TextBox values
                    Dim box1 As TextBox = DirectCast(Gridview1.Rows(rowIndex).Cells(1).FindControl("TextBox1"), TextBox)
                    Dim box2 As TextBox = DirectCast(Gridview1.Rows(rowIndex).Cells(2).FindControl("TextBox2"), TextBox)
                    Dim box3 As TextBox = DirectCast(Gridview1.Rows(rowIndex).Cells(3).FindControl("TextBox3"), TextBox)

                    drCurrentRow = dtCurrentTable.NewRow()
                    drCurrentRow("RowNumber") = i + 1
                    drCurrentRow("Column1") = box1.Text
                    drCurrentRow("Column2") = box2.Text
                    drCurrentRow("Column3") = box3.Text

                    rowIndex += 1
                Next
                dtCurrentTable.Rows.Add(drCurrentRow)
                ViewState("CurrentTable") = dtCurrentTable

                Gridview1.DataSource = dtCurrentTable
                Gridview1.DataBind()
            End If
        Else
            Response.Write("ViewState is null")
        End If

        'Set Previous Data on Postbacks
        SetPreviousData()
    End Sub

    Private Sub SetPreviousData()
        Dim rowIndex As Integer = 0
        If ViewState("CurrentTable") IsNot Nothing Then
            Dim dt As DataTable = DirectCast(ViewState("CurrentTable"), DataTable)
            If dt.Rows.Count > 0 Then
                For i As Integer = 1 To dt.Rows.Count - 1
                    Dim box1 As TextBox = DirectCast(Gridview1.Rows(rowIndex).Cells(1).FindControl("TextBox1"), TextBox)
                    Dim box2 As TextBox = DirectCast(Gridview1.Rows(rowIndex).Cells(2).FindControl("TextBox2"), TextBox)
                    Dim box3 As TextBox = DirectCast(Gridview1.Rows(rowIndex).Cells(3).FindControl("TextBox3"), TextBox)

                    box1.Text = dt.Rows(i)("Column1").ToString()
                    box2.Text = dt.Rows(i)("Column2").ToString()
                    box3.Text = dt.Rows(i)("Column3").ToString()


                    rowIndex += 1
                Next
                ' ViewState["CurrentTable"] = dt;

            End If
        End If
    End Sub
    Protected Sub Page_Load(sender As Object, e As EventArgs)
        If Not Page.IsPostBack Then
            SetInitialRow()
        End If
    End Sub
    Protected Sub ButtonAdd_Click(sender As Object, e As EventArgs)
        AddNewRowToGrid()
    End Sub
    Protected Sub Button1_Click(sender As Object, e As EventArgs)
        Dim rowIndex As Integer = 0
        Dim sc As New StringCollection()
        If ViewState("CurrentTable") IsNot Nothing Then
            Dim dtCurrentTable As DataTable = DirectCast(ViewState("CurrentTable"), DataTable)
            Dim drCurrentRow As DataRow = Nothing
            If dtCurrentTable.Rows.Count > 0 Then
                For i As Integer = 1 To dtCurrentTable.Rows.Count
                    'extract the TextBox values
                    Dim box1 As TextBox = DirectCast(Gridview1.Rows(rowIndex).Cells(1).FindControl("TextBox1"), TextBox)
                    Dim box2 As TextBox = DirectCast(Gridview1.Rows(rowIndex).Cells(2).FindControl("TextBox2"), TextBox)
                    Dim box3 As TextBox = DirectCast(Gridview1.Rows(rowIndex).Cells(3).FindControl("TextBox3"), TextBox)

                    'get the values here

                    'Response.Write(box1.Text + "<BR/>");
                    'Response.Write(box2.Text + "<BR/>");
                    'Response.Write(box3.Text);

                    sc.Add((box1.Text + "," + box2.Text & ",") + box3.Text)
                    rowIndex += 1
                Next
                InsertRecords(sc)
            End If
        End If
    End Sub
    'A method that returns a string which calls the connection string from the web.config
    Private Function GetConnectionString() As String
        '"DBConnection" is the name of the Connection String
        'that was set up from the web.config file
        Return System.Configuration.ConfigurationManager.ConnectionStrings("WeddingPerfection").ConnectionString

    End Function

    'A method that Inserts the records to the database
    Private Sub InsertRecords(sc As StringCollection)
        Dim conn As New SqlConnection(GetConnectionString())
        Dim sb As New StringBuilder(String.Empty)
        Dim splitItems As String() = Nothing
        For Each item As String In sc

            Const sqlStatement As String = "INSERT INTO SampleTables (Column1,Column2,Column3) VALUES"
            If item.Contains(",") Then
                splitItems = item.Split(",".ToCharArray())
                sb.AppendFormat("{0}('{1}','{2}','{3}'); ", sqlStatement, splitItems(0), splitItems(1), splitItems(2))

            End If
        Next

        Try
            conn.Open()
            Dim cmd As New SqlCommand(sb.ToString(), conn)
            cmd.CommandType = CommandType.Text
            cmd.ExecuteNonQuery()


            Page.ClientScript.RegisterClientScriptBlock(GetType(Page), "Script", "alert('Records Successfuly Saved!');", True)
        Catch ex As System.Data.SqlClient.SqlException
            Dim msg As String = "Insert Error:"
            msg += ex.Message

            Throw New Exception(msg)
        Finally
            conn.Close()
        End Try
    End Sub
    Protected Sub Gridview1_RowCreated(sender As Object, e As GridViewRowEventArgs)

        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim l As Label = DirectCast(e.Row.FindControl("Label1"), Label)
            If l IsNot Nothing Then
                Dim script As String = "window.open('addGroup.aspx');"
                l.Attributes.Add("onclick", script)
            End If
        End If
    End Sub

End Class

2 个答案:

答案 0 :(得分:0)

您将它放入HeadContent区域 - 这是您打算将脚本包含和特定于该页面的内容放在哪里。

所以这代表

<head>

</head>

下面会有第二个内容区域 - 这就是你想要的内容区域。

答案 1 :(得分:0)

检查此内容的ContentPlaceHolderID重定向到HeadContent。内容在<body>标记内显示;不在<head>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server"> 

设置ContentPlaceHolderID="contentplaceHolderInsideBodayTagOfMasterPage"。就这样做。

所有其他代码应该可以正常工作..