简体   繁体   English

在自定义控件中渲染LinkBut​​ton

[英]Render LinkButton in custom control

I am creating a custom control with markup like this: 我正在使用这样的标记创建自定义控件:

<Custom:CustomGrid>
    <Columns>
        <Custom:DataColumn HeaderText="FirstName" />
        <Custom:DataColumn HeaderText="LastName" />
    </Columns>
</Custom:CustomGrid>

I am overriding both Render and RenderChildren in order to render the outer Div/Table and Column tags respectively. 我同时重写Render和RenderChildren以便分别渲染外部Div / Table和Column标签。

I want each of the THs to include a LinkButton, with the HeaderText for sorting. 我希望每个TH都包含一个LinkBut​​ton,其中HeaderText用于排序。 I created a foreach loop to run over the collection of columns and create a TH for each with a LinkButton inside it. 我创建了一个foreach循环来foreach列的集合,并为每个循环创建一个TH,并在其中带有LinkBut​​ton。 I render out the LinkButton using linkBtn.RenderControl(writer); 我使用linkBtn.RenderControl(writer);渲染出LinkBut​​ton linkBtn.RenderControl(writer);

What I get in html is just an empty anchor tag. 我在html中得到的只是一个空的锚标记。 I'm guessing this is due to the fact that asp.net does not know to render the JS side of the server control when it is rendered this way. 我猜这是由于asp.net不知道在以这种方式呈现时呈现服务器控件的JS端。 But I have no idea what I can do differently. 但是我不知道我能做些什么。 Any ideas? 有任何想法吗?

EDIT: I can see that href="javascript:_doPostBack('myGrid$Sort_firstName','')" is created. 编辑:我可以看到创建了href =“ javascript:_doPostBack('myGrid $ Sort_firstName','')”。 I don't know why the characters are html escaped, but I'm guessing it's just the html source viewer (or maybe not...) Anyway, the event handler is still not being fired. 我不知道为什么字符会被html转义,但是我猜想它只是html源代码查看器(或者可能不是...)。无论如何,事件处理程序仍然没有被触发。

Turns out that CreateChildControls is not the place to create child controls... (Who would of thunk it...) 事实证明CreateChildControls不是创建子控件的地方...(谁想对它进行修改...)

I moved the code adding the controls to the Controls colletion to OnInit and everything was wiered up correctly. 我将代码添加到OnInit的“控件”集合中,然后移动了代码,一切都正常了。

Turns out that CreateChildControls is called too late in the lifecycle for the post back to route the events correctly. 事实证明,在生命周期中调用CreateChildControls太晚了,以至于帖子无法正确地路由事件。

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

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