作爲註冊頁面的一部分,用戶可以添加他們的地址,如果他們沒有得到地址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