开发者

GridView.FindControl() - works, but not properly

开发者 https://www.devze.com 2022-12-11 20:23 出处:网络
I have the the GridView\'s column looking like that: <Columns> <asp:TemplateField HeaderText=\"Opcje\">

I have the the GridView's column looking like that:

         <Columns>
            <asp:TemplateField HeaderText="Opcje">
                <ItemTemplate>
                    <asp:LinkButton runat="server" Text="Accept" ID="AcceptBtn" CommandName="Accept"/>
                    <asp:LinkButton runat="server" T开发者_开发技巧ext="Deny" ID="DenyBtn" CommandName="Deny"/> 
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>

while a new row is being created, I want to change both LinkButton's CommandArgument property:

    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;
    }

The problem is, the code seems to be not changing anything, when I click on the AcceptBtn, the code below is invoked:

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

ss = "". Why ? If the page is PostedBack, both CommandArguments are cleared ?


Try setting the CommandArgument in the RowDataBound event instead of RowCreated.


you need to use rowdatabound event like..

 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;
    }
}


where do you set

myFiles[fileIndex].Name;

and each of its components?

0

精彩评论

暂无评论...
验证码 换一张
取 消