动态地添加一个命令按钮到GridView [英] Dynamically adding a command button to a GridView

查看:71
本文介绍了动态地添加一个命令按钮到GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在与试图将按钮添加到我的网的问题。我的GridView先装载在pageLoad的事件数据。

I'm having an issue with trying to add a button to my grid. My GridView is first loaded with data in the PageLoad event.

我然后取在每一行的第一个单元中的数据,并创建一个按钮,将链接到一个URL。要获取URL,我必须运行在作为参数,第一个单元格中的数据的查询。我在RowDataBound事件在第一次这样做,但触及该查询的每一行是使得它真的很慢。

I'm then taking the data in the first cell of each row, and creating a button that will link to a URL. To get the URL, I have to run a query with the data in the first cell as a parameter. I was doing this in the RowDataBound event at first, but hitting that query for every row was making it really slow.

所以我决定增加一个按钮,当您单击该按钮,将只检索URL。

So I decided to add a button that would retrieve the URL only when you clicked the button.

下面是我的GridView的:

Here's my GridView:

<asp:GridView ID="gvResults" runat="server"
    OnRowDataBound="gvResults_RowDataBound"
    OnRowCommand="gvResults_RowCommand">               
</asp:GridView>    

和我的code:

protected void gvResults_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.DataItem != null)
    {
        LinkButton lb = new LinkButton();
        lb.CommandArgument = e.Row.Cells[0].Text;
        lb.CommandName = "NumClick";
        lb.Text = e.Row.Cells[0].Text;

        e.Row.Cells[0].Controls.Add((Control)lb);
    }
}


protected void gvResults_RowCommand(object sender, CommandEventArgs e)
{
    switch (e.CommandName.ToLower())
    {
        case "numclick":
            string url = GetUrl(e.CommandArgument.ToString());
            Response.Redirect(url);
            break;
        default:
            break;
    }
}

网格生成细,按钮被添加到网格的每一行。但是,当我点击它,在RowCommand事件不火了,页面只是刷新。

The grid generates fine, the button gets added to the grid for each row. But when I click on it, the RowCommand event doesn't fire, and the page just refreshes.

有谁知道问题是什么?

推荐答案

为什么要使用动态按钮呢?您可以轻松地将LinkBut​​ton的直接进入GridView控件的标记(只要你不介意使用模板字段),并会有没有必要与RowDataBound事件陷入混乱。

Why use a dynamic button at all? You can easily put the linkbutton directly into the markup of the gridview (as long as you don't mind using a template field) and there will be no need to mess around with the RowDataBound event.

您的标记将类似于以下内容:

Your markup would look something like the following:

<Columns>
    <asp:TemplateField HeaderText="SomeHeaderText">
            <ItemTemplate>
                <asp:LinkButton ID="lnkBtn" runat="server" CommandName="NumClick" CommandArgument= '<%# (string)Eval("dbValue") %>'  Text='<%# (string)Eval("dbValue") %>'></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>        
    <asp:BoundField></asp:BoundField>
    <asp:BoundField></asp:BoundField>
    <asp:BoundField></asp:BoundField>
</Columns>


添加断点到RowCommand事件,并确保你能打到断点。


Add breakpoints to the RowCommand event and make sure that you can hit the breakpoints.

问题可能在其他地方。

此外,还要确保你没有数据绑定在回发。

Also, make sure that you're not databinding on postback.

这篇关于动态地添加一个命令按钮到GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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