1

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

我试过了,但控件中没有加载值,它显示为空

代码:

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

有任何想法吗?提前致谢

4

1 回答 1

-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];
   }
   }
于 2013-10-01T10:50:07.557 回答