简体   繁体   English

动态创建的链接按钮的常见事件未触发

[英]Dynamically created linkbuttons' common event not firing

Whenever DropDownList SelectedIndexChanged, I am adding LinkButtons as ul-li list in codebehind. 每当DropDownList SelectedIndexChanged时,我都会在代码隐藏中将LinkBut​​tons作为ul-li列表添加。 Each linkbuttons were assigned with IDs and a common Click event. 每个链接按钮都分配了ID和一个通用的Click事件。 Problem is code in Click event is not executed or maybe event is not triggered. 问题是Click事件中的代码未执行,或者事件未触发。 My code below: [Edit] I tried like this as suggested in other posts ( dynamically created list of link buttons, link buttons not posting back ) 我的代码如下:[编辑]我按照其他帖子中的建议进行了尝试( 动态创建的链接按钮列表,链接按钮不发回

protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
   populate();
}

protected override void OnInit(EventArgs e)
{
   base.OnInit(e);
   populate();
}

void populate()
{
   HtmlGenericControl ulList = new HtmlGenericControl("ul");
   panel.Controls.Add(ulList);

   foreach (DataRow dr in drc) 
   {
      HtmlGenericControl liList = new HtmlGenericControl("li");
      ulList.Controls.Add(liList);

      var lnk = new LinkButton();
      lnk.ID = dr["col1"].ToString();
      lnk.Text = dr["col1"].ToString();
      lnk.Click += Clicked;
      liList.Controls.Add(lnk);
   }
}              

private void Clicked(object sender, EventArgs e)
{
   var btn = (LinkButton)sender;
   label1.Text = btn.ID.ToString();
}

Im missing something. 我错过了一些东西。 Any help please. 请帮忙。

Here the issue is with the ViewState . 这是ViewState When the selected index of the dropdownlist changes there is a postback which takes place and the previous state is lost, so at this point you have to maintain the state of the controls. dropdownlist的选定索引更改时,将发生一个postback ,并且先前的状态将丢失,因此在这一点上,您必须保持控件的状态。

Now in your code actually the state of the control is lost and so the click event does not fire. 现在,在您的代码中,控件的状态实际上已经丢失,因此click event不会触发。 So the solution is to maintain the state of the controls. 因此解决方案是保持控件的状态。

The Below is a working example, you can just paste it and try. 以下是一个有效的示例,您可以将其粘贴并尝试。

This is my page load . 这是我的page load

protected void Page_Load(object sender, EventArgs e)
        {
            for (var i = 0; i < LinkButtonNumber; i++)
                AddLinkButton(i);
        }

Similarly you have to maintain the state of the previously added control like this. 同样,您必须像这样维护先前添加的控件的状态。

private int LinkButtonNumber
        {
            get
            {
                var number = ViewState["linkButtonNumber"];
                return (number == null) ? 0 : (int)number;
            }
            set
            {
                ViewState["linkButtonNumber"] = value;
            }
        }

The below is my SelectedIndexChanged Event for the DropDownList 以下是我的DropDownList SelectedIndexChanged Event

protected void Example_SelectedIndexChanged(object sender, EventArgs e)
        {
            AddLinkButton(LinkButtonNumber);
            LinkButtonNumber++;
        }

I have a function which dynamically creates the controls, which is called on the page load and on SelectedIndexChanged . 我有一个动态创建控件的函数,该函数在页面加载和SelectedIndexChanged上调用。

private void AddLinkButton(int index)
        {
            LinkButton linkbutton = new LinkButton { ID = string.Concat("txtDomain", index) };
            linkbutton.ClientIDMode = ClientIDMode.Static;
            linkbutton.Text = "Link Button ";
            linkbutton.Click += linkbutton_Click;
            PanelDomain.Controls.Add(linkbutton);
            PanelDomain.Controls.Add(new LiteralControl("<br />"));
        }

And this is the Click event for the LinkButton 这是LinkButtonClick event

void linkbutton_Click(object sender, EventArgs e)
        {
            //You logic here
        }

I solved it using brute code lol. 我使用蛮横的代码哈哈解决了它。 Since controls' event were not bound on postback then we recreate them on postback. 由于控件的事件未绑定在回发上,因此我们在回发时重新创建它们。 So in my Page_Load I called the module that re-creates the controls, thus binding them to corresponding event. 因此,在我的Page_Load中,我调用了重新创建控件的模块,从而将它们绑定到相应的事件。 This works, but... 这有效,但是...

Re-creating these controls create duplicates ( Multiple Controls with same ID were found ) and you will get into trouble in instances of finding a control by ID like using panel.FindControl . 重新创建这些控件会创建重复项( Multiple Controls with same ID were found ),在按ID查找控件的实例(如使用panel.FindControl )时,您会遇到麻烦。 To remedy this scenario, I put a check if same control ID already existed before recreating them, and voila! 为了纠正这种情况,我在重新创建控件之前检查了是否存在相同的控件ID,瞧! It works. 有用。

protected void Page_Load(object sender, EventArgs e)
{
   populate();
}

void populate()
{
   HtmlGenericControl ulList = new HtmlGenericControl("ul");
   panel.Controls.Add(ulList);

   foreach (DataRow dr in drc) 
   {
      HtmlGenericControl liList = new HtmlGenericControl("li");
      ulList.Controls.Add(liList);

      if (liList.FindControl(dr["col1"].ToString()) == null)
      {
          var lnk = new LinkButton();
          lnk.ID = dr["col1"].ToString();
          lnk.Text = dr["col1"].ToString();
          lnk.Click += Clicked;
          liList.Controls.Add(lnk);
      }
   }
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM