简体   繁体   English

从 CodeBehind 访问 EditItemTemplate

[英]Access EditItemTemplate from CodeBehind

I have problem dealing with EditItemTemplate.我在处理 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.我想要做的是更新我的TextBox txt_name但我无法获得用户进入的内容而且我在代码隐藏处获得了旧值。

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?绑定网格时是否检查 Page IsPostback? 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.如果是回发,则不需要在 Page_Load 中重新绑定它,否则您将丢失新值,因为 Page Init、Page Load 事件发生在其他事件之前,例如 Grid Updating 事件。

May be you are binding you grid on every page_load event.可能是您在每个 page_load 事件上绑定您的网格。 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.如果网格未处于编辑模式,您可以将网格与数据源绑定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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