我有网格视图和以下代码在javascript中添加新行。问题是,当我单击按钮添加新行时,它会添加行,但它也包含前一行数据,不会保存到DB。它会生成错误“无效的数字”。
<script type='text/javascript' language='javascript'>
function AddNewRecord()
{
var grd = document.getElementById('GridView1');
var tbod=grd.rows[0].parentNode;
var newRow=grd.rows[grd.rows.length - 1].cloneNode(true);
tbod.appendChild(newRow);
return false;
}
</script>
<asp:Button ID="Button2" runat="server" Text="Add Row" OnClientClick="return AddNewRecord();"/>
我应该怎么做才能使该行生成为空,并且还应该保存到DB。
我有Grid的代码
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" OnRowCreated="myGridView_ItemCreated" Headerstyle-CssClass="myStyle"
BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" CellPadding="4" Width="859px">
<RowStyle BackColor="White" ForeColor="#003399" HorizontalAlign="Right" />
<Columns>
<asp:TemplateField HeaderText="Card No">
<ItemTemplate>
<asp:TextBox ID="txtCardNo" runat="server" Width="60px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Zone">
<ItemTemplate>
<asp:DropDownList ID="ddlZone" runat="server" DataSourceID="SqlDataSource2"
DataTextField="ZONE_DESC" DataValueField="ZONE_NO" Width="80px">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand='SELECT DISTINCT * FROM "MLK_00_05" order by Zone_DESC'>
</asp:SqlDataSource>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Vehicle No">
<ItemTemplate>
<asp:DropDownList ID="ddlVeh" runat="server" DataSourceID="SqlDataSource1" DataTextField="VEHICLE_NUMBER"
DataValueField="VEH_COD" Width="80px">
</asp:DropDownList><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand='SELECT DISTINCT VEHICLE_NUMBER, DEVICEID, VEH_COD FROM MLK_VEHICLE ORDER BY 1'>
</asp:SqlDataSource>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date">
<ItemTemplate>
<asp:DropDownList ID="ddlStartDate" runat="server" Width="80px" AppendDataBoundItems="True">
</asp:DropDownList>
</ItemTemplate>
<ItemStyle Wrap="False" />
</asp:TemplateField>
<asp:TemplateField HeaderText="HH MM">
<ItemTemplate>
<asp:DropDownList ID="ddlStartHH" runat="server" Width="50px">
</asp:DropDownList> <asp:DropDownList ID="ddlStartMM" runat="server" Width="50px">
</asp:DropDownList>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="800px" Wrap="False" />
<HeaderTemplate>
HH MM
</HeaderTemplate>
<HeaderStyle Wrap="False" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Date">
<ItemTemplate>
<asp:DropDownList ID="ddlEntryDate" runat="server" Width="80px">
</asp:DropDownList>
</ItemTemplate>
<ItemStyle Wrap="False" />
</asp:TemplateField>
<asp:TemplateField HeaderText="hh:mm">
<ItemTemplate>
<asp:DropDownList ID="ddlEntryHH" runat="server" Width="50px">
</asp:DropDownList>
<asp:DropDownList ID="ddlEntryMM" runat="server" Width="50px">
</asp:DropDownList>
</ItemTemplate>
<ItemStyle Wrap="False" />
<HeaderTemplate>
HH MM
</HeaderTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date">
<ItemTemplate>
<asp:DropDownList ID="ddlGrossDate" runat="server" Width="80px">
</asp:DropDownList>
</ItemTemplate>
<ItemStyle Wrap="False" />
</asp:TemplateField>
<asp:TemplateField HeaderText="hh:mm">
<ItemTemplate>
<asp:DropDownList ID="ddlGrossHH" runat="server" Width="50px">
</asp:DropDownList>
<asp:DropDownList ID="ddlGrossMM" runat="server" Width="50px">
</asp:DropDownList>
</ItemTemplate>
<ItemStyle Wrap="False" />
<HeaderTemplate>
HH MM
</HeaderTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Gross Weight(kg)">
<ItemTemplate>
<asp:TextBox ID="txtGrossWt" runat="server" Width="60px"></asp:TextBox>
</ItemTemplate>
<ItemStyle HorizontalAlign="Right" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Tare Weight(kg)">
<ItemTemplate>
<asp:TextBox ID="txtTareWt" runat="server" Width="60px" ></asp:TextBox>
</ItemTemplate>
<ItemStyle HorizontalAlign="Right" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Net Weight(kg)">
<ItemTemplate>
<asp:TextBox ID="txtNetWt" runat="server" Width="60px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Rate">
<ItemTemplate>
<asp:TextBox ID="txtRate" runat="server" Width="60px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date">
<ItemTemplate>
<asp:DropDownList ID="DdlTareDate" runat="server" Width="80px" >
</asp:DropDownList>
</ItemTemplate>
<ItemStyle Wrap="False" />
</asp:TemplateField>
<asp:TemplateField HeaderText="hh:mm">
<ItemTemplate>
<asp:DropDownList ID="ddlTareHH" runat="server" Width="50px">
</asp:DropDownList>
<asp:DropDownList ID="ddlTareMM" runat="server" Width="50px">
</asp:DropDownList>
</ItemTemplate>
<ItemStyle Wrap="False" />
<HeaderTemplate>
HH MM
</HeaderTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="FAT(%)">
<ItemTemplate>
<asp:TextBox ID="txtFat" runat="server" Width="50px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="LR">
<ItemTemplate>
<asp:TextBox ID="txtLR" runat="server" Width="50px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="SNF(%)">
<ItemTemplate>
<asp:TextBox ID="txtSNF" runat="server" Width="50px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Protein(%)">
<ItemTemplate>
<asp:TextBox ID="txtProtein" runat="server" Width="50px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="MBRT">
<ItemTemplate>
<asp:TextBox ID="txtMBRT" runat="server" Width="50px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Cheese">
<ItemTemplate>
<asp:TextBox ID="txtCheese" runat="server" Width="50px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Distance">
<ItemTemplate>
<asp:TextBox ID="txtDistance" runat="server" Width="60px"></asp:TextBox>
</ItemTemplate>
<ItemStyle HorizontalAlign="Right" />
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
<PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
<SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
<HeaderStyle BackColor="CornflowerBlue" Font-Bold="False" ForeColor="White" Font-Names="Arial" Font-Size="9pt" CssClass="myStyle" />
</asp:GridView>
答案 0 :(得分:2)
因为你正在做cloneNode(true)
,它只会克隆前一行,因此与前一行有相同的数据。要使其为空,请使用行中每个元素的.text("")
,因为您没有包含html我无法编写代码。
答案 1 :(得分:0)
对于你的场景,你通过javasscript在gridview中创建一个新行,但是新行不是服务器端控件,所以当回发时,控制状态无法保存在viewstate中,这就是你的原因所在无法保存到DB。
所以我认为你可以在页面中添加一个asp:hidden字段,合并新行(所有列)的数据并存储在其中,当你点击一个按钮时,它应该触发一个回发事件,你可以得到数据通过此隐藏字段的值的新行。然后将它们保存到DB。