如何绑定usercontrol下拉列表中的值?

时间:2013-10-23 08:19:11

标签: c# asp.net drop-down-menu user-controls loadviewstate

我绑定usercontrol下拉列表中的值

但是当我添加usercontrol行时,时间值没有在下拉列表中绑定

代码:

的.ascx

   <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TimesheetUserControl.ascx.cs" Inherits="Portal.TimesheetUserControl" %>
   <table>
   <tr>
   <td>
        <asp:DropDownList ID="DropDownActivities" Width="150px" runat="server"></asp:DropDownList>
    </td>
    <td>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </td>
    <td>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    </td>
    <td>
        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
    </td>
    <td>
        <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
    </td>
    <td>
        <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
    </td>
    <td>
        <asp:TextBox ID="TextBox7" runat="server"></asp:TextBox>
    </td>
</tr>

的.aspx

 <uc:Timesheet ID="Timesheet" runat="server" />
    <asp:Repeater ID="rpt1" runat="server">
    <ItemTemplate> 
    </ItemTemplate>
 </asp:Repeater>

 <asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" OnClick="ButtonAdd_Click"/>

.aspx.cs

    protected void Page_Load(object sender, EventArgs e)
    {
      if(!IsPostback)
      {
       BindActivities();
      }
    }

    protected void BindActivities()
    {
        DropDownList dropActivities = Timesheet.FindControl("DropDownActivities") as DropDownList;
        DbConnection.Open();
        OleDbCommand cmd1 = new OleDbCommand("select designation from emp_master where username = '" + username + "'", DbConnection);
        OleDbDataAdapter da = new OleDbDataAdapter(Deptcmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            // DbConnection.Close();
            dropActivities.DataSource = ds;
            dropActivities.DataTextField = "ActivityName";
            dropActivities.DataBind();
            dropActivities.Items.Insert(0, new ListItem("--Select--", "0"));
   }

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

        if (IsPostBack)
        {
            GenerateControls();

        }

    }

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

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

    }

    protected void ButtonAdd_Click(object sender, EventArgs e)
    {
        Button thisButton = (Button)sender;
        List<string> temp = null;
        var uc = (TimesheetUserControl)this.LoadControl(@"TimesheetUserControl.ascx");

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

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

    }

在下图中,如果添加了单击添加按钮行,但第二个值未在下拉列表中绑定

enter image description here

有什么想法吗?提前谢谢。

1 个答案:

答案 0 :(得分:1)

通过您给出的ID找到用户控件,然后您可以在usercontrol中找到DropDownList

protected void BindActivities()
{
    foreach (string controlName in NoOfControls)
    {
        TimesheetUserControl  userControl = Timesheet.FindControl(controlName) as TimesheetUserControl;
        if(userControl == null) return;
        DropDownList dropActivities = userControl.FindControl("DropDownActivities") as DropDownList;
        if(dropActivities == null) return;
        DbConnection.Open();
        OleDbCommand cmd1 = new OleDbCommand("select designation from emp_master where username = '" + username + "'", DbConnection);
        OleDbDataAdapter da = new OleDbDataAdapter(Deptcmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        // DbConnection.Close();
        dropActivities.DataSource = ds;
        dropActivities.DataTextField = "ActivityName";
        dropActivities.DataBind();
        dropActivities.Items.Insert(0, new ListItem("--Select--", "0"));
    }
}