c#
  • asp.net
  • gridview
  • templatefield
  • 2015-04-03 53 views 1 likes 
    1

    我无法检索在我的GridView中的文本框模板字段中输入的新值。无法从GridView模板字段中检索新值

    这里是我的标记:

    <asp:TemplateField HeaderText="username" SortExpression="username"> 
        <ItemTemplate> 
         <asp:Label ID="Label2" runat="server" Text='<%# Bind("username") %>'></asp:Label> 
        </ItemTemplate> 
        <EditItemTemplate> 
         <asp:TextBox ID="txtUserName" runat="server" Text='<%# Bind("username") %>'></asp:TextBox> 
        </EditItemTemplate> 
    </asp:TemplateField> 
    

    这里是如何我试图检索新的价值,我的GridView的RowCommand事件处理中:

    string userName = ((TextBox)grdUserList.Rows[rowIndex].FindControl("txtUserName")).Text; 
    

    我得到的是旧值而不是新输入的值,当我执行此代码。

    有人知道我失踪了吗?提前致谢。

    +0

    你能告诉我们你绑定gridview的代码吗? – Adil 2015-04-03 05:14:43

    +0

    @Adil嗨。我刚刚发现了解决问题的方案。我搜索并发现在检索过程开始之前GridView正在刷新,因为我正在重新绑定Page_Load方法上的GridView。我通过不重新绑定GridView的方式解决了这个问题(或者至少在我做出更改之前)。感谢您的回复无论如何:) – Rian 2015-04-03 05:34:01

    +0

    我在想,你绑定GridView的回发和丢失新输入的值。 – Adil 2015-04-03 05:39:09

    回答

    0
    use this code in gridview  
    
        <Columns> 
    
        <asp:TemplateField HeaderText="SrNo"> 
        <EditItemTemplate> 
    
        <asp:TextBox ID="txtsrno" runat="server" Text='<%#Eval("SrNo") %>'> 
        </asp:TextBox> 
        </EditItemTemplate> 
    
    
        <ItemTemplate> 
        <asp:Label ID="lblsrno" runat="server" Text='<%#Eval("SrNo") %>'> 
        </asp:Label> 
    
        </ItemTemplate> 
        </asp:TemplateField> 
        </Columns> 
    
    1

    您正在错误的GridView事件中检索新值。您必须在GridView控件中添加OnRowUpdating="grdUserList_RowUpdating"事件,然后检索新的TextBox值。

    OnRowUpdating事件的代码隐藏:

    protected void grdUserList_RowUpdating(object sender, GridViewUpdateEventArgs e) 
    { 
        string userName = ((TextBox)grdUserList.Rows[e.RowIndex].FindControl("txtUserName")).Text; 
    
        // Write your update query and logic over here. 
    } 
    

    您可以从here额外的知识提供参考。

    请让我知道如果您有任何问题。

    1

    我刚刚发现了我的问题的解决方案。我搜索并发现在检索过程开始之前GridView正在刷新,因为我正在重新绑定Page_Load方法上的GridView。我通过在使用IsPostback方法进行回发(或者至少不是在进行更改之前)时重新绑定gridview来解决问题。感谢大家的回复:)

    相关问题