GridViewRowCommand keep fire

by right my label1.text will be display by each clicks, however my label was fire during page load, and this is not what i want, so any idea can perform fire event per clicks?

gridview property

asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" OnRowDataBound="abcde"

link button that inside gridview

asp:LinkButton ID="lnkname" runat="server" Text='<%#Eval("movieTitle") %>' Width=500 CommandName="cmdLink">

code behind for link button

protected void abcde(object sender, GridViewRowEventArgs e)

{

    Label1.Text = ((LinkButton)e.Row.FindControl("lnkname")).Text;


}

Problem is that whenever any click event occur in Gridview that time GridviewRowCommand will be fire..you just check that when you want to fire this event.its means during paging event also fire this event..thats y just check it out using [CommandName] like this

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand.aspx

        string namec = e.CommandName.ToString();
        if (namec == "cmdLink")
        {
           //put your code  
        } 

It sounds like you bind the gridview on page load event. Every time you bind the gridview, the onbound event will be fired:

protected void abcde(object sender, GridViewRowEventArgs e)

You can avoid that by checking postback in pageload before gridview databind:

if (!IsPostBack)
{
     //Gridview databind
}

Change the Command Name of LinkButton to

CommandName="Select"

and use GridView1_SelectIndexChanged event First Get the Select Row

GridViewRow gvr =  new  GridView1.SelectedRow;

and FindControl of the row

链接地址: http://www.djcxy.com/p/46782.html

上一篇: .rar,.zip文件的MIME类型

下一篇: GridViewRowCommand保持火