6

是否可以让 FormView 在刚刚通过其 InsertItemTemplate 插入的记录上自动默认为 ReadOnly 模式?似乎这对 FormView 来说应该是自然而然的事情。

4

2 回答 2

1

恕我直言,还有另一种更简单的解决方案。

使用上面的(@SK-INFOPOINT)数据,我将编写我的 FormView1.aspx 非常相同,只有几个小的更改默认为“只读”模式,并将 SQL 插入和选择命令放入 SQLAdapter 看起来像这个:

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>

这允许按钮和模板完成大部分功能,而无需手动编码插入和选择。然后我将只在 FormView.aspx 页面的代码后面添加以下代码:

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;
    }
}

您正在以只读模式显示绑定的新项目。而且,如果您将一个简单的分页模板添加到您的 FormView,您可以随意上下导航记录...

:)

—— 炸肉排

于 2011-12-15T21:14:31.043 回答
1

对的,这是可能的。

我正在用 Employee 表演示它

使用以下 Sql Server 脚本创建名为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

将记录插入 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>

并在 FormView.aspx 页面的代码后面粘贴以下代码:

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();
}
于 2011-07-16T11:20:44.877 回答