我正在使用具有添加,更新和删除功能的嵌套网格视图。添加和删除操作正在成功运行。更新内部网格的特定条目时出现问题,它以逗号分隔的前一个值更新值。我不知道为什么会这样。
在更新外部网格值时,它会在没有重复值的情况下更新。
我的嵌套gridview的代码:
<asp:GridView ID="grdViewMaster" runat="server" AutoGenerateColumns="false"
DataKeyNames="MasterID" CssClass="dataTable" ShowFooter="true"
OnRowCommand="grdViewMaster_RowCommand" OnRowEditing="grdViewMaster_RowEditing"
OnRowUpdating="grdViewMaster_RowUpdating"
OnRowDeleting="grdViewMaster_RowDeleting"
OnRowCancelingEdit="grdViewMaster_RowCancelingEdit"
OnRowDataBound="grdViewMaster_OnRowDataBound">
<Columns>
<asp:TemplateField HeaderText="Options" ItemStyle-CssClass="mastertabletd" HeaderStyle-CssClass="mastertabletd" FooterStyle-CssClass="mastertabletd">
<EditItemTemplate>
<asp:ImageButton ID="imgbtnUpdateMaster" CommandName="Update" runat="server" ImageUrl="~/images/1-update.png" ToolTip="Update" Height="15px" Width="15px" />
<asp:ImageButton ID="imgbtnCancelMaster" runat="server" CommandName="Cancel" ImageUrl="~/images/1-Cancel.png" ToolTip="Cancel" Height="15px" Width="15px" />
</EditItemTemplate>
<ItemTemplate>
<asp:ImageButton ID="imgbtnEditMaster" CommandName="Edit" runat="server" ImageUrl="~/images/1-edit.png" ToolTip="Edit" Height="15px" Width="15px" />
<asp:ImageButton ID="imgbtnDeleteMaster" CommandName="Delete" ImageUrl="~/images/delete.png" runat="server" ToolTip="Delete" Height="15px" Width="15px" />
<a href="javascript:divexpandcollapse('div<%# Eval("MasterID")%>')">
<img id='imgdiv<%# Eval("MasterID")%>' alt="Details" src="images/plus.png" />
</a>
</ItemTemplate>
<FooterTemplate>
<asp:ImageButton ID="imgbtnAddMaster" runat="server" ImageUrl="~/images/1-add.png" CommandName="AddNewMaster" Width="15px" Height="15px" ToolTip="Add new User" ValidationGroup="validaiton" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Style #" ItemStyle-CssClass="mastertabletd" HeaderStyle-CssClass="mastertabletd" FooterStyle-CssClass="mastertabletd">
<EditItemTemplate>
<asp:TextBox ID="txtEditStyleID" runat="server" Text='<%#Eval("StyleID") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="txtStyleID" runat="server" Text='<%# Eval("StyleID") %>' />
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtftrStyleID" runat="server" CssClass="width90per" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="0" HeaderStyle-Width="0"
FooterStyle-Width="0" ItemStyle-CssClass="mastertabletd"
HeaderStyle-CssClass="mastertabletd" FooterStyle-CssClass="mastertabletd">
<ItemTemplate>
<div id='div<%# Eval("MasterID")%>' style="display: none;" class="dataTables_wrapper">
<asp:GridView ID="gvExcelData" DataKeyNames="ID" runat="server"
GridLines="Horizontal" ShowFooter="true" CssClass="dataTable"
BorderStyle="None" EmptyDataText="No Data Found"
AutoGenerateColumns="False"
OnRowCommand="gvExcelData_RowCommand"
OnRowEditing="gvExcelData_RowEditing"
OnRowUpdating="gvExcelData_RowUpdating"
OnRowDeleting="gvExcelData_RowDeleting"
OnRowCancelingEdit="gvExcelData_RowCancelingEdit">
<EmptyDataRowStyle CssClass="form-horizontal fill-up validatable width100per" />
<Columns>
<asp:TemplateField HeaderText="Options">
<EditItemTemplate>
<asp:ImageButton ID="imgbtnUpdate" CommandName="Update" runat="server" ImageUrl="~/images/1-update.png" ToolTip="Update" Height="15px" Width="15px" />
<asp:ImageButton ID="imgbtnCancel" runat="server" CommandName="Cancel" ImageUrl="~/images/1-Cancel.png" ToolTip="Cancel" Height="15px" Width="15px" />
</EditItemTemplate>
<ItemTemplate>
<asp:ImageButton ID="imgbtnEdit" CommandName="Edit" runat="server" ImageUrl="~/images/1-edit.png" ToolTip="Edit" Height="15px" Width="15px" />
<asp:ImageButton ID="imgbtnDelete" CommandName="Delete" Text="Edit" runat="server" ImageUrl="~/Images/delete.png" ToolTip="Delete" Height="15px" Width="15px" />
</ItemTemplate>
<FooterTemplate>
<asp:ImageButton ID="imgbtnAdd" runat="server" ImageUrl="~/images/1-add.png" CommandArgument='<%# Eval("MasterID") %>' OnClientClick="return validateInputField(this);" CommandName="AddNew" Width="15px" Height="15px" ToolTip="Add new User" ValidationGroup="validaiton" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Vendor #">
<EditItemTemplate>
<asp:TextBox ID="txtVendorID" runat="server" Text='<%# Eval("VendorID") %>' />
</EditItemTemplate>
<ItemTemplate>
<div style="display: none">
<asp:Label ID="lblMasterID" runat="server" Text='<%# Eval("MasterID") %>' />
</div>
<%#Eval("VendorID") %>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtftrVendorID" CssClass="txtftrVendorID" runat="server" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle CssClass="custom-pagination" />
<PagerSettings Mode="NumericFirstLast" FirstPageText="Prev" LastPageText="Next" PageButtonCount="5" Position="Bottom" />
<FooterStyle CssClass="custom-footer" />
</asp:GridView>
</div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
这是我的cs文件代码。
string gvUniqueID = String.Empty;
int gvEditIndex = -1;
protected void grdViewMaster_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
if (e.CommandName.Equals("AddNewMaster"))
{
if (isChildAdded)
return;
TextBox txtStyleID = (TextBox)grdViewMaster.FooterRow.FindControl("txtftrStyleID");
string StyleID = txtStyleID.Text;
if (StyleID != string.Empty)
{
InsertMasterRecord(StyleID); /* My Database function which Insert values to database */;
BindMasterGrid();
}
}
}
catch (Exception ex)
{
Common.LogError(ex);
throw ex;
}
}
protected void grdViewMaster_RowEditing(Object sender, GridViewEditEventArgs e)
{
grdViewMaster.EditIndex = e.NewEditIndex;
BindMasterGrid();
}
// in that master row update, this works fine. no duplicate data here.
protected void grdViewMaster_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
try
{
int recordId = Convert.ToInt32(grdViewMaster.DataKeys[e.RowIndex].Value.ToString());
TextBox txtStyleID = (TextBox)grdViewMaster.Rows[e.RowIndex].FindControl("txtEditStyleID");
string StyleID = txtStyleID.Text;
if (StyleID != string.Empty)
{
UpdateMasterRecord(StyleID, recordId); /* My Database function which updates this values */;
grdViewMaster.EditIndex = -1;
BindMasterGrid();
}
}
catch (Exception ex)
{
throw ex;
}
}
protected void grdViewMaster_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
// my delete logic here which works fine.
}
protected void grdViewMaster_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
grdViewMaster.EditIndex = -1;
BindMasterGrid();
}
protected void grdViewMaster_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewRow row = e.Row;
string strSort = string.Empty;
if (row.DataItem == null)
{
return;
}
GridView gv = new GridView();
gv = (GridView)row.FindControl("gvExcelData"); // for binding the inner grid datas.
if (gv.UniqueID == gvUniqueID)
{
gv.PageIndex = gvNewPageIndex;
gv.EditIndex = gvEditIndex;
}
gv.DataSource = /* My Database function which returns DataTable*/;
gv.DataBind();
}
protected void gvExcelData_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
GridView gvExcelData = (GridView)sender;
if (e.CommandName.Equals("AddNew"))
{
Label lblMasterID = (Label)gvExcelData.NamingContainer.FindControl("lblMasterID");
int masterID = Convert.ToInt32(lblMasterID.Text);
TextBox txtVendorID = (TextBox)gvExcelData.FooterRow.FindControl("txtftrVendorID");
string vendorID = txtVendorID.Text;
if (masterID != 0)
{
insertToDatabase(masterID,vendorID); // my inserted database logic here..
BindMasterGrid();
}
}
}
catch (Exception ex)
{
throw ex;
}
}
protected void gvExcelData_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView gvTemp = (GridView)sender;
gvUniqueID = gvTemp.UniqueID;
gvEditIndex = e.NewEditIndex;
BindMasterGrid();
}
// here i got the duplicate values by comma separated while updating the values.
protected void gvExcelData_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
try
{
GridView gvExcelData = (GridView)sender;
GridViewRow gr = gvExcelData.Rows[e.RowIndex];
int recordId = Convert.ToInt32(gvExcelData.DataKeys[e.RowIndex].Value.ToString());
TextBox txtVendorID = (TextBox)gr.FindControl("txtVendorID");
string vendorID = txtVendorID.Text;
UpdateToDatabase(recordId,VendorID); // my method for updating the database values.
gvExcelData.EditIndex = -1;
BindMasterGrid();
}
catch (Exception ex)
{
throw ex;
}
}
protected void gvExcelData_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
// my delete methods for deleting the values which works superb.
}
protected void gvExcelData_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView gvExcelData = (GridView)sender;
gvExcelData.EditIndex = -1;
BindMasterGrid();
}
请帮我解决这个问题。
答案 0 :(得分:0)
由于您说您的插件工作正常,您的问题可能与您传递给更新方法的供应商ID有关。
插入:
string vendorID = txtVendorID.Text;
insertToDatabase(masterID,vendorID);
更新
string vendorID = txtVendorID.Text;
UpdateToDatabase(recordId,VendorID);