在GridView隐藏字段

时间:2021-12-21 00:19:38

在GridView隐藏字段

在GridView中隐藏一字段,方便这条记录的处理,同时隐藏一个Button实现点击这条记录时的处理

1.绑定

<asp:TemplateField>
                        <HeaderTemplate>
                              <label id="forApprove">是否批准</label>  
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:DropDownList ID="approve" runat="server" AutoPostBack="True" OnSelectedIndexChanged="approve_SelectedIndexChanged">
                            <asp:ListItem Selected="True">待定</asp:ListItem>
                            <asp:ListItem>同意</asp:ListItem>
                            <asp:ListItem>不同意</asp:ListItem>
                        </asp:DropDownList>
                         <asp:Label ID="extraID" Visible="false"  runat="server" Text='<%# Eval("ExtraID") %>'></asp:Label>

<asp:Button ID="hiddenPost" CommandName="hiddenCommand"  runat="server"
                            Text="hiddenButton" style="display:none" />
                    </ItemTemplate>     
 </asp:TemplateField>

2.处理

实现光棒效果,并注册这条记录点击时的处理

protected void Gv_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            Button btn = e.Row.FindControl("hiddenPost") as Button;
            if (btn != null)
            {
                e.Row.Attributes["onclick"] = string.Format("javascript:document.getElementById('{0}').click();", btn.ClientID);
            }
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#FED8E0'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#99CCFF'");
            }
        }

protected void Gv_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "hiddenCommand":
                    Control cmdControl = e.CommandSource as Control;
                    GridViewRow row = cmdControl.NamingContainer as GridViewRow;
                    string Id = (row.FindControl("extraID") as Label).Text.Trim();
                    if (!string.IsNullOrEmpty(Id))
                    {
                        Response.Redirect("....aspx?extraId=" + Id);
                    }
                    break;
            }
        }