gridview中嵌套控件示例 —— 添加了一个linkbutton 前台: asp:GridView ID= " gv2 " runat= " server " AutoGenerateColumns= " false " OnRowCommand= " gv2_RowCommand " Columns asp:BoundField DataField= " pref_name " HeaderText=
gridview中嵌套控件示例 —— 添加了一个linkbutton
前台:
<asp:GridView ID="gv2" runat="server" AutoGenerateColumns="false" OnRowCommand="gv2_RowCommand"> <Columns> <asp:BoundField DataField="pref_name" HeaderText="地区" /> <asp:TemplateField> <ItemTemplate> <asp:LinkButton runat="server" ID="gv_lbtn" Text="测试1" CommandArgument="<%# Container.DataItemIndex %>" CommandName="test1" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
后台:
protected void gv2_RowCommand(object sender, GridViewCommandEventArgs e) { switch (e.CommandName) { case "test1": int rowIndex = Convert.ToInt32(e.CommandArgument); GridViewRow gr = gv2.Rows[rowIndex]; string alter = string.Format("alert(‘{0}‘)", gr.Cells[0].Text.Trim()); ScriptManager.RegisterStartupScript(Page, GetType(), "", alter, true); break; default: break; } }