0

正如我在标题中所写,这个问题很有趣:

<asp:ScriptManager ID="ScriptManager1" runat="server" />
    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
        <ContentTemplate>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id_Newsletter" HeaderStyle-BackColor="Black" 
                    HeaderStyle-ForeColor="White" RowStyle-BackColor="DarkGray" AlternatingRowStyle-BackColor="LightGray" PageSize="6" AllowPaging="true"
                    AllowSorting="true" ShowHeaderWhenEmpty="true" EmptyDataText="La tabella non contiene dati" EnableViewState="true" RowStyle-Wrap="true"
                    PagerStyle-HorizontalAlign="Left" PagerStyle-BorderWidth="0" BorderWidth="0" OnRowCommand="GridView_RowCommand"
                    OnRowCancelingEdit="GridView_RowCancelingEdit" OnRowUpdating="GridView_RowUpdating" OnRowDeleting="GridView_RowDeleting"
                    OnPageIndexChanging="GridView_PageIndexChanging" OnSorting="GridView_Sorting" OnRowEditing="GridView_RowEditing" CssClass="NewsletterManager_GridView" >
        <Columns>
        <asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText="Indirizzo Email" HeaderStyle-Width="275" SortExpression="emailNewsletter">
        <ItemTemplate>
            <asp:Label ID="lbl_emailNewsletter" runat="server" Text='<%# Eval("emailNewsletter") %>'></asp:Label>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:Label ID="lbl_emailNewsletter" runat="server" Text='<%# Eval("emailNewsletter") %>' Visible="false" ></asp:Label>
            <asp:TextBox ID="txt_emailNewsletter" runat="server" Text='<%# Bind("emailNewsletter") %>'></asp:TextBox>
        </EditItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText="Azioni" HeaderStyle-Width="100">
        <EditItemTemplate>
            <asp:ImageButton ID="Img_Aggiorna" runat="server" CommandName="Update" ImageUrl="~/images/GridIcon/update.png" ToolTip="Aggiorna" />&nbsp;&nbsp;
            <asp:ImageButton ID="Img_Annulla" runat="server" CommandName="Cancel" ImageUrl="~/images/GridIcon/undo.png" ToolTip="Annulla" />
        </EditItemTemplate>
        <ItemTemplate>
            <asp:ImageButton ID="Img_Modifica" runat="server" CommandName="Edit" ImageUrl="~/images/GridIcon/edit.png" ToolTip="Modifica" />&nbsp;&nbsp;
            <asp:ImageButton ID="Img_Cancella" runat="server" CommandName="Delete" ImageUrl="~/images/GridIcon/delete.png" OnClientClick="return confirm('Sei sicuro di voler cancellare l\'email?');" ToolTip="cancella" />
            </ItemTemplate>
        </asp:TemplateField>

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
        LoadGridView(GridView1, -1, 0);
    lbl_result.Text = String.Empty;
}

protected void GridView_RowEditing(Object sender, GridViewEditEventArgs e)
{
    LoadGridView(((GridView)sender), e.NewEditIndex, ((GridView)sender).PageIndex);
}

protected void GridView_RowUpdating(Object sender, GridViewUpdateEventArgs e)
{
    NewsLetterBL newsBl = new NewsLetterBL();
    String oldmail = ((Label)((GridView)sender).Rows[e.RowIndex].FindControl("lbl_emailNewsletter")).Text;
    String newmail = ((TextBox)((GridView)sender).Rows[e.RowIndex].FindControl("txt_emailNewsletter")).Text;
    if (newsBl.ModifyWrongEmail(oldmail, newmail))
        lbl_result.Text = "Modify Done";
    else lbl_result.Text = newsBl.LastMessage;

    LoadGridView(((GridView)sender), -1, ((GridView)sender).PageIndex);
}

protected void LoadGridView(GridView GV, int EditIndex, int PageIndex)
{
    NewsLetterBL newsBl = new NewsLetterBL();
    DataView dw = new DataView(newsBl.getFullNewsletter());

    String SortExpression = GV.ID.ToString() + "_SortExpression";

    if (ViewState[SortExpression] != null) dw.Sort = (String)ViewState[SortExpression];

    GV.DataSource = dw;
    GV.PageIndex = PageIndex;
    GV.EditIndex = EditIndex;
    GV.DataBind();
}

当我尝试从处理 的事件的txt_emailNewsletter编辑模式下将控件访问到行时 ,元素会抛出,但可以创建控件。GridView_RowUpdatingOnRowUpdatingGridView1NullReferenceExceptionlbl_emailNewsletter

ItemTemplate 和 EditItemTemplate 中的标签具有相同的 ID ( lbl_emailNewsletter):如果我将标签的 ID 更改为 ItemTemplate,则 String oldmail 和 newmail 都会引发 null Reference 异常;显然问题是,当我单击 ImageButton Img_Aggiornawith 时CommandName="Update",ASP.NET 出于某种神秘原因从已编辑的行中仅查找 ItemTemplate 的控件而不是 EditItemTemplate。

4

1 回答 1

0

GridView 事件“OnRowUpdating”仅在您使用数据源时才有用:在这种情况下,可以访问 Dictionary e.OldValues[] 或 e.NewValues[] 并正确获取旧值和新值。

我已经使用带有个性化 CommandName 的事件“OnRowCommand”和带有旧值的 CommandArgument 解决了这个问题:在这种情况下,可以使用新值访问 GridView 中已编辑行内的所有控件。

<asp:ImageButton ID="Img_Update" runat="server" CommandName="Change" CommandArgument='<%# Eval("emailNewsletter") %>' ImageUrl="~/images/GridIcon/update.png" ToolTip="Update" />

protected void GridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
    switch (e.CommandName)
    {
        case "Change":

            ImageButton btnNew = e.CommandSource as ImageButton;
            GridViewRow row = btnNew.NamingContainer as GridViewRow;
            TextBox txt_emailNewsletter = row.FindControl("txt_emailNewsletter") as TextBox;

            if ( !CheckEmail(e.CommandArgument.ToString()) || !CheckEmail(txt_emailNewsletter.Text) )
                lbl_result.Text = "ERROR : Email not Valid";
            else {
                if (txt_emailNewsletter.Text.Trim().ToLower() == e.CommandArgument.ToString())
                    lbl_result.Text = "Email values are indentical";
                else
                {
                    //Business Layer Call
                    NewsLetterBL newsBl2 = new NewsLetterBL();
                    if (newsBl2.ModifyWrongEmail(e.CommandArgument.ToString(), txt_emailNewsletter.Text.Trim().ToLower()))
                        lbl_result.Text = "Email Changed";
                    else
                        lbl_result.Text = "ERROR : " + newsBl2.LastMessage;
                }
            }

            LoadDataForGridView(((GridView)sender));
            BindGridView(((GridView)sender), -1, ((GridView)sender).PageIndex);
            break;
    }
}
于 2013-01-21T19:32:11.630 回答