我在ASP.NET中創建了一個動態控制腳本。現在,當你在下面看到的照片...訪問aspx.cs文件的動態用戶控制
現在我想要做的是,從UserDontrol產生的內容,有下劃線的值,我想將它們存儲在數據庫中。我創建了一個公共類,但在數據庫中它被存儲爲空值(不是NULL)。
這些代碼:
.ascx.cs文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
public partial class UserControl_ContactDetails : System.Web.UI.UserControl
{
public String TextProperty
{
get
{
return LabelCmimi.Text;
}
set
{
LabelCmimi.Text = value;
}
}
public void Page_Load(object sender, EventArgs e)
{
//if(Page.IsPostBack)
//{
DropDownListArtikulli.Text = Request.Form[DropDownListArtikulli.UniqueID];
LabelCmimi.Text = Request.Form[LabelCmimi.UniqueID];
TextBoxSasia.Text = Request.Form[TextBoxSasia.UniqueID];
LabelVlera.Text = Request.Form[LabelVlera.UniqueID];
//}
Cmimi();
Vlera();
Session["Artikulli"] = DropDownListArtikulli.SelectedItem.ToString();
Session["Cmimi"] = LabelCmimi.Text;
Session["Sasia"] = TextBoxSasia.Text;
Session["Vlera"] = LabelVlera.Text;
}
public void Cmimi()
{
DataTable listaArtikujt = new DataTable();
using (SqlConnection lidhje = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString))
{
try
{
SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM [Artikujt]", lidhje);
adapter.Fill(listaArtikujt);
DropDownListArtikulli.DataSource = listaArtikujt;
DropDownListArtikulli.DataTextField = "Artikulli";
DropDownListArtikulli.DataValueField = "Cmimi";
DropDownListArtikulli.DataBind();
LabelCmimi.Text = DropDownListArtikulli.SelectedValue.ToString();
}
catch (Exception ex)
{
Response.Write("Gabim:" + ex.ToString());
}
}
}
public void Vlera()
{
if(!string.IsNullOrEmpty(TextBoxSasia.Text))
{
LabelVlera.Text = TextBoxSasia.Text.ToString();
int a = Convert.ToInt32(LabelCmimi.Text);
int b = Convert.ToInt32(LabelVlera.Text);
int c = a * b;
LabelVlera.Text = c.ToString();
Session["Totali"] = (Session["Totali"] == null) ? 0 : Convert.ToInt32(Session["Totali"].ToString()) + c;
}
}
}
.aspx.cs文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
public partial class DynamicControl : System.Web.UI.Page
{
private const string VIEWSTATEKEY = "ContactCount";
protected void Page_Load(object sender, EventArgs e)
{
//Set the number of default controls
ViewState[VIEWSTATEKEY] = ViewState[VIEWSTATEKEY] == null ? 1 : ViewState[VIEWSTATEKEY];
Session["Totali"] = 0;
//Load the contact control based on Vewstate key
LoadContactControls();
}
private void LoadContactControls()
{
for (int i = 0; i < int.Parse(ViewState[VIEWSTATEKEY].ToString()); i++)
{
phContactDetails.Controls.Add(LoadControl("~/UserControl/ContactDetails.ascx"));
}
}
protected void btnAddMore_Click(object sender, EventArgs e)
{
ViewState[VIEWSTATEKEY] = int.Parse(ViewState[VIEWSTATEKEY].ToString()) + 1;
LoadContactControls();
}
protected void Button1_Click(object sender, EventArgs e)
{
}
protected void OK_Click(object sender, EventArgs e)
{
LabelTotal.Text = Session["Totali"].ToString();
}
}
的問題是...我怎麼能存儲的值數據庫,當我按下按鈕「德戈」(Button1_Click)?
P.S.一位朋友告訴我,問題是由用戶控件引起的,我的Page_Load事件中有重新加載,所以我得到的值始終是空的。 如果我想獲得價值,他建議我在我的用戶控件中添加一個事件,然後從事件中獲取我的輸入值。和e參考鏈接http://msdn.microsoft.com/en-us/library/fb3w5b53(v=vs.100).aspx
我聽取了他的建議,但我在2周前開始學習ASP.NET,但沒有成功。任何人都可以幫助我獲得更多信息或請寫下代碼嗎? 謝謝。
通知:額外使用'viewstate'讓你的頁面重,becuz,'viewstate'保存網頁上的項目,如果你'視圖頁面source'可以看到'viewstate',也可以設置'viewstate'爲false,在你不需要'viewstate'的頁面上讓你的頁面變亮 –