asp.net GridView 中 TemplateField 中的 UpdateCommand 问题
我有以下网格:
<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>
当我单击“编辑”按钮并尝试更新名称时,上面的代码永远不会更新名称
当我将名称更新参数更改为
<asp:ControlParameter ControlID="txtGridAccountName" Name="Name" PropertyName="Text" Type="String" />
页面崩溃时“无法在 ControlParameter Name 中找到控件 txtGridAccountName” 。我相信这是因为当网格被渲染时,模板字段中的文本框获得了不同的 ID(类似于 ct100$txtGridAccountName$..),并且显然没有找到它。
帐户名称首先呈现为模板字段,因为我将其显示为详细信息/交易页面的链接。
如果我删除模板字段并显示名称,这样就可以了。
任何帮助解决这个问题的帮助都是值得赞赏的。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要自行调试,您应该挂钩 SQL 数据源更新事件和 gridview 行更新事件来检查问题。
无论如何,在不了解您的数据库设计的情况下,我的猜测是您缺少更新参数的类型。尝试这样的事情:
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: