0

私の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」のフィールドはありません。(削除しました)私の質問は、挿入する前にこれら2つの列の値( "property_id"と"city_id")を編集するにはどうすればよいですか?たとえば、「city_id」は「querystring」に応じて特定の値である必要があります。問題を明確に説明できれば幸いです。

4

1 に答える 1

0

これは、OnItemInserting イベントの値を変更することで実現できます。

イベント「FormView1_ItemInserting」を宣言した aspx マークアップを読み取ると、cs ファイルに FormView1_ItemInserting イベント ハンドラが必要になります。

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

「 Valuesプロパティを使用して、新しいレコードのフィールド値を読み取ったり変更したりすることもできます」

そして、ここにも例があります:

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");
于 2012-12-27T11:36:16.580 に答える