e.NewValues在Asp.Net DetailsView ItemUpdating中返回Null

时间:2014-04-08 08:54:35

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

我有一个详细信息视图来显示用户详细信息并在必要时进行更新。我正在使用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();

}

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

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

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

这是另一篇帮我解释的帖子。 How to convert null value into string in c# .net?