访问动态用户控制到aspx.cs文件

时间:2014-02-11 15:20:43

标签: c# asp.net .net user-controls

我在ASP.NET中创建了一个动态控制脚本。现在如下图所示...

enter image description here

现在我要做的是从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(); 
    }
}

问题是......当我按下“Dergo”按钮(Button1_Click)时,如何将值存储在数据库中?

P.S。一位朋友告诉我,问题是由用户控件在我的Page_Load事件中重新加载,所以我得到的值总是为空。 如果我想获得该值,他建议我在我的用户控件中添加一个事件,然后从事件中获取我的输入值。并参考链接http://msdn.microsoft.com/en-us/library/fb3w5b53(v=vs.100).aspx

我接受了他的建议,但我在两周前开始学习ASP.NET,但我没有成功。任何人都可以帮我提供更多信息或编写代码吗? 谢谢。

2 个答案:

答案 0 :(得分:0)

您需要在.ascx.cs上定义属性以返回dropdwontextbox的值,并控制您需要其值... 在.aspx.csclick handler中,您可以简单地阅读usercontrol个属性,例如USERCONTROL_ID.DropDownValue ... DropDownValue是您的用户控件上的属性,如下所示:

public string DropDownValue
    {
        get
        {
            return DropDownList1.SelectedValue;
        }
    }

然后您可以在database

中保存所需的所有内容

你也可以有一个属性返回一个包含所有值的对象,如:

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;}
}

并从aspx.csUSERCONTROL_ID.Value.property1

USERCONTROL_ID.Value.property2阅读此内容

答案 1 :(得分:0)

如何在加载后获取控件的id并将其添加到会话

control o = LoadControl("~/UserControl/ContactDetails.ascx");
(Session["controlIDList"] as List<string>).Add(o.ID);

您之前需要创建会话,可能在page load使用此

Session.Add("controlIDList", new List<string>());

从会话中的已保存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 ....
});