当前位置: 代码迷 >> ASP.NET >> GridView中的LinkButton的OnClick事件不触发
  详细解决方案

GridView中的LinkButton的OnClick事件不触发

热度:9239   发布时间:2013-02-25 00:00:00.0
求助:GridView中的LinkButton的OnClick事件不触发
前台:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Height="16px" Width="100%" AllowSorting="True" onpageindexchanging="GridView1_PageIndexChanging" DataKeyNames="CD_id" BorderStyle="Dotted" GridLines="Horizontal" onrowdatabound="GridView1_RowDataBound" onrowcommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField HeaderStyle-CssClass="GridView_head_collect" HeaderText="下载简历" ItemStyle-CssClass="textcenter">
  <ItemTemplate>
  <asp:LinkButton ID="download" runat="server" onclick="download_Click" CommandName="download">下载</asp:LinkButton>
  <asp:Label ID="lb_resid" runat="server" Text='<%# Eval("res_id") %>' Visible="False"></asp:Label>  
  </ItemTemplate>
  <HeaderStyle CssClass="GridView_head_collect"></HeaderStyle>
  <ItemStyle CssClass="textcenter"></ItemStyle>
</asp:TemplateField>
</Columns>
</asp:GridView>
后台:
C# code
 int resid = 0;        int pdid = 0;        try        {            con.Open();            LinkButton lb = (LinkButton)sender;            DataControlFieldCell dcf = (DataControlFieldCell)lb.Parent;            GridViewRow grv = (GridViewRow)dcf.Parent;            Label lbr = (Label)GridView1.Rows[grv.RowIndex].FindControl("lb_resid");            resid = Convert.ToInt32(lbr.Text);            SqlCommand cmd = new SqlCommand("select PD_id from xxx where CD_id=" + GridView1.DataKeys[grv.RowIndex].Value);            pdid = Convert.ToInt32(cmd.ExecuteScalar());        }        catch (Exception ee)        {        }        finally        {            con.Close();        }        errors = "<script>window.open('xxx.aspx?res_id=" + resid + "&PD_id=" + pdid + "','width=400,height=150,,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no')</script>";


无论我怎么点击linkbutton,就是不触发控件,求高手指教

------解决方案--------------------------------------------------------
你贴出来的这一段后端的cs代码,是在哪个事件里?download_Click??

事实上你只要写在GridView1_RowCommand事件里,判断CommandName就行了。LinkButton的onclick="download_Click"事件完全是多余的。
另外
 Label lbr = (Label)GridView1.Rows[grv.RowIndex].FindControl("lb_resid");
 resid = Convert.ToInt32(lbr.Text);
这种传值方式也显得笨重,取值还要先找到控件。。可以用LinkButton的CommandArgument传值
  相关解决方案