2011-06-14 53 views
1

我遇到EditItemTemplate问题。从CodeBehind访问EditItemTemplate

我想要做的是更新我的TextBox txt_name,但我不能得到什么用户入口而我得到的旧值,而不是在代码隐藏。

我错过了什么吗?

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

后面的代码

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

我已经很长时间,因为我已经做了我的最后一页,所以像回发的东西是我可能错过的东西 – apacay 2011-06-14 18:24:34

回答

2

你如果页面的IsPostBack结合网格检查时?如果是Page_Load,那么您不需要在Page_Load中重新绑定它,否则您将失去新值,因为Page Init,Page Load事件发生在其他事件(例如Grid Updating事件)之前。

+0

感谢您的回答,我会检查并告诉你。你也许是对的...... =) – apacay 2011-06-14 18:15:36

1

也许你正在绑定你每个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) 

此检查,如果格处于编辑模式。如果网格不处于编辑模式,则可以将网格与数据源绑定。

+0

很好的答案,你也是对的。谢谢! – apacay 2011-06-14 18:20:47