作为注册页面的一部分,用户可以添加他们的地址,如果他们没有得到地址EmptyItemTemplate
我的ListView
控制命令用户点击按钮在ListView
控件外添加一个如下所示的地址;插入查询添加到数据库,但插入后直到页面刷新后才显示在ListView中
<asp:Button ID="add_new_address" CssClass="blue" runat="server" Text="Add New Address" OnClick="add_new_address_Click" />
<div id="add_address_div" runat="server">
<asp:DropDownList ID="address_dropdown_insert" runat="server">
<asp:ListItem Value="Home">Home Address</asp:ListItem>
<asp:ListItem Value="Term">Term Time Address</asp:ListItem>
<asp:ListItem Value="Mail">Mail Address</asp:ListItem>
<asp:ListItem Value="Business">Business Address</asp:ListItem>
</asp:DropDownList><br />
Address 1:
<asp:TextBox Text="" runat="server" ID="address_1TextBox" /><br />
Address 2:
<asp:TextBox Text="" runat="server" ID="address_2TextBox" /><br />
Town/City:
<asp:TextBox Text="" runat="server" ID="town_cityTextBox" /><br />
County:
<asp:TextBox Text="" runat="server" ID="countyTextBox" /><br />
PostCode:
<asp:TextBox Text="" runat="server" ID="postcodeTextBox" /><br />
Country:
<asp:TextBox Text="" runat="server" ID="countryTextBox" />
<asp:Button runat="server" CommandName="Insert" Text="Insert" ID="InsertButton" OnClick="insert_address_button_Click" />
<asp:Button runat="server" CommandName="Cancel" Text="Clear" ID="Button4" OnClick="cancel_address_button_Click" /><br />
</div>
我然后在C#背后下面的代码从所述插入件形式的数据插入上述到数据库表;
protected void add_new_address_Click(object sender, EventArgs e)
{
add_address_div.Visible = true;
add_new_address.Visible = false;
}
protected void insert_address_button_Click(object sender, EventArgs e)
{
string userid = Session["user_id"].ToString();
string addtype = address_dropdown_insert.SelectedValue;
string add1 = address_1TextBox.Text;
string add2 = address_2TextBox.Text;
string town = town_cityTextBox.Text;
string county = countyTextBox.Text;
string pcode = postcodeTextBox.Text;
string country = countryTextBox.Text;
string ConnectionString = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection myConnection = new SqlConnection(ConnectionString);
myConnection.Open();
String query = "INSERT INTO address (user_id, address_type, address_1, address_2, town_city, county, postcode, country) VALUES (@user_id, @address_type, @address_1, @address_2, @town_city, @county, @postcode, @country)";
SqlCommand myCommand = new SqlCommand(query, myConnection);
myCommand.Parameters.AddWithValue("@user_id", userid);
myCommand.Parameters.AddWithValue("@address_type", addtype);
myCommand.Parameters.AddWithValue("@address_1", add1);
myCommand.Parameters.AddWithValue("@address_2", add2);
myCommand.Parameters.AddWithValue("@town_city", town);
myCommand.Parameters.AddWithValue("@county", county);
myCommand.Parameters.AddWithValue("@postcode", pcode);
myCommand.Parameters.AddWithValue("@country", country);
myCommand.ExecuteNonQuery();
myConnection.Close();
}
protected void cancel_address_button_Click(object sender, EventArgs e)
{
add_address_div.Visible = false;
}
这工作,在数据被插入到数据库表,因为它应该,但我点击insert_address_button
后,该数据不会在ListView
控制显示,它只显示EmptyItemTemplate
。但是,如果我要刷新页面,则会显示数据。
我也在View
控件中工作,所以正常的刷新将使用户回到注册过程的开始,并希望他们将被重定向回地址的View
控件。
在插入后缺少的过程中是否存在一个步骤,以便数据显示在列表视图中?任何帮助将不胜感激。
您首先使用什么代码加载ListView数据?您基本上需要再次查询数据库以加载新结果 – levelonehuman