繁体   English   中英

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

[英]Access Dynamically User Control to aspx.cs file

我已经在ASP.NET中创建了动态控制脚本。 现在,如您在下面的照片中看到的...

在此处输入图片说明

现在,我要做的是从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)时,如何在数据库中存储值?

PS一个朋友告诉我,问题是由用户控件遇到的情况已经在我的Page_Load事件中重新加载的,所以我得到的值始终为空。 而且,如果我想获取值,他建议我在用户控件中添加一个事件,然后从该事件中获取输入值。 和e链接http://msdn.microsoft.com/en-us/library/fb3w5b53(v=vs.100).aspx

我接受了他的建议,但是2周前我开始学习ASP.NET,但没有成功。 谁能帮我提供更多信息或写代码? 谢谢。

您需要在.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.property1USERCONTROL_ID.Value.property2

如何在加载后获取控件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 ....
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM