[英]Access Dynamically User Control to aspx.cs file
I have created a dynamic control script in ASP.NET. 我已经在ASP.NET中创建了动态控制脚本。 Now as you see in the photo below... 现在,如您在下面的照片中看到的...
Now what I want to do is the contents that are generated from UserDontrol, the values that are underlined I want to store them in database. 现在,我要做的是从UserDontrol生成的内容,加下划线的值我想将它们存储在数据库中。 I create a public class but in database it is stored as empty value (not NULL). 我创建了一个公共类,但在数据库中将其存储为空值(非NULL)。
These are the codes: 这些是代码:
.ascx.cs file .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 file .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();
}
}
The question is... how can I store the values in database when I press the button "Dergo" (Button1_Click)? 问题是...按下按钮“ Dergo”(Button1_Click)时,如何在数据库中存储值?
PS A friend told me that the problem is cased by the user control have reload in my Page_Load event, so I get the value is always empty. PS一个朋友告诉我,问题是由用户控件遇到的情况已经在我的Page_Load事件中重新加载的,所以我得到的值始终为空。 And if I want to get the value, he suggested me to add a event in my user control, and then get my input value from the event. 而且,如果我想获取值,他建议我在用户控件中添加一个事件,然后从该事件中获取输入值。 And e refer link http://msdn.microsoft.com/en-us/library/fb3w5b53(v=vs.100).aspx 和e链接http://msdn.microsoft.com/en-us/library/fb3w5b53(v=vs.100).aspx
I took his advice but I started learning ASP.NET 2 weeks ago and I didn't succeed. 我接受了他的建议,但是2周前我开始学习ASP.NET,但没有成功。 Can anyone help me with more info or write the code please? 谁能帮我提供更多信息或写代码? Thank You. 谢谢。
you need to define property on .ascx.cs
to return value of the dropdwon
, textbox
, and controls you need their value... and in .aspx.cs
and in click handler
you can read usercontrol
properties simply, like this USERCONTROL_ID.DropDownValue
... DropDownValue
is a property on your user control like this: 您需要在.ascx.cs
上定义属性以返回dropdwon
, textbox
和控件所需的值...并且在.aspx.cs
和click handler
您可以轻松读取usercontrol
属性,例如USERCONTROL_ID.DropDownValue
... DropDownValue
是用户控件上的一个属性,如下所示:
public string DropDownValue
{
get
{
return DropDownList1.SelectedValue;
}
}
and then you can save everything you want in database
然后您可以将所需的所有内容保存在database
also you can have one property which return a object containing all values like: 您还可以拥有一个属性,该属性返回包含所有值的对象,例如:
public UserControlValue Value
{
get
{
return new UserControlValue(DropDownList1.SelectedValue, TextBox1.Text);
}
}
public class UserControlValue
{
public UserControlValue(string property1, string property2)
{
this.property1 = property1;
this.property2 = property2;
}
public string property1 {get; set;}
public string property2 {get; set;}
}
and read this from aspx.cs
like USERCONTROL_ID.Value.property1
or USERCONTROL_ID.Value.property2
从阅读本aspx.cs
像USERCONTROL_ID.Value.property1
或USERCONTROL_ID.Value.property2
how about get id of controls after loading and add them to session, 如何在加载后获取控件ID并将其添加到会话中,
control o = LoadControl("~/UserControl/ContactDetails.ascx");
(Session["controlIDList"] as List<string>).Add(o.ID);
you need to create session before, maybe in page load
using this 您需要在此之前创建会话,也许在page load
中
Session.Add("controlIDList", new List<string>());
and find controls from saved id list in session, and then cast to its type, and get the value you want... 然后从会话中保存的ID列表中找到控件,然后将其强制转换为其类型,并获取所需的值...
(Session["controlIDList"] as List<string>).ForEach(u =>
{
var c = (TYPE_OF_USERCONTROL)this.Page.FindControl(u);
c.Value; // value is a property on user controls which return value of dropdown, textbox, and ....
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.