2010-02-07 19 views

Respuesta

1

Sí, es posible.

estoy demostrando con la tabla de empleados

utilizar el siguiente script de SQL Server para crear empleado presentadas nombrado tbemployee

CREATE TABLE [dbo].[tbemployee](
[empid] [int] IDENTITY(1,1) NOT NULL, 
[ename] [varchar](50) NULL, 
[eadd] [varchar](50) NULL, 
[esal] [int] NULL, 
[edno] [int] NULL, 

CONSTRAINT [PK_tbemployee] PRIMARY KEY CLUSTERED 
(
    [empid] ASC 
) 
) ON [PRIMARY] 
GO 

El código fuente para la inserción de registro en FormView se muestra a continuación:

FormView.aspx

<asp:FormView ID="FormView1" runat="server" DataKeyNames="empid" 
     oniteminserting="FormView1_ItemInserting" DefaultMode="Insert" 
     onmodechanging="FormView1_ModeChanging">    
     <InsertItemTemplate> 
      ename: 
      <asp:TextBox ID="txtename" runat="server" Text='<%# Bind("ename") %>' /> 
      <br /> 
      eadd: 
      <asp:TextBox ID="txteadd" runat="server" Text='<%# Bind("eadd") %>' /> 
      <br /> 
      esal: 
      <asp:TextBox ID="txtesal" runat="server" Text='<%# Bind("esal") %>' /> 
      <br /> 
      edno: 
      <asp:TextBox ID="txtedno" runat="server" Text='<%# Bind("edno") %>' /> 
      <br /> 
      <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" 
       CommandName="Insert" Text="Insert" /> 
      &nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server" 
       CausesValidation="False" CommandName="Cancel" Text="Cancel" /> 
     </InsertItemTemplate> 
     <ItemTemplate> 
      empid: 
      <asp:Label ID="empidLabel" runat="server" Text='<%# Eval("empid") %>' /> 
      <br /> 
      ename: 
      <asp:Label ID="enameLabel" runat="server" Text='<%# Bind("ename") %>' /> 
      <br /> 
      eadd: 
      <asp:Label ID="eaddLabel" runat="server" Text='<%# Bind("eadd") %>' /> 
      <br /> 
      esal: 
      <asp:Label ID="esalLabel" runat="server" Text='<%# Bind("esal") %>' /> 
      <br /> 
      edno: 
      <asp:Label ID="ednoLabel" runat="server" Text='<%# Bind("edno") %>' /> 
      <br /> 
      <asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" 
       CommandName="New" Text="New" /> 
     </ItemTemplate> 
    </asp:FormView> 
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
     ConnectionString="Data Source=gts7;Initial Catalog=dbemp14;Integrated Security=True;Pooling=False"    
     ProviderName="System.Data.SqlClient">   
    </asp:SqlDataSource> 

y en el código subyacente de la página FormView.aspx pegue el código siguiente:

FormView.aspx.cs

using System; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data; 
using System.Data.SqlClient; 
using System.Configuration; 

public partial class _Default : System.Web.UI.Page 
{ 
    SqlConnection con=new SqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString); 

protected void Page_Load(object sender, EventArgs e) 
{ 

} 

protected void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e) 
    { 
     string ename, eadd,insertqry; 
     Int32 esal, edno; 
     ename = ((TextBox)(FormView1.FindControl("txtename"))).Text; 
     eadd = ((TextBox)(FormView1.FindControl("txtename"))).Text; 
     esal = Convert.ToInt32(((TextBox)(FormView1.FindControl("txtesal"))).Text); 
     edno = Convert.ToInt32(((TextBox)(FormView1.FindControl("txtedno"))).Text); 

    insertqry="insert tbemployee(ename,eadd,esal,edno) values(@ename,@eadd,@esal,@edno)"; 

    if (con.State == ConnectionState.Closed) 
    { 
     con.Open(); 
    } 

    SqlCommand cmd = new SqlCommand(insertqry, con); 
    cmd.Parameters.Add("@ename", SqlDbType.VarChar, 50).Value = ename; 
    cmd.Parameters.Add("@eadd", SqlDbType.VarChar, 50).Value = eadd; 
    cmd.Parameters.Add("@esal", SqlDbType.Int).Value = esal; 
    cmd.Parameters.Add("@edno", SqlDbType.Int).Value = edno; 

    cmd.ExecuteNonQuery(); 
    cmd.Dispose(); 
    con.Close(); 

    FormView1.ChangeMode(FormViewMode.ReadOnly); 
    formbind(); 
} 

