×

Loading...
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务

I had a piece of code using similar techonolgy, but it doesn't work. Instead of the linkbutton, i created dropdownlist dynamicly. Can you help me to take a look why my code doesn't work.

本文发表在 rolia.net 枫下论坛for (int i = 0; i < ElementCount - 1; i++)
{
TableRow tr1 = new TableRow();

if (i < objProfiles.Profiles.get_Item(ref tempGlProfile).Elements.Count-1)
{
_Element = objProfiles.Profiles.get_Item(ref tempGlProfile).Elements.get_Item(ref tmpItem).Value;

Label label1 = new Label();
DropDownList DDL1 = new DropDownList();
ListBox ListBox1 = new ListBox();

label1.Text = _Element.Substring(6, _Element.Length - 6).Trim();
label1.Width = 100;

DDL1.Width = 300;
DDL1.Items.Add("Match Any Value");
DDL1.SelectedIndexChanged += new EventHandler(DDL1_SelectedIndexChanged);
Level = "0"+i;

DI.GetDictionaryLevelList(_YearPeriod.Substring(0, 4), 1 ,Level, Gl_Sec, Gl_Exclude,ref rsDI, ref _ErrorMsg);
if (!_ErrorMsg.Equals(""))
{

//put the error message into a label
}
else
{
if (rsDI.RecordCount != 0)
{
rsDI.MoveFirst();
while (rsDI.EOF == false)
{
DDL1.Items.Add(rsDI.Fields["DI_ACCT"].Value + " " + rsDI.Fields["DI_Desc"].Value);
rsDI.MoveNext();
}
}
DDL1.Text = DDL1.Items[0].Text;

}

TableCell tc1 = new TableCell();
tc1.Controls.Add(label1 );
tc1.Width = 150;
tr1.Cells.Add(tc1);

tc1 = new TableCell();
tc1.Controls.Add(DDL1);
tc1.Width = 300;
tr1.Cells.Add (tc1);
}
}

private void DDL1_SelectedIndexChanged(object sender, EventArgs e)
{
string SelectedElement = DDL1.SelectedItem.ToString ();
if (SelectedElement.Equals("Match Any Value"))
{
MyDynamicString += SelectedElement.Substring(0, 2);
}
}



The DDL1_SelectedIndexChanged never triggered. I don't know why.更多精彩文章及讨论,请光临枫下论坛 rolia.net
Report

Replies, comments and Discussions:

  • 工作学习 / 专业技术讨论 / How to dynamic add some controls in .NET using C#?
    I tried using placeholder. The controls did dynamic added.However, it added side by side. I want to display them one under another. How can I do it? Anyone know?Thanks in advance.
    • 使用PlaceHolder 和 Panel都行, Panel1.Controls.Add(new Label());
      想控制位置首先声明一下Label

      Label lb1 = new Label();
      lb1.Text="this is my lable";
      lb1.Style = ...... ;

      Panel1.Controls.Add(lb1);
      • Thanks! I used PlaceHolder combined with Tablerow and TableCell so I can add controls in the way I like to display.
    • 这是我的一段C#程序,供你参考。
      public void GeneratePaginationBar()
      {
      this.pnlPageBar.Controls.Clear();
      for(int i=1;i<6;i++)
      {
      System.Web.UI.WebControls.LinkButton lbtPage = new LinkButton();
      lbtPage.ID=i.ToString();
      lbtPage.CommandName="Page";
      lbtPage.CommandArgument=i.ToString();
      lbtPage.Text=i.ToString();
      lbtPage.Enabled=true;
      lbtPage.Command += new System.Web.UI.WebControls.CommandEventHandler(this.lbtPage_Click);
      this.pnlPageBar.Controls.Add(lbtPage);
      this.pnlPageBar.Controls.Add(new LiteralControl("<br>"));
      }
      }
      private void lbtPage_Click(object sender, System.Web.UI.WebControls.CommandEventArgs e)
      {

      // BindData();
      }
      • Thanks! By the way, how can i trigger an event on a dynamic generated control or get a value from it.
        • there is a set of link buttons on my program, their click event is ..
          private void lbtPage_Click(object sender, ystem.Web.UI.WebControls.CommandEventArgs e){}
          • I had a piece of code using similar techonolgy, but it doesn't work. Instead of the linkbutton, i created dropdownlist dynamicly. Can you help me to take a look why my code doesn't work.
            本文发表在 rolia.net 枫下论坛for (int i = 0; i < ElementCount - 1; i++)
            {
            TableRow tr1 = new TableRow();

            if (i < objProfiles.Profiles.get_Item(ref tempGlProfile).Elements.Count-1)
            {
            _Element = objProfiles.Profiles.get_Item(ref tempGlProfile).Elements.get_Item(ref tmpItem).Value;

            Label label1 = new Label();
            DropDownList DDL1 = new DropDownList();
            ListBox ListBox1 = new ListBox();

            label1.Text = _Element.Substring(6, _Element.Length - 6).Trim();
            label1.Width = 100;

            DDL1.Width = 300;
            DDL1.Items.Add("Match Any Value");
            DDL1.SelectedIndexChanged += new EventHandler(DDL1_SelectedIndexChanged);
            Level = "0"+i;

            DI.GetDictionaryLevelList(_YearPeriod.Substring(0, 4), 1 ,Level, Gl_Sec, Gl_Exclude,ref rsDI, ref _ErrorMsg);
            if (!_ErrorMsg.Equals(""))
            {

            //put the error message into a label
            }
            else
            {
            if (rsDI.RecordCount != 0)
            {
            rsDI.MoveFirst();
            while (rsDI.EOF == false)
            {
            DDL1.Items.Add(rsDI.Fields["DI_ACCT"].Value + " " + rsDI.Fields["DI_Desc"].Value);
            rsDI.MoveNext();
            }
            }
            DDL1.Text = DDL1.Items[0].Text;

            }

            TableCell tc1 = new TableCell();
            tc1.Controls.Add(label1 );
            tc1.Width = 150;
            tr1.Cells.Add(tc1);

            tc1 = new TableCell();
            tc1.Controls.Add(DDL1);
            tc1.Width = 300;
            tr1.Cells.Add (tc1);
            }
            }

            private void DDL1_SelectedIndexChanged(object sender, EventArgs e)
            {
            string SelectedElement = DDL1.SelectedItem.ToString ();
            if (SelectedElement.Equals("Match Any Value"))
            {
            MyDynamicString += SelectedElement.Substring(0, 2);
            }
            }



            The DDL1_SelectedIndexChanged never triggered. I don't know why.更多精彩文章及讨论,请光临枫下论坛 rolia.net
            • 建议你先读一读有关书籍或相关网站,你的问题是比较普遍的. 我猜是ViewState的问题. 处理不好的话dynamic controls的viewstate可能在postback后就丢了.一个方法检查,就是在IE里view source,看有没有你的__doPostBack function.
              建议你多Google. 比在这里问问题快得多. :-)
            • Sorry for later reply. you should call the dynamic function in both page load and post back.
              • Thanks!. Yes, I did that, it works fine now.