为什么我的Linkbuttons命令的功能没有得到触发时? [英] Why is my Linkbuttons command function not getting trigged?

查看:167
本文介绍了为什么我的Linkbuttons命令的功能没有得到触发时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的code在我的codebehind:

I have this code in my codebehind:

   for (int i = 0; i < linkList.Count; i++)
            {
                var link = UppercaseFirst(linkList[i]);
                var linkButton = new LinkButton
                    {
                        Text = link + " > ",
                        ID = Convert.ToString(i),
                        CommandArgument = urlList[i]
                    };
                linkButton.Command += new CommandEventHandler(lnkWeb_Click);
                bcHolder.Controls.Add(linkButton);

            }

和这里是lnkWeb_Click方式:

and here is the lnkWeb_Click method:

protected void lnkWeb_Click(object sender, CommandEventArgs e)
        {
          var url = e.CommandArgument.ToString();
          //code...
        }

这个方法,当我点击这些产生linkbuttons之一是没有得到触发。

This method is not getting triggered when I click on one of those generated linkbuttons.

任何人有任何的想法是什么问题?

Anyone have any idea what the problem is?

尝试按需=lnkWeb_Click在ASPX文件和方法得到触发,而不是那些由我code产生。他们甚至不必须按需=lnkWeb_Click属性。

Tried OnCommand="lnkWeb_Click" in the aspx file and the method got trigged, but not those that I generate by code. They dont even have OnCommand="lnkWeb_Click" attribute.

推荐答案

这里的问题是控制生命周期。如果你要妥善处理一些控制的事件 - 你必须把这个控件添加到页面上的每一个页面加载过程,即在每一个回发

The problem here is with the control life cycle. If you want to handle events of some control properly - you have to add this control to the page on every page loading process, that is on every postback.

看看会发生什么你的情况:

Look what happens in your case:


  1. 初始按钮被点击

  2. 在回发的动态链接按钮添加到页面,事件处理程序分配给他们

  3. 在新生成的链接按钮,点击

  4. 在回发这些动态链接按钮不会添加到页面再次,ASP.NET不知道事件的起源,因此它不会调用处理程序。

要解决这个问题,你可能需要有关必须添加链接按钮视图状态信息存储(请不要存放控件本身,这将是一个巨大的开销)。另外要注意其标识 - 他们必须为同一控制相同

To fix this you might need to store in the View State information about link buttons that have to be added (please do not store the controls themselves, that would be a huge overhead). Also pay attention to their IDs - they have to be the same for the same controls.

更新。的视图状态的解决方案的一些更多的提示。

Update. Some more hints on the View State solution.

基本上你需要一些指标,该页面加载过程中,你需要创建一些动态链接按钮。要做到这一点非常基本的方法是将存储链接按钮标识符的列表(或文字,或两者),然后在的Page_Load 检查是否有任何存储在视图状态。例如:

Basically you need some indicator that during the page loading you need to create some dynamic link buttons. The very basic way to do it is to store the list of the link button identifiers (or texts, or both) and then during Page_Load check if there is anything stored in View State. For example:

// Property to access the view state data
protected List<string> Links
{
    get { return ViewState['links']; }
    set { ViewState['links'] = value; }
}

...

protected void Page_Load(object sender, EventArgs e)
{
    ...
    if (this.Links != null && this.Links.Count > 0)
    {
        // inside this method you create your link buttons and add them to the page
        // you actually have this code already
        RenderLinkButtons();
    }
}

...

// Not sure about what name you have here
protected void InitialButtonHandlerName(object sender, EventArgs e)
{
    List<string> linkList = ...; //your variable, guessing a type

    // this is exactly the method you use already to add links to the page
    // just one more action added to it - store info about these links into View State to use it on later post backs
    this.Links = linkList;
    RenderLinkButtons();    
}

请使用它只是在正确的方向的一个点 - 你可能会根据您的要求和preferences有不同的实现。但我希望现在的概念是明确的。

Please use it just a point in right direction - you might have different implementation depending on your requirements and preferences. But I hope concept is clear now.

这篇关于为什么我的Linkbuttons命令的功能没有得到触发时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