0

My formview datasource is a database table named "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);

My Formview is like that :

`<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>`

If you pay attention there is no fields for "property_id" and "city_id" in formview. (I deleted them) My question is that: How can I edit these two columns' values ("property_id" and "city_id") before inserting? Because for example; "city_id" need to be a specific value accordingly "querystring". I hope I explained my problem clearly.

4

1 回答 1

0

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

阅读您的 aspx 标记,您已经声明了事件“FormView1_ItemInserting”,因此在您的 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 回答