Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 动态访问aspx.cs文件的用户控件_C#_Asp.net_.net_User Controls - Fatal编程技术网

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

C# 动态访问aspx.cs文件的用户控件,c#,asp.net,.net,user-controls,C#,Asp.net,.net,User Controls,我已经在ASP.NET中创建了一个动态控制脚本。现在,正如你在下面的照片中看到的 现在我想做的是从UserDontrol生成的内容,带下划线的值,我想将它们存储在数据库中。我创建了一个公共类,但在数据库中它存储为空值NOTNULL 以下是代码: .ascx.cs文件 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using Syst

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

现在我想做的是从UserDontrol生成的内容,带下划线的值,我想将它们存储在数据库中。我创建了一个公共类,但在数据库中它存储为空值NOTNULL

以下是代码:

.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\u单击时,如何将值存储在数据库中

另外,一位朋友告诉我,这个问题是由用户控件在我的页面加载事件中重新加载引起的,所以我得到的值总是空的。 如果我想获取值,他建议我在用户控件中添加一个事件,然后从事件中获取输入值。和e参考链接

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

您需要在.ascx.cs上定义属性,以返回所需的DropdOn、textbox和控件的值。。。 在.aspx.cs和click handler中,您可以简单地读取usercontrol属性,比如这个usercontrol\u ID.DropDownValue。。。DropDownValue是用户控件上的一个属性,如下所示:

public string DropDownValue
    {
        get
        {
            return DropDownList1.SelectedValue;
        }
    }
然后你们可以在数据库中保存你们想要的一切

此外,您还可以使用一个属性返回包含所有值的对象,如:

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.cs(如USERCONTROL_ID.Value.property1或USERCONTROL_ID.Value.property2)读取此信息,加载控件后获取控件的ID并将其添加到会话中如何

control o = LoadControl("~/UserControl/ContactDetails.ascx");
(Session["controlIDList"] as List<string>).Add(o.ID);
并从会话中保存的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 ....
});

不需要。我需要以编程方式访问这些值,而不是使用UserControlID。因为按Shto Artikull btnAddMore_,单击它将添加一个以上的用户控件。我想这一切都来自ViewState,但我不知道怎么做。我不知道你是否理解我。注意:额外使用viewstate会使你的页面沉重,因为viewstate会将其项目保存在页面上,如果你获得查看页面源,你可以查看viewstate,也可以将viewstate设置为false,在页面上,你不需要viewstate来使页面变轻我没有团队视图我向你发送了一封电子邮件
(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 ....
});