使用 FormView 插入

发布于 2024-10-27 13:20:58 字数 1091 浏览 5 评论 0原文

我正在尝试使用基本 FormView 控件插入项目。

我对 C# 及其控件有点陌生,所以请耐心等待。 FormView 具有标准条目,例如:

ItemName
ItemPrice
ItemSize

它还具有对用户隐藏的控件,例如:

ItemDateCreated
ItemDatechanged
ItemChangedBy

这些项目我试图在 Insert() 发生之前修改它们的值,因此我捕获了事件 InsertButton_Click()

protected void InsertButton_Click(object sender, EventArgs e)
{
    LinkButton btnInsert = (LinkButton)FormView1.FindControl("InsertButton");
    TextBox txtDateAdded = (TextBox)FormView1.FindControl("ItemDateAddedTextBox");
    TextBox txtDateChanged = (TextBox)FormView1.FindControl("ItemDateChangedTextBox");
    TextBox txtChangedBy = (TextBox)FormView1.FindControl("ItemChangedByTextBox");

    txtDateAdded.Text = DateTime.Now.ToString("MMMM dd, yyyy");
    txtDateChanged.Text = DateTime.Now.ToString("MMMM dd, yyyy");
    txtChangedBy.Text = HttpContext.Current.Request.ServerVariables["AUTH_USER"].ToString();

    tblItems.Insert();
}

:告诉我 ItemName 字段为 NULL,并引发错误,即使我可以清楚地看到文本框中正在设置该值。为什么这个值被抛出为 NULL?在调用 Insert() 之前是否需要手动创建 INSERT 语句?我该怎么做呢?

I am attempting to insert an items using a basic FormView control.

I am somewhat new to C# and its controls, so bear with me.
The FormView has standard entries like:

ItemName
ItemPrice
ItemSize

It also has controls that will be hidden from the user such as:

ItemDateCreated
ItemDatechanged
ItemChangedBy

These items I am attempting to modify their values before the Insert() takes place, so I have captured the event InsertButton_Click():

protected void InsertButton_Click(object sender, EventArgs e)
{
    LinkButton btnInsert = (LinkButton)FormView1.FindControl("InsertButton");
    TextBox txtDateAdded = (TextBox)FormView1.FindControl("ItemDateAddedTextBox");
    TextBox txtDateChanged = (TextBox)FormView1.FindControl("ItemDateChangedTextBox");
    TextBox txtChangedBy = (TextBox)FormView1.FindControl("ItemChangedByTextBox");

    txtDateAdded.Text = DateTime.Now.ToString("MMMM dd, yyyy");
    txtDateChanged.Text = DateTime.Now.ToString("MMMM dd, yyyy");
    txtChangedBy.Text = HttpContext.Current.Request.ServerVariables["AUTH_USER"].ToString();

    tblItems.Insert();
}

It keeps telling me that the ItemName field is NULL, and throws an error, even though I can plainly see the value is being set in the textbox. Why is this value being thrown as NULL? Do I need to manually create the INSERT statement before I call the Insert()? How would I go about doing that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月染过的梦 2024-11-03 13:20:58

如果没有看到您的 .aspx 代码,很难确定,但 ItemName 未包含在您提供的代码示例中。听起来您的数据绑定语法可能不正确,因此页面上文本框中的值未正确映射到插入命令中的参数。

此外,您可能想要做的不是隐藏文本框控件,而是与数据源上的 Inserting 事件相关联。此示例假设您使用的是 SqlDataSource 控件,因此请根据您的特定数据源进行更改:

private void On_Inserting(Object sender, SqlDataSourceCommandEventArgs e)
{
    e.Command.Parameters.Add(new SqlParameter("ItemDateAddedTextBox", DateTime.Now.ToString("MMMM dd, yyyy")));
    ...
}

将此处理程序附加到 .aspx 页面中数据源的定义中。在网页的设计视图中,单击数据源,然后在属性窗口中单击“事件”选项卡。如果双击插入事件,它应该在代码隐藏中为您创建处理程序,并且您只需填写实现即可。

Without seeing your .aspx code it's hard to tell for sure, but ItemName wasn't included in the code sample you gave. It sounds like your databinding syntax might not be correct, so the value in the textbox on your page isn't getting correctly mapped to the parameter in your Insert command.

Also, what you probably want to do instead of having hidden textbox controls is tie into the Inserting event on your datasource. This example assumes you're using a SqlDataSource control, so change according to your specific datasource:

private void On_Inserting(Object sender, SqlDataSourceCommandEventArgs e)
{
    e.Command.Parameters.Add(new SqlParameter("ItemDateAddedTextBox", DateTime.Now.ToString("MMMM dd, yyyy")));
    ...
}

You attach this handler in the definition of your datasource in your .aspx page. In the design view of your webpage, click on the datasource and in the properties window, click over to the Events tab. If you double-click the Inserting event, it should create the handler for you in your codebehind, and you just fill in your implementation.

夜清冷一曲。 2024-11-03 13:20:58

如果字段值是从系统设置的,那么我建议直接在 Sql 中进行设置使用 getdate 而不是通过 ASP.NET 代码。

例如:

insert into mytable (...,..., ItemDateCreated) values (...,...,getdate()) 

If the field values are set from system then I would recommend doing that directly in Sql using getdate and not via asp.net code.

e.g.:

insert into mytable (...,..., ItemDateCreated) values (...,...,getdate()) 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文