繁体   English   中英

从 CodeBehind 访问 EditItemTemplate

[英]Access EditItemTemplate from CodeBehind

我在处理 EditItemTemplate 时遇到问题。

我想要做的是更新我的TextBox txt_name但我无法获得用户进入的内容而且我在代码隐藏处获得了旧值。

我错过了什么吗?

正面代码

<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>

代码背后

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();
}//

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

可能是您在每个 page_load 事件上绑定您的网格。 如果是这样,你需要这样的东西:

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

或代替

 if(!Page.IsPostBack) 

您可以使用

 if (GridView_account.EditIndex == -1)

这会检查网格是否处于编辑模式。 如果网格未处于编辑模式,您可以将网格与数据源绑定。

暂无
暂无

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

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