一个费解的问题,datalist不能修改数据项?
做了一个简单的网页,是把一些系放在datalist里面,然后想实现重命名,删除等操作,但是我点击“重命名”,“删除”链接时没有反映,点击“重命名”linkbutton并没有转到EditItemTemplate,点击“删除”linkbutton也没能删除,请强人帮我看一下啊,呵呵,应该很简单的。代码如下:
<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1" DataKeyField="id">
<ItemTemplate>
编号:<asp:Label ID="Label1" runat="server" Text='<%# Eval("id") %>'></asp:Label> 系名:<asp:Label
ID="Label2" runat="server" Text='<%# Bind("department") %>'></asp:Label><br />
<asp:LinkButton ID="LinkButton3" runat="server" CommandName="Edit">重命名</asp:LinkButton>
<asp:LinkButton ID="LinkButton4" runat="server" CommandName="Delete">删除</asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
系名:<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("department") %>'></asp:TextBox>
<br />
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Update">修改</asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="Cancel">取消</asp:LinkButton>
</EditItemTemplate>
</asp:DataList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [id], [department] FROM [department]" DeleteCommand="DELETE FROM department WHERE id=@id" UpdateCommand="UPDATE department SET department=@department WHERE id=@id">
<DeleteParameters>
<asp:Parameter Name="id" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="department" />
<asp:Parameter Name="id" />
</UpdateParameters>
</asp:SqlDataSource>
知道的帮我一下啊,谢谢啦
------解决方案--------------------这样写肯定不行啦,还是在代码分离,在cs事件里写操作吧
------解决方案--------------------先问问您。。不仅仅是删除,您点击修改也没有反应吧?
------解决方案--------------------判断 CommandName是什么值,当是Edit时就去修改数据源重新绑定。其他也一样。
------解决方案--------------------
<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1" DataKeyField="id" OnDeleteCommand="DataList1_DeleteCommand" OnUpdateCommand="DataList1_UpdateCommand" >
<ItemTemplate>
编号: <asp:Label ID="Label1" runat="server" Text=' <%# Eval("id") %>'> </asp:Label> 系名: <asp:Label
ID="Label2" runat="server" Text=' <%# Bind("department") %>'> </asp:Label> <br />
<asp:LinkButton ID="LinkButton3" runat="server" CommandName="Edit">重命名 </asp:LinkButton>
<asp:LinkButton ID="LinkButton4" runat="server" CommandName="Delete">删除 </asp:LinkButton>
</ItemTemplate>