asp.net - 如何在用户控件中加载数据库中的值?

时间:2013-09-30 13:10:50

标签: c# asp.net user-controls

我想在用户控件中加载数据库中的值

我可以添加更多用户控件,但我无法在用户控件中加载值

代码:

Add.aspx.cs

下面的代码用于添加多个用户控件并保留以前的控件值

    public List<string> NoOfControls
    {
        get
        {
            return ViewState["NoOfControls"] == null ? new List<string>() : (List<string>)ViewState["NoOfControls"];

        }
        set
        {
            ViewState["NoOfControls"] = value;
        }

    }


    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);

        GenerateControls();
    }



    private void GenerateControls()
    {
        foreach (string i in NoOfControls)
        {
            VisaUserControl ctrl = (VisaUserControl)Page.LoadControl("VisaUserControl.ascx");

            ctrl.ID = i;
            this.rpt1.Controls.Add(ctrl);
        }
    }

    protected void btnAddVisa_Click(object sender, EventArgs e)
    {

        List<string> temp = null;
        var uc = (VisaUserControl)this.LoadControl(@"VisaUserControl.ascx");

        string id = Guid.NewGuid().ToString();
        uc.ID = id;

        temp = NoOfControls;
        temp.Add(id);
        NoOfControls = temp;
        rpt1.Controls.Add(uc);
    }

Edit.aspx.cs

下面的代码用于从用户控件中的数据库加载值

using (OleDbCommand cmd = new OleDbCommand("Select * from visa_details where emp_id = '"+ empid +"'", DbConnection))
using (OleDbDataAdapter da = new OleDbDataAdapter(cmd))
{

   OleDbDataReader DR1 = cmd.ExecuteReader();
   while(DR1.Read())
   {
     //Here I can get values
      string visaNumb = DR1[2].ToString();
      string visaCountry = DR1[3].ToString();
      string visaType = DR1[4].ToString();
      string visaEntry = DR1[5].ToString();
      string expiryDate = DR1[6].ToString();

      for (int i = 0; i < y; i++)
      {
       VisaUserControl userconrol = (VisaUserControl)Page.LoadControl("VisaUserControl.ascx");
       userconrol.TextVisaNumber = visaNumb;
       userconrol.VisaCountry = visaCountry;
       userconrol.VisaType = visaType;
       userconrol.VisaEntry = visaEntry;
       userconrol.ExpiryDate = expiryDate;

      repeater1.Controls.Add(userconrol);
      }
     }
    }  

.ascx.cs

此处有值,但在加载时,所有值都被删除

protected void Page_Load(object sender, EventArgs e)
{
 txtUser.Text = Request.Form[txtUser.UniqueID];
 dropCountry.SelectedValue = Request.Form[dropCountry.UniqueID];
 dropVisa.SelectedValue = Request.Form[dropVisa.UniqueID];
 dropEntry.SelectedValue = Request.Form[dropEntry.UniqueID];
 txtDate.Text = Request.Form[txtDate.UniqueID];
}

public string TextVisaNumber
{
  get { return txtUser.Text; }
  set { txtUser.Text = value; }
}

public string VisaCountry
{
 get { return dropCountry.SelectedValue; }
 set { dropCountry.SelectedValue = value; }
}

public string VisaType
{
 get { return dropVisa.SelectedValue; }
 set { dropVisa.SelectedValue = value; }
}

public string VisaEntry
{
 get { return dropEntry.SelectedValue; }
 set { dropEntry.SelectedValue = value; }
}

public string ExpiryDate
{
  get
  {
    return txtDate.Text;
  }
  set
  {
    txtDate.Text = value;
  }
}

有什么想法吗?提前致谢

1 个答案:

答案 0 :(得分:0)

您应该在一些现有控件的内部HTML中保留一组数据,而不是在页面的控件集合中添加控件。