asp.netsqldatasourcetemplatefieldupdatecommand

updatecommand in asp page not working


ok i have solved my last question by using update parameters instead und putting templatefields on my gridview instead of boundfields.

it is displayed alright, only thing is the update button doesn't seem to work. when i click it, it just returns to the previous window without updating the data fields in the database.

could you please look at the following code and tell me if i missed something here? thanks....

WORKING:

 <asp:GridView DataKeyNames="BenutzerID" ID="grdBenutzer" runat="server" Visible="False" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" OnRowCommand="GridView1_RowCommand">
            <Columns>
                <asp:BoundField ReadOnly="true" DataField="BenutzerID" HeaderText="ID" />
                <asp:TemplateField HeaderText="Titel">
                    <ItemTemplate>
                        <%# Eval("Titel")%>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox runat="server" ID="txtTitel" Text='<%# Bind("Titel")%>'></asp:TextBox>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Bezeichnung">
                    <ItemTemplate>
                        <%# Eval("Bezeichnung")%>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:DropDownList AppendDataBoundItems="true" runat="server" ID="ddwnBezeichnung" Text='<%# Bind("Bezeichnung")%>'>
                            <asp:ListItem Text="Mitarbeiter" Value="Mitarbeiter"></asp:ListItem>
                            <asp:ListItem Text="Praktikant" Value="Praktikant"></asp:ListItem>
                            <asp:ListItem Text="Azubi" Value="Azubi"></asp:ListItem>
                            <asp:ListItem Text="Umschüler" Value="Umschüler"></asp:ListItem>
                        </asp:DropDownList>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Vorname">
                    <ItemTemplate>
                        <%# Eval("Vorname")%>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox runat="server" ID="txtVorname" Text='<%# Bind("Vorname")%>'/>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Nachname">
                    <ItemTemplate>
                        <%# Eval("Nachname")%>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox runat="server" ID="txtNachname" Text='<%# Bind("Nachname")%>'/>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:BoundField ReadOnly="true" HeaderText="Geburtsdatum" DataField="Geburtsdatum" />
                <asp:BoundField ReadOnly="true" HeaderText="Geburtsort" DataField="Geburtsort" />
                <asp:TemplateField HeaderText="Straße">
                    <ItemTemplate>
                        <%# Eval("Straße")%>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox runat="server" ID="txtStraße" Text='<%# Bind("Straße")%>' />
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Nationalität">
                    <ItemTemplate>
                        <%# Eval("Nationalität")%>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox runat="server" ID="txtNationalität" Text='<%# Bind("Nationalität")%>' />
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Hausnummer">
                    <ItemTemplate>
                        <%# Eval("Hausnummer")%>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox runat="server" ID="txtHausnummer" Text='<%# Bind("Hausnummer")%>' />
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="PLZ">
                    <ItemTemplate>
                        <%# Eval("PLZ")%>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox runat="server" ID="txtPLZ" Text='<%# Bind("PLZ")%>' />
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Ort">
                    <ItemTemplate>
                        <%# Eval("Ort")%>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox runat="server" ID="txtOrt" Text='<%# Bind("Ort")%>' />
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Land">
                    <ItemTemplate>
                        <%# Eval("Land")%>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:DropDownList AppendDataBoundItems="true" runat="server" ID="ddwnLand" Text='<%# Bind("Land")%>'>

                        </asp:DropDownList>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Mobil">
                    <ItemTemplate>
                        <%# Eval("Mobil")%>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox runat="server" ID="txtMobil" Text='<%# Bind("Mobil")%>' />
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:BoundField ReadOnly="true" DataField="Status" HeaderText="Status" />
                <asp:TemplateField HeaderText="Benutzerart">
                    <ItemTemplate>
                        <%# Eval("Benutzerart")%>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:DropDownList AppendDataBoundItems="true" runat="server" ID="ddwnBenutzerart" Text='<%# Bind("Benutzerart")%>'>
                            <asp:ListItem Text="Mitarbeiter" Value="Mitarbeiter"></asp:ListItem>
                            <asp:ListItem Text="Geschäftsführung" Value="Geschäftsführung"></asp:ListItem>
                            <asp:ListItem Text="Führungskraft" Value="Führungskraft"></asp:ListItem>
                            <asp:ListItem Text="Administrator" Value="Administrator"></asp:ListItem>
                        </asp:DropDownList>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Homepage">
                    <ItemTemplate>
                        <%# Eval("Homepage")%>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox runat="server" ID="txtHomepage" Text='<%# Bind("Homepage")%>' />
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Email">
                    <ItemTemplate>
                        <%# Eval("Email")%>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox runat="server" ID="txtEmail" Text='<%# Bind("Email")%>' />
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Festnetz">
                    <ItemTemplate>
                        <%# Eval("Festnetz")%>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox runat="server" ID="txtFestnetz" Text='<%# Bind("Festnetz")%>' />
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Fax">
                    <ItemTemplate>
                        <%# Eval("Fax")%>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox runat="server" ID="txtFax" Text='<%# Bind("Fax")%>' />
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Urlaubstage">
                    <ItemTemplate>
                        <%# Eval("UrlaubstageInsgesamt")%>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox runat="server" ID="txtUrlaubstageInsgesamt" Text='<%# Bind("UrlaubstageInsgesamt")%>' />
                    </EditItemTemplate>                        
                </asp:TemplateField>
                <asp:CommandField HeaderText="Ändern" ButtonType="Button" ShowEditButton="true" ShowCancelButton="true" />
                <asp:CommandField HeaderText="Neu" ButtonType="Button" ShowInsertButton="true" />
                <asp:CommandField HeaderText="Archivieren" ButtonType="Button" ShowDeleteButton="true"/>
            </Columns>
        </asp:GridView>

Solution

  • Everywhere that you have the following code in your EditItemTemplates:

    Text='<%# Eval("fieldName")%>'
    

    You need to change it to this:

    Text='<%# Bind("fieldName")%>'
    

    Eval is for one-way (read only) databinding. You want two-way databinding, in order to update the database values.


    You also need to set the DataKeyNames property in your GridView:

    <asp:GridView ID="grdBenutzer" runat="server" Visible="False" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" OnRowCommand="GridView1_RowCommand" DataKeyNames="BenutzerID" >
    

    This is required for the auto-update functionality to work, per the documentation (linked above):

    Use the DataKeyNames property to specify the field or fields that represent the primary key of the data source. You must set the DataKeyNames property in order for the automatic update and delete features of the GridView control to work.