Vb.net – Binding value to textbox inside of gridview

asp.netbindinggridviewtextboxvb.net

I have problems with binding a value of a field inside a gridview to a textbox which is inside the gridview as well. I'm intending to do this for editing the table.

I tried to do this with eval and bind, but the textbox won't show the values and I have absolutely no clue why.

<asp:UpdatePanel runat="server">
    <ContentTemplate>
        <asp:GridView ID="gvBS" runat="server" AutoGenerateColumns="false" DataKeyNames="ID" SkinID="gvWithoutWidth">
         <Columns>
            <asp:CommandField ShowEditButton="true" EditImageUrl="~/Images/GridView/gv_edit.png" ButtonType="Image" 
                CancelImageUrl="~/Images/GridView/gv_cancel.png" UpdateImageUrl="~/Images/GridView/gv_update.png"/>
            <asp:TemplateField HeaderText="Sollmonat" HeaderStyle-HorizontalAlign="Left">
                <EditItemTemplate>
                    <asp:TextBox ID="tbSollMonat" runat="server" Text='<%# Eval("SollMonat") %>'></asp:TextBox>
                    <asp:RequiredFieldValidator ID="rfvSollMonat" ValidationGroup="Update" runat="server"
                        ControlToValidate="tbSollMonat" ErrorMessage="Bitte Sollmonat (dd.mm.yyyy) angeben"
                        SetFocusOnError="true">*</asp:RequiredFieldValidator> 
                    <asp:RegularExpressionValidator ID="revSollMonat" ValidationGroup="Update" runat="server"
                        ValidationExpression="^\d+$" ControlToValidate="tbSollMonat">*</asp:RegularExpressionValidator>
                </EditItemTemplate>
                <ItemTemplate>
                    <%# Eval("SollMonat")%>
                </ItemTemplate>
            </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </ContentTemplate>
</asp:UpdatePanel>

The thing is that it works fine inside the ItemTemplate, but doesn't inside the EditItemTemplate-element. Really no clue what the problem is.

Code behind:

Sub gvBS_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs) Handles gvBS.RowEditing
        gvBS.EditIndex = e.NewEditIndex
    End Sub

    Sub gvBS_RowCancelingEdit() Handles gvBS.RowCancelingEdit
        Me.gvBS.EditIndex = -1
        gvBS_DataBind()
    End Sub

Best Answer

I assume that the GridView enters never edit-mode since you aren't handling the RowEditing event or you didn't DataBind it after you've set gvBS.EditIndex = e.NewEditIndex;.

<asp:GridView 
    OnRowEditing="gvBS_RowEditing" OnRowCancelingEdit="gvBS_RowCancelingEdit"
    ID="gvBS" runat="server" AutoGenerateColumns="false" 
    DataKeyNames="ID" SkinID="gvWithoutWidth">

codebehind (BindGrid is the method which databinds your grid):

protected void gvBS_RowEditing(object sender, GridViewEditEventArgs e)
{
    gvBS.EditIndex = e.NewEditIndex;
    BindGrid();
}

protected void gvBS_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
    gvBS.EditIndex = -1;
    BindGrid();
}

You should also remember to databind it only on the first load, not on consecutive postbacks when ViewState is enabled(default). Therefore you can check the page's IsPostBack property:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        BindGrid();
    }
}
Related Topic