按钮单击动态创建控件

时间:2018-01-08 06:26:01

标签: c# asp.net .net c#-4.0 dynamic-controls

我正在尝试在按钮点击上创建动态控件。

HTML:

<asp:LinkButton ID="lnkSrt" runat="server" Text="Multi Sort" ForeColor="Black" Font-Names="Calibri" Font-Size="10pt" />
<ajax:ModalPopupExtender runat="server" ID="mpeSrt"
 TargetControlID="lnkSrt" PopupControlID="pnlSrt" CancelControlID="btnClose">
 </ajax:ModalPopupExtender>
     <asp:UpdatePanel ID="upPanel" runat="server">
         <ContentTemplate>
              <asp:Panel ID="pnlSort" runat="server" align="center" Style="display: none" BackColor="LightGray">
              <asp:Label runat="server" Text="Sort By Column" />
              <asp:DropDownList ID="ddlColumnSort" runat="server" AutoPostBack="true" />
              <asp:Label runat="server" Text="Order By" />
              <asp:DropDownList ID="ddlOrder" runat="server" AutoPostBack="true">
              <asp:ListItem Text="Ascending" Value="0"></asp:ListItem>
              <asp:ListItem Text="Descending" Value="1"></asp:ListItem>
              </asp:DropDownList>
              <asp:LinkButton ID="lnkAdd" runat="server" Text="Add Sorting" Font-Underline="true" OnClick="lnkAddOrder_Click" Font-Names="Calibri" Font-Size="10pt" ForeColor="Black" />
              <asp:Button ID="btnSorting" runat="server" Text="Sort" OnClick="btnSorting_Click" />
              <asp:Button ID="btnClose" runat="server" Text="Close" />
            </asp:Panel>
         </ContentTemplate>
      </asp:UpdatePanel>

CODE:

点击AddSorting时我试图使用下面的代码创建下拉控件,而控件不会显示。

protected void lnkAddSort_Click(object sender, EventArgs e)
{
    int index = pnlSort.Controls.OfType<DropDownList>().ToList().Count + 1;
    this.Createddl("ddldyn" + index);
}

public void Createddl(string id)
{
    DropDownList ddl = new DropDownList();
    ddl.ID = id;
    pnlSort.Controls.Add(ddl);
    Literal lt = new Literal();
    lt.Text = "<br />";
    pnlSort.Controls.Add(lt);
}

图像:

enter image description here

2 个答案:

答案 0 :(得分:2)

pnlSrt设置为display: none,因此动态添加的子控件也不会显示。

答案 1 :(得分:0)

根据@Yoshi的回答。您可以使用public function get_date_wise_agent_report($start_date, $end_date, $agent = NULL, $abc = NULL) { $this->db->where("DATE(date) BETWEEN '{$start_date}' AND '{$end_date}'") $query = $this->db->get('datas'); $results1 = $query->result_array(); $this->db->where("DATE(date) BETWEEN '{$start_date}' AND '{$end_date}'") $query = $this->db->get('abc_datas'); $results2 = $query->result_array(); //Here you need to use $query->result_array() instead of $query->result() because result() returns a object. //Go to following link to learn more about result //Here I assumed both `datas` and `abc_datas` tables are having `date` column. $data = array_merge($results1,$results2); return $data; }

true控件设置可见的pnlSrt

试试这个

pnlSort.Style["display"] = "block";// or pnlSort.Attributes.Add("style", "display:block");