none
¿Por que no me carga el GridView? RRS feed

  • Pregunta

  • Hola, les anexo el codigo para que lo visualicen

    Index.asp

    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Index.aspx.vb" Inherits="M16.Index" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        
            &nbsp;<input id="txtnombre" type="text" runat ="server" /></div>
            <p>
                &nbsp;</p>
            <p>
        
            <asp:Button id="btnGuardar" text="Guardar" runat="server" Visible ="true"/>
    
            </p>
          <asp:GridView ID="grid1" runat="server" AutoGenerateColumns="false" Visible ="TRUE">
                    <Columns>
                        <asp:TemplateField HeaderText="ID" HeaderStyle-Width="100px" >
                            <ItemTemplate >
                                <asp:Label ID="Label1" runat="server" Text='<%# Eval("ID")%>'></asp:Label>
                                
                            </ItemTemplate>
                        </asp:TemplateField>
    
    
                        <asp:TemplateField HeaderText="Nombre" HeaderStyle-Width="100px" >
                            <ItemTemplate >
                                <asp:Label ID="Label2" runat="server" Text='<%# Eval("Nombre")%>'></asp:Label>
                                
                            </ItemTemplate>
                        </asp:TemplateField>
                       
    
                        <asp:TemplateField HeaderText="Editar" HeaderStyle-Width="80px" >
                            <ItemTemplate >
                                <asp:Button ID="btnMod" runat="server" Text="Editar" CommandName="Editar" CommandArgument='<%# Eval("ID")%>' />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Eliminar" HeaderStyle-Width="80px" >
                            <ItemTemplate >
                                <asp:Button ID="btnDel" runat="server" Text="Eliminar" CommandName="Eliminar" CommandArgument='<%# Eval("ID")%>' />
                                
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
        </form>
    </body>
    </html>
    

    index.asp.vb

    Public Class Index
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
            Dim WBS As WebService = New WebService
            grid1.DataSource = WBS.Grid()
        End Sub
    
        Protected Sub btnGuardar_Click(sender As Object, e As EventArgs) Handles btnGuardar.Click
            Dim WBS As WebService = New WebService()
    
            WBS.insertar(txtnombre.Value.ToString)
        End Sub
    End Class

    WebService.asmx

    Imports System.Web.Services
    Imports System.Web.Services.Protocols
    Imports System.ComponentModel
    Imports System.Data.SqlClient
    Imports System.Configuration.ConfigurationManager
    
    
    
    ' Para permitir que se llame a este servicio web desde un script, usando ASP.NET AJAX, quite la marca de comentario de la línea siguiente.
    ' <System.Web.Script.Services.ScriptService()> _
    <System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
    <System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
    <ToolboxItem(False)> _
    Public Class WebService
        Inherits System.Web.Services.WebService
    
        Public con As SqlConnection
    
        Private cadena As String = ConfigurationManager.ConnectionStrings("credencialsql").ToString
    
        Sub New()
            con = New SqlConnection(cadena)
        End Sub
    
        Private Sub 
    
        End Sub
    
    
        <WebMethod> _
        Public Function insertar(ByVal strNombre As String) As Boolean
    
            Try
                Dim Query As String = "Insert into prueba (nombre) values ('" + strNombre + "')"
                Using cmd As SqlCommand = New SqlCommand(Query)
                    cmd.Connection = con
    
                    Using sqldata As SqlDataAdapter = New SqlDataAdapter()
                        sqldata.SelectCommand = cmd
    
                        Using dt As DataTable = New DataTable()
                            sqldata.Fill(dt)
    
                        End Using
                    End Using
                End Using
            Catch ex As Exception
    
            End Try
            Return True
        End Function
    
    
        <WebMethod> _
        Public Function Grid() As DataTable
    
            Try
    
    
                Using cmd As SqlCommand = New SqlCommand("select id,nombre from prueba;")
                    cmd.Connection = con
    
                    Using da As SqlDataAdapter = New SqlDataAdapter()
                        da.SelectCommand = cmd
    
                        Using dt As DataTable = New DataTable()
                            da.Fill(dt)
    
                            Return dt
                        End Using
                    End Using
    
                End Using
            Catch ex As Exception
                Debug.WriteLine(ex.Message)
            End Try
    
            Return New DataTable()
        End Function
    
    
    End Class

    viernes, 24 de marzo de 2017 19:29

Respuestas

  • Era esto

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
                If (Not IsPostBack) Then
                Grid()
                End If
        End Sub
    
        Protected Sub Grid()
            Dim servicio As WebService = New WebService()
            grid1.DataSource = servicio.Grid()
            grid1.DataBind()
        End Sub

    viernes, 24 de marzo de 2017 19:55

Todas las respuestas