GridView 中点击行操作显示基本信息,类似查看的功能。
实现的功能是 ,我点击 Gridview 的行 就能显示下面的基础信息,而不是我去点击查看显示信息。(实际上 他的原理 还是 点击查看按钮,只是把查看按钮给隐藏了,当点击行后就调用查看按钮的事件。)
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" EnableModelValidation="True" Height="50px" ForeColor="Black" BorderStyle="Solid" BorderWidth="1px" BorderColor="#666666" Width="3800px" OnRowDataBound="GridView1_RowDataBound" DataKeyNames="zcbm,FWZC_ID,fwbm" AllowPaging="true" OnRowCommand="GridView1_RowCommand"> <Columns> <asp:CommandField> <ControlStyle BackColor="White" /> <ItemStyle BackColor="#B8AEC1" ForeColor="#B8AEC1" /> </asp:CommandField> <asp:TemplateField> <ItemTemplate> <%--<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>--%> <asp:LinkButton ID="btnsc" runat="server" Text="删除" CommandName="sc" OnClientClick="return delcfm_zc()" /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField> <HeaderStyle CssClass="yangshi" /> <ItemStyle CssClass="yangshi" /> <ItemTemplate> <%--<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>--%> <asp:LinkButton ID="btnbj" runat="server" Text="编辑" CommandName="bj" /> </ItemTemplate> </asp:TemplateField> <ItemTemplate> </ItemTemplate> </asp:TemplateField>--%> <asp:BoundField DataField="ZCBM" HeaderText="资产编码" /> <asp:BoundField DataField="ZCMC" HeaderText="资产名称" /> <asp:BoundField DataField="FWZC_ID" HeaderText="FWZC_ID"> <HeaderStyle CssClass="yangshi" /> <ItemStyle CssClass="yangshi" /> </asp:BoundField> <asp:BoundField DataField="cou_fwbm" HeaderText="cou_fwbm"> <HeaderStyle CssClass="yangshi" /> <ItemStyle CssClass="yangshi" /> </asp:BoundField> </Columns> <HeaderStyle BackColor="#F5F6F7" Height="20px" /> <PagerSettings Visible="False" /> <SelectedRowStyle BackColor="#B8AEC1" ForeColor="#333333" /> <EditRowStyle BackColor="#B7D3FA" /> </asp:GridView> //点击gridview 行事件 function ClickEvent(cid) { document.getElementById(cid).click(); }
后台代码:
实际上我们还是要在 RowCommand 中写好查看的事件 只是在RowDateBound 中给注册一下 我这里是点击的 编辑按钮 id=btnbj
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { this.lblCurrentPage.Text = string.Format("当前第{0}页/总共{1}页", this.GridView1.PageIndex + 1, this.GridView1.PageCount); e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor,this.style.backgroundColor='#B8AEC1'"); e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c"); //gd(); string id = e.Row.Cells[1].FindControl("btnbj").ClientID; // 行单击事件 for (int columnIndex = 1; columnIndex < e.Row.Cells.Count - 3; columnIndex++) { // e.Row.Cells[columnIndex].Attributes["onclick"] = js; //每一个单 e.Row.Cells[columnIndex].Attributes.Add("OnClick", "ClickEvent('" + id + "')"); } } } // 这里就不粘贴那么多了,实际上我们还是要在 RowCommand 中写好查看的事件 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { try { string lbt = e.CommandName; string keyname = GridView1.DataKeyNames[1].ToString();//获取主键名字 string keyzcbm = GridView1.DataKeyNames[0].ToString();//获取主键名字 string keyfcbm = GridView1.DataKeyNames[2].ToString();//获取主键名字 //GridViewRow gvrow = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer); //int index = gvrow.RowIndex; //string zcbm1 = this.GridView1.DataKeys[index][keyname].ToString(); //wsw修改 选择 if (lbt == "xz") { GridViewRow gvrow = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer); int index = gvrow.RowIndex; string fwzc_id = this.GridView1.DataKeys[index][keyname].ToString(); Select(fwzc_id); dwsxx.Enabled = false; ssqyy.Enabled = false; zcsxx.Enabled = false; sfzzz.Enabled = false; } }