1

我绑定用户控件下拉列表中的值

但是当我添加用户控件行时,时间值未绑定在下拉列表中

代码:

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

    }

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

在此处输入图像描述

有任何想法吗?提前致谢。

4

1 回答 1

1

通过您提供的 id 找到用户控件,然后您可以找到用户控件的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"));
    }
}
于 2013-10-23T08:29:17.413 回答