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

时间:2013-09-30 12:10:33

标签: c# asp.net user-controls

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

我试过但是值没有加载到显示为空的控件

代码:

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();
int y = 0;
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();

   y++;
   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

  <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="VisaUserControl.ascx.cs" Inherits="Portal.VisaUserControl" %>
  <%@ Register Assembly="BasicFrame.WebControls.BasicDatePicker" Namespace="BasicFrame.WebControls" TagPrefix="dp" %>
  <asp:ScriptManager ID="scriptmanager1" runat="server"></asp:ScriptManager>
  <div id="divreg" runat="server">
  <table id="tbl" runat="server">
  <tr>
  <td>
     <asp:Label ID="lbl2" runat="server"></asp:Label>
  </td>
  </tr>
  <tr>
  <td> Visa Number:</td>
  <td><asp:TextBox ID="txtUser" Width="160px" runat="server"/></td>
  <td> Country Name:</td>
  <td><asp:DropDownList ID="dropCountry" Width="165px" runat="server"> </asp:DropDownList>   </td>
  </tr>
  <tr>
  <td> Type of Visa:</td>
  <td><asp:DropDownList ID="dropVisa" Width="165px" runat="server" OnSelectedIndexChanged="dropVisa_SelectedIndexChanged"></asp:DropDownList></td>
  <td> Type of Entry:</td>
  <td><asp:DropDownList ID="dropEntry" Width="165px" runat="server"></asp:DropDownList>       </td>
  </tr>
  <tr>
  <td> Expiry Date</td>
  <td><asp:TextBox ID="txtDate" runat="server"></asp:TextBox>
  <ajaxToolkit:CalendarExtender ID="CalendarExtender1" runat="server" 
                  TargetControlID="txtDate" PopupButtonID="Imgbtnfromdate" Format="dd/MM/yyyy">
              </ajaxToolkit:CalendarExtender>
  </td>
  <td>
  <asp:Button ID="btnRemove" Text="Remove" runat="server" OnClick="btnRemove_Click" />
  </td>
  </tr>
  </table>
  </div>

.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 :(得分:-1)

尝试所有代码!IsPostback

   protected void Page_Load(object sender, EventArgs e)
   {
   if(!IsPostback)
   {
       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];
   }
   }