从gridview行获取数据已经从sql数据库检索,并将其传递到gridview外部的文本框
本文关键字:gridview 外部 文本 数据 获取 检索 数据库 sql | 更新日期: 2023-09-27 18:06:52
我在sql数据库中创建了名为国家的表,现在我在asp中创建了网站,我设计了网格视图,我通过网格视图从sql中获得了值,我有三个文本框"CountryID" "Name" "CountryNotes"和ADD按钮在网页中,当我填写文本框它将保存到db并在gridview中显示它在同一页面本身使用更新面板,现在我想编辑gridview中的表,所以当我点击编辑按钮在gridview的每一行,它应该传递选定的行值的文本框这是在页面的顶部,这意味着文本框是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();
}
}
}
这是我现有的输出
输入图片描述
有人能解决这个问题吗?Note - there are a few changes you;ll want to make to this code. The example I'm giving you is not meant to be optimized or generalized - it's designed to answer your specific question while changing as little of your original code as possible. There may be other errors.
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 = GridView1[ROW_NUM][COL_NUM].Text;
Text1.Text = GridView1[ROW_NUM][COL_NUM].Text;
Text2.Text = GridView1[ROW_NUM][COL_NUM].Text;
// GridView1.DataBind();
}
}
}
您需要为上面的每个ROW_NUM和COL_NUM输入适当的值才能使此工作,例如GridView1[0][0]或GridView1[searchRow][4],其中searchRow是int。