更新gridview行,添加旧值和新值

编程入门 行业动态 更新时间:2024-10-20 01:27:38
本文介绍了更新gridview行,添加旧值和新值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个Gridview,内部还有另一个嵌套的GridView.当我按下加号按钮时,嵌套的GridView使用JavScript展开.嵌套的GridView在使用TextBox控件的编辑模式下展开.因此,当用户在TextBox上键入内容时,便可以使用更新"按钮来更新单元格.我的问题是,当我按下更新按钮时,会发生更新,但不是我期望的那样.例如,如果某个单元格的初始值为我的名字是彼得",而我已经完成了我没有名字"的编辑,那么将要保存的新值就是这样:我的名字是彼得,我不知道没有名字".嵌套GridView的数据绑定发生在父GridView DataBound事件上. 我的代码:

I have a Gridview and inside I have another one nested GridView. When I press a plus button the nested GridView expands using a JavScript. The nested GridView expands on edit mode using TextBox controls. So when the user types on a TextBox would have the ability to update the cell using an update button. My problem is that when I press the update button the update occurs but not how I would expected. If for example the initial value of a cell was "My name is Peter" and I have done the edit "I don’t have a name" The new value that will be saved is exactly this: "My name is Peter, I don’t have a name". The databind of the nested GridView occurs on the parent GridView DataBound event. My code:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" OnPageIndexChanging="gridView_PageIndexChanging" AutoGenerateColumns="False" DataKeyNames="myitemID" OnRowDataBound="GridView_RowDataBound"> <Columns> <asp:TemplateField> <ItemTemplate> <img alt = "" style="cursor: pointer" src="../plus.png" /> <asp:GridView ID="nestedGridView" runat="server" AutoGenerateColumns="False" DataKeyNames="mynestedID"> <Columns> <asp:TemplateField HeaderText="nestedID" Visible="false" ItemStyle-Width="20%" SortExpression="nesteditemID"> <ItemTemplate> <asp:Label ID="nesteditemID" runat="server" Text='<%# Bind("nesteditemID") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Name" ItemStyle-Width="20%" SortExpression="Name"> <ItemTemplate> <asp:TextBox ID="name" TextMode="MultiLine" Width="80%" Rows="3" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox> </ItemTemplate> </asp:TemplateField> <asp:Panel ID="mypanel" runat="server"> <table> <tr> <td> &nbsp; <asp:ImageButton ID="ImageButton2" OnClick="updatename_Click" ImageUrl="~/images/update.jpg" Width="15px" Height="15px" runat="server"></asp:ImageButton> </td> </tr> </table> </asp:Panel> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </asp:Panel> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="myitemID" InsertVisible="False" SortExpression="myitemID" Visible="False"> <ItemTemplate> <asp:Label ID="myitemID" runat="server" Text='<%# Bind("myitemID") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="ItemName" ItemStyle-Width="20%" SortExpression="ItemName"> <ItemTemplate> <asp:Label ID="ItemName" runat="server" Text='<%# Bind("ItemName") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>

cs代码:

protected void updatename_Click(object sender, EventArgs e) { GridViewRow masterrow = (GridViewRow)(sender as Control).Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent; GridViewRow row = (GridViewRow)(sender as Control).Parent.Parent.Parent; int index = row.RowIndex; int mi = masterrow.RowIndex; int i = index; GridView nestedGridView = (GridView)GridView1.Rows[mi].FindControl("nestedGridView"); Label nestedID = (Label)nestedGridView.Rows[index].FindControl("nestedID"); int sbid = Convert.ToInt32(nestedID.Text); TextBox name = (TextBox)nestedGridView.Rows[index].FindControl("name"); string myname = Convert.ToString(name.Text); //update name with the new value Nesteditem updatenesteditem = mylinqobjects.Nesteditems.Single(p => p.nesteditemID == sbid); if (!string.IsNullOrEmpty(myname)) { updatenesteditem.nesteditemName = myname; mylinqobjects.SubmitChanges(); } }

推荐答案

通过删除旧文本替换了当前文本.

Replaced the current text by removing the old one.

string myname = name.Text.Substring(name.Text.LastIndexOf(",")+1);

尝试了所有可能性,但是由于嵌套的网格视图呈现及其限制,我们只能像上面那样做.

Tried all possiblities, but due nested grid view rendering and its restrictions, we could do only like above.

请提供其他解决方案.

更多推荐

更新gridview行,添加旧值和新值

本文发布于:2023-10-26 23:41:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1531683.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:gridview

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!