GridView.FindControl() - 有效,但不正确

时间:2009-11-20 13:07:42

标签: c# .net asp.net gridview

我有GridView的列看起来像这样:

         <Columns>
            <asp:TemplateField HeaderText="Opcje">
                <ItemTemplate>
                    <asp:LinkButton runat="server" Text="Accept" ID="AcceptBtn" CommandName="Accept"/>
                    <asp:LinkButton runat="server" Text="Deny" ID="DenyBtn" CommandName="Deny"/> 
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>

在创建新行时,我想更改LinkBut​​ton的CommandArgument属性:

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        ((LinkButton)e.Row.FindControl("AcceptBtn")).CommandArgument = myFiles[fileIndex].Name;
        ((LinkButton)e.Row.FindControl("DenyBtn")).CommandArgument = myFiles[fileIndex].Name;
    }

问题是,代码似乎没有改变任何东西,当我点击AcceptBtn时,调用下面的代码:

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Accept")
        {
            string ss = (string)e.CommandArgument;
            ...
        }
    }

ss =“”。为什么?如果页面是PostedBack,那么两个CommandArguments都被清除了?

3 个答案:

答案 0 :(得分:2)

尝试在CommandArgument事件中设置RowDataBound而不是RowCreated

答案 1 :(得分:1)

你需要使用像..的

这样的rowdatabound事件
 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        ((LinkButton)e.Row.FindControl("AcceptBtn")).CommandArgument = myFiles[fileIndex].Name;
        ((LinkButton)e.Row.FindControl("DenyBtn")).CommandArgument = myFiles[fileIndex].Name;
    }
}

答案 2 :(得分:0)

你在哪里设置

myFiles[fileIndex].Name;

及其每个组件?