在.cs文件中编辑表单视图

本文关键字:表单 视图 编辑 cs 文件 | 更新日期: 2023-09-27 17:49:42

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

如果你注意,没有字段"property_id"answers"city_id"在formview。(我删除了它们)我的问题是:在插入之前,我如何编辑这两列的值("property_id"answers"city_id")?因为例如;"city_id"需要与"querystring"对应。我希望我把问题解释清楚了。

在.cs文件中编辑表单视图

您可以通过修改oniteminserts事件的值来实现这一点。

阅读你的aspx标记,你已经声明了事件" formview1_iteminsert "所以在你的cs文件中,你必须有formview1_iteminsert事件处理程序。

来自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");