简体   繁体   中英

LinkButton CommandName not working

I have a link button ( the text of the link button is edit ) in a gridview , i give it a commandname call modify

So in my design the code is :

   <asp:TemplateField>
      <ItemTemplate>
          <asp:LinkButton ID="LinkButton1" runat="server" CommandName="modify">Edit</asp:LinkButton>
           </ItemTemplate>
   </asp:TemplateField>

And in my code behind is :

  protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    LinkButton linkBtn = (LinkButton)sender;

    if (linkBtn.CommandName == "modify") // tried linkBtn.CommandArguemtn , doesn't help
    {
        Panel1.Visible = true;
        int index = Convert.ToInt32(e.CommandArgument);       

        Label login = (Label)GridView1.Rows[index].Cells[0].FindControl("Label1");
       //Things i want to do 

    }
}

As you can see i set the panel to be visible , but it won't appear ... am i doing the correct way? i know of an alternative which is to use the default edit button generated , but i don't want to do that . I am trying to put a link button in a gridview and click on the link button and then panel ( consists of textboxs controls ) should appear then make changes of the gridview data in the panel .

just use

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "modify")
    {
        Panel1.Visible = true;
        int index = Convert.ToInt32(e.CommandArgument);       

        Label login = (Label)GridView1.Rows[index].Cells[0].FindControl("Label1");
       //Things i want to do 

    }
}

you wont need to check the button first, e.CommandName will be sufficent

Try like this.use the CommandName property to specify or determine the command name MSDN

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if(e.CommandName=="modify")
        {
            \\
         }
}

Use the following code

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{


    if (e.CommandName.ToLower()== "modify") 
    {
        Panel1.Visible = true;
        int index = Convert.ToInt32(e.CommandArgument);       

        Label login = (Label)GridView1.Rows[index].Cells[0].FindControl("Label1");
       //Things i want to do 

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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