【发布时间】:2016-09-13 06:45:55
【问题描述】:
我有gridview 和gridview 以上我有两个文本框。当我填充文本框并单击添加按钮时,内容应动态添加到gridview和sql数据库中,当我单击gridview每一行中显示的编辑按钮时,我应该将值返回到上述文本框,添加应该更改为更新按钮,在我更新 gridview 后必须再次填充更新的数据。
这是我的 HTML 页面设计
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="script1" runat="server"></asp:ScriptManager>
<div>
<table align="center" style="width:50%;">
<tr>
<td class="auto-style1">
<asp:Label ID="Label1" runat="server" Text="Country Name" Font-Bold="True" ForeColor="Red"></asp:Label>
</td>
<td>
<asp:TextBox ID="Text1" runat="server" Width="180px"></asp:TextBox>
</td>
<td> </td>
</tr>
<tr>
<td class="auto-style1">
<asp:Label ID="Label2" runat="server" Text="Country Notes" Font-Bold="True" ForeColor="Red"></asp:Label>
</td>
<td>
<asp:TextBox ID="Text2" runat="server" Width="180px"></asp:TextBox>
</td>
<td> </td>
</tr>
<tr>
<td>
<br />
<asp:Button ID="Button1" runat="server" Text="Add" BackColor="#990000" ForeColor="White" OnClick="Button1_Click" />
</td>
<td> </td>
<td> </td>
</tr>
</table>
<br />
<br />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="GridView1" EventName="PageIndexChanging" />
</Triggers>
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataKeyNames="CountryID" DataSourceID="SqlDataSource1" OnRowEditing="cmd = new SqlCommand("insert into country1 (Name,CountryNotes) values(@Name, @CountryNotes)", con);" >
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowSelectButton="True" />
<asp:BoundField DataField="CountryID" HeaderText="CountryID" ReadOnly="True" SortExpression="CountryID" InsertVisible="False" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="CountryNotes" HeaderText="CountryNotes" SortExpression="CountryNotes" />
</Columns>
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ATSConnectionString %>" DeleteCommand="DELETE FROM [country1] WHERE [CountryID] = @original_CountryID AND (([Name] = @original_Name) OR ([Name] IS NULL AND @original_Name IS NULL)) AND (([CountryNotes] = @original_CountryNotes) OR ([CountryNotes] IS NULL AND @original_CountryNotes IS NULL))" InsertCommand="INSERT INTO [country1] ([Name], [CountryNotes]) VALUES (@Name, @CountryNotes)" SelectCommand="SELECT * FROM [country1]" UpdateCommand="UPDATE [country1] SET [Name] = @Name, [CountryNotes] = @CountryNotes WHERE [CountryID] = @original_CountryID AND (([Name] = @original_Name) OR ([Name] IS NULL AND @original_Name IS NULL)) AND (([CountryNotes] = @original_CountryNotes) OR ([CountryNotes] IS NULL AND @original_CountryNotes IS NULL))" ConflictDetection="CompareAllValues" OldValuesParameterFormatString="original_{0}">
<DeleteParameters>
<asp:Parameter Name="original_CountryID" Type="Int32" />
<asp:Parameter Name="original_Name" Type="String" />
<asp:Parameter Name="original_CountryNotes" Type="String" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="CountryNotes" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="CountryNotes" Type="String" />
<asp:Parameter Name="original_CountryID" Type="Int32" />
<asp:Parameter Name="original_Name" Type="String" />
<asp:Parameter Name="original_CountryNotes" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
【问题讨论】:
-
你的问题是?
-
我在 gridview 本身中提供了编辑选项,当我单击编辑选项时,gridview 内的行中的值本身就是在那里编辑的,但我希望这些值回到我用来添加的上面的文本框中,, ,并且在我修改内容后,我必须从那里更新