views:

628

answers:

1

I have the following grid:

<asp:GridView DataSourceID="accountsDataSource" DataKeyNames="Id" ShowEditButton="true" ...>

  <asp:TemplateField HeaderText="Name">
    <ItemTemplate>
      <asp:Hyperlink ID="lnkGridEditEntry" runat="server" Text='<%# Bind("Name")%>' NavigateUrl="..." />
    </ItemTemplate>
    <EditItemTemplate>
      <asp:TextBox ID="txtGridAccountName" runat="server" Text='<%# Bind("Name") %>' />
    </EditItemTemplate>
  </asp:TemplateField>

</asp:GridView>

<asp:SqlDataSource ID="accountsDataSource" SelectCommand= "..." DeleteCommand= "..." UpdateCommand="update Account set [Name]=@Name where [Id]=@Id">
  <UpdateParameters>
    <asp:Parameter Name="Name" />
    <asp:Parameter Name="Id" />
  </UpdateParameters>
</asp:SqlDataSource>

When I click on the "Edit" button and try to update the Name, above code never updates the Name

When I change the Name update parameter to

<asp:ControlParameter ControlID="txtGridAccountName" Name="Name" PropertyName="Text" Type="String" />

the page crashes with "unable to find control txtGridAccountName in ControlParameter Name". I believe this is because the text box in the template field gets a different ID (something like ct100$txtGridAccountName$..) when the grid is rendered and obviously it is not found.

the accout name is rendered as a template field in the first place, because I'm displaying that as a link to a details/transactions page.

if I remove the template field and display the name as this works.

any help to solve this problem is appreciated.

A: 

To debug this yourself, you should hook into both the SQL datasource Updating event and the gridview Row Updating events to check for problems.

Anyways, without knowing about your database design, my guess is that you are missing the type on the Update Parameters. Try something like this:

<UpdateParameters>
  <asp:Parameter Name="Name" Type="String" />
  <asp:Parameter Name="Id" Type="Int64" />
</UpdateParameters>
BenB