【问题标题】:UpdateMethod of ObjectDataSource not reflecting in the DB on Run but works in DebuggingObjectDataSource 的 UpdateMethod 未在运行时反映在数据库中,但在调试中有效
【发布时间】:2014-01-22 04:58:28
【问题描述】:

标题听起来很奇怪,但这就是实际发生的事情。 我找到了有类似问题的人,但这些页面上没有答案。 我有一个绑定到 ObjectDataSource 的 DetailsView 控件 代码:

<asp:DetailsView ID="DetailsView1" runat="server" BackColor="#7B7C95"   BorderColor="White" BorderWidth="1px" CellPadding="2" ForeColor="White" GridLines="None" Style="height: 80%; width: 80%" AutoGenerateRows="False" DataSourceID="UserTableObjectDataSource" Font-Bold="True" Font-Names="Tahoma" AutoGenerateEditButton="True" OnItemUpdating="DetailsView1_ItemUpdating">
    <AlternatingRowStyle BackColor="#393847" />
    <FieldHeaderStyle VerticalAlign="Middle" />
    <Fields>
        <asp:TemplateField ShowHeader="False">
            <ItemTemplate>
                <div style="width: 50%; height: auto; overflow: auto;">
                    <asp:Image ID="Image1" runat="server" AlternateText="No Image Added" ImageUrl='<%# Eval("ImageUrl") %>' />
                </div>
            </ItemTemplate>
            <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
        </asp:TemplateField>
        <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" ReadOnly="True" />
        <asp:BoundField DataField="PackagesBooked" HeaderText="Packages Booked" SortExpression="PackagesBooked" ReadOnly="True">
            <ItemStyle Font-Underline="True" />
        </asp:BoundField>
        <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
        <asp:BoundField DataField="PhoneNumber" HeaderText="PhoneNumber" SortExpression="PhoneNumber" />
    </Fields>
    <FooterStyle BackColor="Tan" VerticalAlign="Middle" />
    <HeaderStyle BackColor="Tan" Font-Bold="True" VerticalAlign="Middle" />
    <PagerStyle BackColor="Silver" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
</asp:DetailsView>

<asp:ObjectDataSource ID="UserTableObjectDataSource" runat="server" SelectMethod="getUser" TypeName="HolidaysForYou.DAL.DbHandler" UpdateMethod="updateUser" ValidateRequestMode="Enabled">
    <SelectParameters>
        <asp:SessionParameter Name="username" SessionField="Username" Type="String" />
    </SelectParameters>
    <UpdateParameters>
        <asp:Parameter Name="Email" Type="String" />
        <asp:Parameter Name="PhoneNumber" Type="String" />
        <asp:SessionParameter Name="username" SessionField="Username" Type="String" />
    </UpdateParameters>
</asp:ObjectDataSource>

更新方法是这样的

 public static void updateUser(string Email, string PhoneNumber, string username)
        {
            string usernameLower = username.ToLower();
            string queryString = "Update [UserTable] SET Email='" + Email + "',PhoneNumber='" + PhoneNumber + "' WHERE ([Name] = '" + usernameLower + "')";
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                SqlCommand command = new SqlCommand(queryString, connection);
                try
                {
                    connection.Open();
                    command.BeginExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    throw ex;
                }

            }
        }

当我在调试模式下运行程序并一步一步缓慢运行时,如果我只是运行它,程序就可以完美运行,Db 没有任何变化。

我还尝试从 DetailsView1_ItemUpdating 函数中调用 updateUser 函数,例如:

 protected void DetailsView1_ItemUpdating(object sender, System.Web.UI.WebControls.DetailsViewUpdateEventArgs e)
        {
            try
            {
                string newEmail = (string)e.NewValues["Email"];
                string newPhoneNumber = (string)e.NewValues["PhoneNumber"];
                if (!UserHomeValidate.userUpdateValidate(newEmail, newPhoneNumber))
                {
                    e.Cancel = true;
                    Error.Visible = true;
                }
                else
                    UserHomeValidate._updateUser(newEmail, newPhoneNumber, Session["Username"].ToString().ToUpper());
            }
            catch (Exception exception)
            {
                Utility.LogFile.CreateLogFile(exception);
            }
        }

其中 _updateUser 与 updateUser 完全相同,并且数据库仍然没有更改......这可能与回发有关,但我不知道它是如何工作的...... 这令人沮丧,将不胜感激 lil 帮助..

【问题讨论】:

    标签: asp.net sql-update objectdatasource detailsview


    【解决方案1】:

    解决了这个问题... 只需要添加

    if (!Page.IsPostBack)
                        DetailsView1.DataBind();
    

    到 page_Load 以避免在回发时绑定,因为旧值将被传递...

    【讨论】:

      猜你喜欢
      • 2017-05-26
      • 2017-05-17
      • 1970-01-01
      • 2015-03-16
      • 1970-01-01
      • 2016-11-23
      • 1970-01-01
      • 2023-01-04
      • 2012-05-17
      相关资源
      最近更新 更多