【问题标题】:e.NewValues return Null in Asp.Net DetailsView ItemUpdatinge.NewValues 在 Asp.Net DetailsView ItemUpdating 中返回 Null
【发布时间】:2014-04-08 08:54:35
【问题描述】:

我有一个详细信息视图来显示用户详细信息并在必要时对其进行更新。我正在使用 e.NewValues 来获取要更新的编辑状态值。但它会引发空异常。代码如下。

    <asp:DetailsView ID="MyDetailsView" CssClass="gv" runat="server" Width="100%" CellPadding="15"
    ForeColor="#333333" AutoGenerateEditButton="True" OnModeChanging="MyDetailsView_ModeChanging"
    AutoGenerateRows="False" OnItemUpdating="MyDetailsView_ItemUpdating">

    <FieldHeaderStyle CssClass="gvheader" Width="100px" />
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <CommandRowStyle BackColor="#E2DED6" Font-Bold="True" />
    <RowStyle BackColor="White" ForeColor="#333333" BorderColor="#666666" />
    <FieldHeaderStyle BackColor="#6F97BA" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <EditRowStyle BackColor="white" />
    <Fields>
        <asp:BoundField HeaderText="Address" DataField="Address" SortExpression="Address">
            <ControlStyle Height="25px" Width="450px" />
        </asp:BoundField>
        <asp:BoundField HeaderText="Postcode" DataField="Postcode" SortExpression="Postcode">
            <ControlStyle Height="25px" Width="450px" />
        </asp:BoundField>
        <asp:BoundField HeaderText="Country" DataField="Countries" SortExpression="Countries">
            <ControlStyle Height="25px" Width="450px" />
        </asp:BoundField>
    </Fields>

    </asp:DetailsView>

代码背后

    protected void MyDetailsView_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{

    string address = e.NewValues["Address"].ToString();
    string postcode = e.NewValues["Postcode"].ToString();
    string country = e.NewValues["Countries"].ToString();

    Client c = new Client();
    c.UpdateMyDetails((string)Session["Code"], address, postcode, country); 
    MyDetailsView.ChangeMode(DetailsViewMode.ReadOnly); 
    MyDetailsView.DataBind();

}

有人可以帮忙吗?

【问题讨论】:

  • 你在哪里绑定你的细节视图?
  • protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { BindMyDetailsView(); } }

标签: c# asp.net visual-studio-2010


【解决方案1】:

我知道这是一个老问题,但也许这会帮助像我一样遇到这个问题的其他人。

对于我的解决方案,我使用了 null tertiary Convert.ToString(e.NewValues["Countries"]) ?? " ";

这是另一个帮助我的帖子,其中有一些解释。 How to convert null value into string in c# .net?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多