Ver Mensaje Individual
  #1 (permalink)  
Antiguo 17/05/2004, 09:16
Avatar de RootK
RootK
Moderador
 
Fecha de Ingreso: febrero-2002
Ubicación: México D.F
Mensajes: 8.004
Antigüedad: 22 años, 2 meses
Puntos: 50
Ejemplo para moverse entre registro con asp.net

Hola, en el siguiente ejemplo (solo basta con copiar y pegar) podrán moverse entre registro con un dataset, y voy a poner el ejemplo usando un datalist para que parezca paginacion, ya dependerá de ustedes como adaptar el código para lo que necesitan.

Utilicé un 2 labels para guardar el Tamaño de la página (intPageSize) y el Indice actual de mi registro(intCurrIndex), los dos con la propiedad visible=false

Un datalist donde usé 2 campos (que pueden ser los que ustedes quieran, solo sería cosa de irlo adaptando)

(Esta en VB.Net pero para c# se darán cuenta que no es dificl adaptarlo )

Modo HTML

Cita:
<asp:datalist id="dList" runat="server" Width="100%">
<HeaderTemplate>
<table width="100%" style="font: 10pt verdana" cellpadding="0" cellspacing="0">
<tr style="background-color:blue">
<th align="left">
<font color="#FFFFFF">Campo 1</font></th>
<th align="left">
<font color="#FFFFFF">Campo 2</font></th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr style="background-color:gainsboro">
<td width="50%"><%# DataBinder.Eval(Container.DataItem, "NombreCampo1 ") %></td>
<td><%# DataBinder.Eval(Container.DataItem, "NombreCampo2 ") %></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:datalist></P>
<P><a name="mensaje"></a>
<table width="100%" align="right">
<tr>
<td align="left" width="76%"><asp:label id="lblStatus " Font-Size="10pt" Font-Name="verdana" Runat="server"></asp:label></td>
<td width="6%"><A id="hrefFirst" href="datalistpaging.aspx#this" runat="server" onserverclick="ShowFirst "><b>&lt;&lt;</b></A>
</td>
<td width="6%"><A id="hrefPrevious" href="#" runat="server" onserverclick="ShowPrevious "><b>&lt;</b></A>
</td>
<td width="6%"><A id="hrefNext" href="#" runat="server" onserverclick="ShowNext "><b>&gt;</b></A>
</td>
<td width="6%"><A id="hrefLast" href="#" runat="server" onserverclick="ShowLast "><b>&gt;&gt;</b></A>
</td>
</tr>
</table>
</P>
<P><asp:label id="intCurrIndex" Runat="server" Visible="False"></asp:label>
<asp:label id="intPageSize" Runat="server" Visible="False"></asp:label><asp:label id="intRecordCount" Runat="server" Visible="False"></asp:label></P>

Codebing de la página:

Cita:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

If Not Page.IsPostBack() Then

intPageSize.Text = "10" 'aqui indican el tamañano de la página
intCurrIndex.Text = "0" 'Inicializo el indice
Bind()

End If


End Sub


Private Sub Bind()
Dim objConn As New SqlConnection(ConfigurationSettings.AppSettings("c onnectionString"))
Dim objDA As New SqlDataAdapter("SELECT * FROM TABLA", objConn)
Dim objDS As New DataSet

If Not Page.IsPostBack() Then
objDA.Fill(objDS)
intRecordCount.Text = CStr(objDS.Tables(0).Rows.Count)
objDS = Nothing
objDS = New DataSet
End If

If intCurrIndex.Text = 0 Then
hrefPrevious.Visible = False
Else
hrefPrevious.Visible = True
End If

objDA.Fill(objDS, CInt(intCurrIndex.Text), CInt(intPageSize.Text), "Logs")


dList.DataSource = objDS.Tables(0).DefaultView
dList.DataBind()
objConn.Close()
PrintStatus()
End Sub

Public Sub ShowFirst (ByVal s As Object, ByVal e As EventArgs)
intCurrIndex.Text = "0"
Bind()
End Sub

Public Sub ShowPrevious (ByVal s As Object, ByVal e As EventArgs)
intCurrIndex.Text = CStr(CInt(intCurrIndex.Text) - CInt(intPageSize.Text))
If CInt(intCurrIndex.Text) < 0 Then
intCurrIndex.Text = "0"
End If
Bind()
End Sub

Public Sub ShowNext (ByVal s As Object, ByVal e As EventArgs)
If CInt(intCurrIndex.Text) + 1 < CInt(intRecordCount.Text) Then
intCurrIndex.Text = CStr(CInt(intCurrIndex.Text) + CInt(intPageSize.Text))
End If
Bind()
End Sub

Public Sub ShowLast (ByVal s As Object, ByVal e As EventArgs)
Dim tmpInt As Integer

tmpInt = CInt(intRecordCount.Text) Mod CInt(intPageSize.Text)
If tmpInt > 0 Then
intCurrIndex.Text = CStr(CInt(intRecordCount.Text) - tmpInt)
Else
intCurrIndex.Text = CStr(CInt(intRecordCount.Text) - CInt(intPageSize.Text))
End If
Bind()
End Sub

Private Sub PrintStatus()
lblStatus .Text = "Total Records:<b>" & intRecordCount.Text
lblStatus.Text += "</b> - Showing Page:<b> "
lblStatus.Text += CStr(CInt(CInt(intCurrIndex.Text) / CInt(intPageSize.Text) + 1))
lblStatus.Text += "</b> of <b>"

If (CInt(intRecordCount.Text) Mod CInt(intPageSize.Text)) > 0 Then
lblStatus.Text += CStr(CInt(CInt(intRecordCount.Text) / CInt(intPageSize.Text) + 1))
Else
lblStatus.Text += CStr(CInt(intRecordCount.Text) / CInt(intPageSize.Text))
End If
lblStatus.Text += "</b>"
End Sub
Espero que les funcione y cualquier cosa avisenme.

Saludos
__________________
Nadie roba nada ya que en la vida todo se paga . . .

Exentrit - Soluciones SharePoint & Net