public void formbind() 
{ 
    if (FormView1.AllowPaging == true) 
    { 
     SqlDataAdapter adp = new SqlDataAdapter("select * from tbemployee", con); 
     DataSet ds = new DataSet(); 
     adp.Fill(ds); 

     Int32 totrecords = ds.Tables[0].Rows.Count; 
     Int32 currentpageindex = totrecords - 1; 
     FormView1.PageIndex = currentpageindex; 
     FormView1.DataSource = ds; 
     FormView1.DataBind(); 
    } 
    else 
    { 
     SqlDataAdapter adp = new SqlDataAdapter("select * from tbemployee where empid in (select isnull(max(empid),0) from tbemployee)", con); 
     DataSet ds = new DataSet(); 
     adp.Fill(ds); 
     FormView1.DataSource = ds; 
     FormView1.DataBind(); 
    } 
} 

protected void FormView1_ModeChanging(object sender, FormViewModeEventArgs e) 
{ 
    FormView1.ChangeMode(e.NewMode); 
    formbind(); 
} 
1

Hay otra solución que es mucho más simple en mi humilde opinión.

Usando los datos anteriores (@ SK-INFOPOINT), escribiría mi FormView1.aspx de forma muy parecida con solo un par de cambios menores en el modo "Solo lectura" y colocando los comandos Insertar y Seleccionar SQL en SQLAdapter a tener este aspecto:

FormView1.aspx

<asp:FormView ID="FormView1" runat="server" DataKeyNames="empid" 
    oniteminserting="FormView1_ItemInserting" DefaultMode="ReadOnly" 
    onmodechanging="FormView1_ModeChanging">    
    <InsertItemTemplate> 
     ename: 
     <asp:TextBox ID="txtename" runat="server" Text='<%# Bind("ename") %>' /> 
     <br /> 
     eadd: 
     <asp:TextBox ID="txteadd" runat="server" Text='<%# Bind("eadd") %>' /> 
     <br /> 
     esal: 
     <asp:TextBox ID="txtesal" runat="server" Text='<%# Bind("esal") %>' /> 
     <br /> 
     edno: 
     <asp:TextBox ID="txtedno" runat="server" Text='<%# Bind("edno") %>' /> 
     <br /> 
     <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" 
      CommandName="Insert" Text="Insert" /> 
     &nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server" 
      CausesValidation="False" CommandName="Cancel" Text="Cancel" /> 
    </InsertItemTemplate> 
    <ItemTemplate> 
     empid: 
     <asp:Label ID="empidLabel" runat="server" Text='<%# Eval("empid") %>' /> 
     <br /> 
     ename: 
     <asp:Label ID="enameLabel" runat="server" Text='<%# Bind("ename") %>' /> 
     <br /> 
     eadd: 
     <asp:Label ID="eaddLabel" runat="server" Text='<%# Bind("eadd") %>' /> 
     <br /> 
     esal: 
     <asp:Label ID="esalLabel" runat="server" Text='<%# Bind("esal") %>' /> 
     <br /> 
     edno: 
     <asp:Label ID="ednoLabel" runat="server" Text='<%# Bind("edno") %>' /> 
     <br /> 
     <asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" 
      CommandName="New" Text="New" /> 
    </ItemTemplate> 
</asp:FormView> 
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="Data Source=gts7;Initial Catalog=dbemp14;Integrated Security=True;Pooling=False"    
    ProviderName="System.Data.SqlClient" 
    SelectCommand="select * from tbemployee" 
    InsertCommand="insert into tbemployee (ename,eadd,esal,edno) values (@ename, @eadd, @esal, @edno)">   
</asp:SqlDataSource> 

esto permite que los botones y plantillas para hacer la mayor parte de la funcionalidad sin necesidad de codificar manualmente la inserción y seleccionar. A continuación, me gustaría añadir sólo esto en el código detrás de la página FormView.aspx el siguiente código:

FormView.aspx.cs

using System; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data; 

public partial class _Default : System.Web.UI.Page 
{ 

    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 

    protected void FormView1_ItemInserted(object sender, FormViewInsertedEventArgs e) 
    { 
     DataView dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty); 
     FormView1.PageIndex = dv.Count - 1; 
    } 
} 

y ya está mostrando su nuevo artículo enlazado en modo de sólo lectura. Y, si se agrega una plantilla de paginación simple a su FormView, puede desplazarse arriba y abajo registros sin embargo, por favor ...

:)

- Schnizzles

Cuestiones relacionadas