【发布时间】:2017-01-22 05:10:47
【问题描述】:
我用 SQL db 创建了 gridview,我创建了三个文本框“CountryID”、“Name”、“CountryNotes”,并在网页中添加按钮,当我添加详细信息时,它将添加到 db 并填充到网格视图。我完成了编辑和更新的编码,一切运行良好,但是在我单击更新按钮后,详细信息会更新到网格视图,但文本框不清晰,更新按钮应该更改为添加按钮。
我在这里添加我的 HTMl 代码:
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="script1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<table align="Center" style="width: 50%;">
<tr>
<td class="auto-style1">
<asp:Label ID="Label1" runat="server" Text="CountryID" ForeColor="#CC0000"></asp:Label>
</td>
<td>
<asp:TextBox ID="Text1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style1">
<asp:Label ID="Label2" runat="server" Text="Name" ForeColor="#CC0000"></asp:Label>
</td>
<td>
<asp:TextBox ID="Text2" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style1">
<asp:Label ID="Label3" runat="server" Text="CountryNotes" ForeColor="#CC0000"></asp:Label>
</td>
<td>
<asp:TextBox ID="Text3" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style1">
<asp:Button ID="Button1" runat="server" Text="Add" BackColor="#CC0000" ForeColor="White" ToolTip="Insert" OnClick="Button1_Click" />
</td>
<td>
<asp:Button ID="Button2" Visible="false" runat="server" BackColor="#CC0000" ForeColor="White" OnClick="Button2_Click" Text="Cancel" />
</td>
</tr>
</table>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CountryID" DataSourceID="SqlDataSource1" CellPadding="4" ForeColor="#333333" GridLines="None" OnRowCommand="Row_edit">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="CountryID" HeaderText="CountryID" ReadOnly="True" SortExpression="CountryID" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="CountryNotes" HeaderText="CountryNotes" SortExpression="CountryNotes" />
<asp:ButtonField ButtonType="Image" ImageUrl="~/Edit.png" CommandName="EditRow" />
<asp:ButtonField ButtonType="Image" ImageUrl="~/Delete.png" CommandName="DeleteRow" />
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConflictDetection="CompareAllValues" 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)" OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT CountryID, Name, CountryNotes FROM Country1" UpdateCommand="UPDATE [Country1] SET [Name] = @Name, [CountryNotes] = @CountryNotes WHERE [CountryID] = @original_CountryID AND [Name] = @original_Name AND [CountryNotes] = @original_CountryNotes">
<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 Country : 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)
{
if (Button1.ToolTip == "Insert")
{
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", Text1.Text);
cmd.Parameters.AddWithValue("@Name", Text2.Text);
cmd.Parameters.AddWithValue("@CountryNotes", Text3.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
else if (Button1.ToolTip == "Update")
{
int id = Convert.ToInt32(ViewState["CountryID"]);
update(id);
}
}
protected void Row_edit(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "EditRow")
{
int nRowIndex = Int32.Parse(e.CommandArgument.ToString());
Int32 nCountryID = Convert.ToInt32(GridView1.DataKeys[nRowIndex].Value);
GridViewRow row = GridView1.Rows[nRowIndex];
Text1.Text = row.Cells[0].Text;
Text2.Text = row.Cells[1].Text;
Text3.Text = row.Cells[2].Text;
Button1.ToolTip = "Update";
Button1.Text = "Update";
Button2.Visible = true;
int CountryID = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Values["CountryID"].ToString());
ViewState["CountryId"] = CountryID.ToString();
}
else if (e.CommandName == "DeleteRow")
{
int id = Int32.Parse(e.CommandArgument.ToString());
GridView1.DeleteRow(id);
}
}
public void update(int id)
{
con = new SqlConnection("Data Source=RYI-SYS-004;Initial Catalog=ATS;Integrated Security=True");
int CountryId = id;
string str = "update Country1 set Name='" + Text2.Text + "', CountryNotes='" + Text3.Text + "' where CountryID='" + Text1.Text + "'";
cmd = new SqlCommand(str, con);
cmd.Parameters.AddWithValue("@Name", Text2.Text);
cmd.Parameters.AddWithValue("@CountryNotes", Text3.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
GridView1.DataBind();
}
protected void Button2_Click(object sender, EventArgs e)
{
Button1.Text = "Add";
Button2.Visible = false;
Text1.Text = "";
Text2.Text = "";
Text3.Text = "";
}
【问题讨论】:
-
您可以尝试使用按钮的
CommandName属性而不是ToolTip。CommandName是标准。