グリッドビューと次のコードをJavaScriptで使用して新しい行を追加します。問題は、新しい行を追加するためにボタンをクリックすると行が追加されますが、以前の行データも含まれていてDBに保存されないということです。エラー "無効な番号"が生成されます。グリッドJavaScriptを使用した行の追加
<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に保存する必要があります。
私はあなたがcloneNode(true)
をグリッド
<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>
私の質問に答えを与えることができます –