【发布时间】:2016-09-13 11:57:31
【问题描述】:
我在 sql db 中有名为 Country 的表,现在我在 asp 中创建了网站,并为网格视图设计,我通过网格视图从 sql 获取值,我有三个文本框“CountryID”“Name”“CountryNotes”和 ADD网页中的按钮,当我填充文本框时,它将保存到数据库并使用更新面板在同一页面本身的gridview中显示它,现在我想在gridview中编辑表格,所以当我单击gridview每一行中的编辑按钮时应该将选定的行值传递给页面顶部的 texboxes,这意味着文本框在 gridview 之外。我需要通过单击更新按钮来编辑内容并更新数据,这意味着添加按钮应该更改为更新按钮
这是我的 HTML 页面
<form id="form1" runat="server">
<asp:ScriptManager ID="script1" runat="server"></asp:ScriptManager>
<div>
<table align="center" style="width:50%;">
<tr>
<td class="auto-style6">
<asp:Label ID="Label3" runat="server" Text="Country ID" Font-Bold="True" ForeColor="Red"></asp:Label>
</td>
<td class="auto-style7">
<asp:TextBox ID="Text0" runat="server" Width="138px" ></asp:TextBox>
</td>
<td class="auto-style8"></td>
<td class="auto-style9">
</td>
</tr>
<tr>
<td class="auto-style1">
<asp:Label ID="Label1" runat="server" Text="Country Name" Font-Bold="True" ForeColor="Red"></asp:Label>
</td>
<td class="auto-style4">
<asp:TextBox ID="Text1" runat="server" Width="137px"></asp:TextBox>
</td>
<td> </td>
<td class="auto-style5">
</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 class="auto-style4">
<asp:TextBox ID="Text2" runat="server" Width="136px"></asp:TextBox>
</td>
<td> </td>
<td class="auto-style5">
</td>
</tr>
<tr>
<td>
<br />
<asp:Button ID="Button1" runat="server" Text="Add" BackColor="#990000" ForeColor="White" OnClick="Button1_Click" />
</td>
<td >
<br />
</td>
<td> </td>
<td class="auto-style5"> </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="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
<Columns>
<asp:BoundField DataField="CountryID" HeaderText="CountryID" InsertVisible="False" ReadOnly="True" SortExpression="CountryID" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="CountryNotes" HeaderText="CountryNotes" SortExpression="CountryNotes" />
<asp:ButtonField ButtonType="Button" CommandName="EditRow" />
<asp:CommandField ShowDeleteButton="True" ShowEditButton="true"/>
</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 AND [CountryNotes] = @original_CountryNotes" InsertCommand="INSERT INTO [Country1] ([CountryID], [Name], [CountryNotes]) VALUES (@CountryID, @Name, @CountryNotes)" SelectCommand="SELECT * FROM [Country1]" UpdateCommand="UPDATE [Country1] SET [Name] = @Name, [CountryNotes] = @CountryNotes WHERE [CountryID] = @original_CountryID AND [Name] = @original_Name AND [CountryNotes] = @original_CountryNotes" 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="CountryID" Type="Int32" />
<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>
这是我的 C# 代码
public partial class atc : System.Web.UI.Page
{
SqlConnection con;
SqlCommand cmd;
public void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
con = new SqlConnection("Data Source=RYI-SYS-004;Initial Catalog=ATS;Integrated Security=True");
cmd = new SqlCommand("insert into Country1 (CountryID,Name,CountryNotes) values(@CountryID,@Name, @CountryNotes)", con);
cmd.Parameters.AddWithValue("@CountryID", Text0.Text);
cmd.Parameters.AddWithValue("@Name", Text1.Text);
cmd.Parameters.AddWithValue("@CountryNotes", Text2.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
if (IsPostBack)
{
Text0.Text = "";
Text1.Text = "";
Text2.Text = "";
GridView1.DataBind();
}
}
}
这是我现有的输出
谁能解决这个问题!!
【问题讨论】:
-
Text0.Text = GridView1.Rows[ROW_NUM][COL_NUM],Text;请注意,行和列都为零,顺便说一句,所以第一个是 0,第二个是 1,等等。
-
对不起,我不知道在哪里插入您评论的那些编码,实际上我还没有尝试编辑事件代码,所以如果知道它可以正确指定它@Shannon Holsinger
标签: c# asp.net sql-server gridview