.view文件中的Formview编辑项目

时间:2012-12-27 11:16:58

标签: asp.net formview c#

我的formview数据源是一个名为“properties”的数据库表。

CREATE TABLE [dbo].[properties] (
[property_id]            INT        IDENTITY (1, 1) NOT NULL,
[property_type_code]     INT        NOT NULL,
[city_id]                INT        NULL,
[date_on_market]         DATETIME   NULL,
[property_name]          CHAR (25)  NULL,
[property_owner]         CHAR (25)  NULL,
[property_description]   CHAR (100) NULL,
[property_address]       CHAR (50)  NULL,
[vendor_requested_price] INT        NULL,
[other_property_details] CHAR (50)  NULL,
CONSTRAINT [Key7] PRIMARY KEY CLUSTERED ([property_id] ASC),
CONSTRAINT [Relationship15] FOREIGN KEY ([property_type_code]) REFERENCES [dbo].[ref_properties_type] ([property_type_code]),
CONSTRAINT [Relationship56] FOREIGN KEY ([city_id]) REFERENCES [dbo].[cities] ([city_id]) ON DELETE CASCADE);

我的Formview就是这样:

`<asp:FormView ID="FormView1" runat="server" DataKeyNames="property_id" DataSourceID="SqlDataSource2" oniteminserted="FormView1_ItemInserted" oniteminserting="FormView1_ItemInserting">

            <InsertItemTemplate>

                property_type_code:
                <asp:TextBox ID="property_type_codeTextBox" runat="server" Text='<%# Bind("property_type_code") %>' />
                <br />
                date_on_market:
                <asp:TextBox ID="date_on_marketTextBox" runat="server" Text='<%# Bind("date_on_market") %>' />
                <br />
                property_name:
                <asp:TextBox ID="property_nameTextBox" runat="server" Text='<%# Bind("property_name") %>' />
                <br />
                property_owner:
                <asp:TextBox ID="property_ownerTextBox" runat="server" Text='<%# Bind("property_owner") %>' />
                <br />
                property_description:
                <asp:TextBox ID="property_descriptionTextBox" runat="server" Text='<%# Bind("property_description") %>' />
                <br />
                property_address:
                <asp:TextBox ID="property_addressTextBox" runat="server" Text='<%# Bind("property_address") %>' />
                <br />
                vendor_requested_price:
                <asp:TextBox ID="vendor_requested_priceTextBox" runat="server" Text='<%# Bind("vendor_requested_price") %>' />
                <br />
                other_property_details:
                <asp:TextBox ID="other_property_detailsTextBox" runat="server" Text='<%# Bind("other_property_details") %>' />
                <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>
            &nbsp;
            &nbsp;<asp:Button ID="NewButton" runat="server" CausesValidation="False" 
                CommandName="New" Text="Add New" />
        </ItemTemplate>
        </asp:FormView>`

如果你注意,formview中没有“property_id”和“city_id”的字段。 (我删除了它们)我的问题是:如何在插入之前编辑这两列的值(“property_id”和“city_id”)?因为例如; “city_id”需要相应的特定值“querystring”。我希望我能清楚地解释我的问题。

1 个答案:

答案 0 :(得分:0)

您可以通过修改OnItemInserting事件的值来实现此目的。

读取你的aspx标记你已经声明了事件“FormView1_ItemInserting” 所以在你的cs文件中你必须有FormView1_ItemInserting事件处理程序。

来自MSDN: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.formview.iteminserting.aspx

  

“您还可以通过以下方式读取或修改新记录的字段值   使用属性“

此处还有一个例子:

void EmployeeFormView_ItemInserting(Object sender, FormViewInsertEventArgs e)
  {

    MessageLabel.Text = "";

    // Iterate through the items in the Values collection
    // and verify that the user entered a value for each 
    // text box displayed in the insert item template. Cancel
    // the insert operation if the user left a text box empty.
    foreach (DictionaryEntry entry in e.Values)
    {
      if (entry.Value.Equals(""))
      {
        // Use the Cancel property to cancel the 
        // insert operation.
        e.Cancel = true;

        MessageLabel.Text += "Please enter a value for the " +
          entry.Key.ToString() + " field.<br/>";

      }
    }
  }

但是对于你的问题,你要添加一个特定的密钥:

 e.Values.Add("city_id", "yourcityidvalue");