简体   繁体   中英

Access EditItemTemplate from CodeBehind

I have problem dealing with EditItemTemplate.

What I am trying to do is to update my TextBox txt_name but I can't get what the user ingresses And I get the old value instead at the code-behind.

Am I missing something?

FRONT CODE

<asp:GridView ID="GridView_account" runat="server" AutoGenerateColumns="false" ShowFooter="True"
    OnRowCancelingEdit="GridView_account_RowCancelingEdit" OnRowEditing="GridView_account_RowEditing"
    OnRowUpdating="GridView_account_RowUpdating" OnRowCommand="GridView_account_RowCommand"
    OnRowDeleting="GridView_account_RowDeleting" OnSelectedIndexChanged="GridView_account_SelectedIndexChanged"
    DataKeyNames="UID" Height="110px" Width="497px">
    <Columns>
        <asp:TemplateField HeaderText="UID" SortExpression="UID">
            <ItemTemplate>
                <asp:Label ID="label_accid" runat="server" Text='<%# Bind("UID") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="User Name (required)" SortExpression="NyA">
            <EditItemTemplate>
                <asp:TextBox ID="txt_name" runat="server" Text='<%# Bind("NyA") %>'/>
            </EditItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="txt_newname" runat="server"></asp:TextBox>
            </FooterTemplate>
            <ItemTemplate>
                <asp:Label ID="label_name" runat="server" Text='<%# Bind("NyA") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Email Address (required)" SortExpression="eMail">
            <EditItemTemplate>
                <asp:TextBox ID="txt_email" runat="server" Text='<%# Bind("eMail") %>'></asp:TextBox>
            </EditItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="txt_newemail" runat="server"></asp:TextBox>
            </FooterTemplate>
            <ItemTemplate>
                <asp:Label ID="label_email" runat="server" Text='<%# Bind("eMail") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Options" ShowHeader="False">
            <EditItemTemplate>
                <asp:LinkButton ID="LinkButton_update" runat="server" CausesValidation="True" CommandName="Update"
                    Text="Update"></asp:LinkButton>
                <asp:LinkButton ID="LinkButton_cancel" runat="server" CausesValidation="False" CommandName="Cancel"
                    Text="Cancel"></asp:LinkButton>
                <asp:LinkButton ID="LinkButton_delete" runat="server" CausesValidation="False" CommandName="Delete"
                    Text="Delete"></asp:LinkButton>
            </EditItemTemplate>
            <FooterTemplate>
                <asp:LinkButton ID="LinkButton_addnew" runat="server" CausesValidation="False" CommandName="AddNew"
                    Text="Add New"></asp:LinkButton>
            </FooterTemplate>
            <ItemTemplate>
                <asp:LinkButton ID="LinkButton_edit" runat="server" CausesValidation="False" CommandName="Edit"
                    Text="Edit"></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Manage Role" ShowHeader="False">
            <ItemTemplate>
                <asp:LinkButton ID="Selectbtn" runat="server" CausesValidation="False" CommandName="Select"
                    Text="Select"></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

CODE BEHIND

protected void GridView_account_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    user u = Business.GetUser(((Label)GridView_account.Rows[e.RowIndex].FindControl("label_accid")).Text);
    //HERE'S MY PROBLEM!
    u.fullname = ((TextBox)GridView_account.Rows[e.RowIndex].FindControl("txt_name")).Text;
    //txt_name is returning the old value and not the one the user has input recently.

    Business.UpdateUser(u);
    GridView_account.EditIndex = -1;
    fillgridview();
}//

Are you checking if Page IsPostback when binding your grid? You need to not rebind it in the Page_Load if it is a post back, or else you will lose the new values, because the Page Init, Page Load events happen before other events, such as a Grid Updating event.

May be you are binding you grid on every page_load event. If so, you need something like this:

 protected void Page_Load(object sender, EventArgs e)
 {
    if(!Page.IsPostBack)
    {
       GridView_account.DataSource = "data source";
       GridView_account.DataBind();
    }
 }

or instead of

 if(!Page.IsPostBack) 

you can use

 if (GridView_account.EditIndex == -1)

this checks, if grid is in edit mode. If grid is not in edit mode, you can bind your grid with data source.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM