Gridview与向下钻取

时间:2012-06-29 10:28:41

标签: jquery asp.net gridview webforms drilldown

大家好我需要你的帮助,我手里有一个项目就是使用gridviews和drilldown,我必须能够点击一个按钮或在线上并向下延伸到另一行,详细信息,我已经有了按钮的代码展开和折叠并更改按钮但不能同时执行这两个操作并且正是我想要的,当我点击“加”时,线条会被展开并将按钮图像更改为减号和减号更改点击“加号”并折叠详细信息。  谢谢 :) 这是gridview的代码,jquery和codebehind:

Gridview:
    <asp:GridView 
    ID="gvMajor" 
    runat="server" 
    AutoGenerateColumns="False" 
    BackColor="White" 
    BorderColor="Black" 
    BorderStyle="Ridge" 
    BorderWidth="1px" 
    CellPadding="1" 
    onrowdatabound="MyGrid_RowDataBound" 
    onrowcommand="Delete_info" 
    AllowPaging="True" 
    onselectedindexchanging="Edit_info" 
    PageSize="5" 
    Width="100%"  
    onpageindexchanged="GridView1_PageIndexChanged" 
    DataKeyNames="ID" 
    DataSourceID="MajorsDS" 
        onselectedindexchanged="gvMajor_SelectedIndexChanged"  >

    <RowStyle 
        BackColor="#EEEEEE" 
        ForeColor="Black" 
        BorderColor="Black"/>

    <Columns>

        <asp:TemplateField ShowHeader="False">
            <ItemTemplate>
                <asp:ImageButton ID="ImageButton1" runat="server" 
                    CommandName="Expand/Collapse" ImageUrl="~/Images/Expand.png" 
                    Text="Button" CssClass="View"/>
            </ItemTemplate>
        </asp:TemplateField>

        <asp:BoundField DataField="ID" HeaderText="ID" 
            SortExpression="ID" ReadOnly="True" />
        <asp:BoundField DataField="Subject" HeaderText="Subject" 
            SortExpression="Subject" />
        <asp:BoundField DataField="InsertDate" HeaderText="InsertDate" 
            SortExpression="InsertDate" />
        <asp:BoundField DataField="DisabledDate" HeaderText="DisabledDate" 
            SortExpression="DisabledDate" />
        <asp:CommandField ShowSelectButton="True" ButtonType="Image" 
            SelectImageUrl="~/Images/1340789891_gtk-edit.png" />

        <asp:ButtonField ButtonType="Image" CommandName="Delete_info" 
                    ImageUrl="~/Images/1340790235_gnome-fs-trash-empty.png" 
            Text="Button"  />

    </Columns>

<FooterStyle 
    BackColor="#CCCCCC" 
    ForeColor="Black" />

<PagerStyle 
    BackColor="#999999" 
    ForeColor="Black" 
    HorizontalAlign="Center" />

<SelectedRowStyle 
    BackColor="#008A8C" 
    Font-Bold="True" 
    ForeColor="White" />

<HeaderStyle 
    BackColor="#000084" 
    Font-Bold="True" 
    ForeColor="White" />

<AlternatingRowStyle 
    BackColor="Gainsboro" />

</asp:GridView>

Jquery:

    <script type="text/jscript">
    $('.View').click(function()
    {
        $('#details').remove();
        var detailID=$(this).attr('data-id');

        $(this).closest('tr').after('<tr id="details"><td colspan="9"></td></tr>')
        $('#details td').load('Details.aspx?detail='+detailID+' #content');

    });
</script> 

<script type="text/jscript">
    $('.Hide').click(function()
    {
        $('table td img.Hide').click(function(){     $(this).parent().parent().remove(); });
    });
</script> 


Codebehind: 

        protected void Delete_info(object sender, GridViewCommandEventArgs e)
    {
        sqlconn.Open();
        int idx = Convert.ToInt32(e.CommandArgument);
        if (e.CommandName == "Delete_info")
        {
            GridViewRow selectedRow = gvMajor.Rows[idx];
            TableCell ID_info = selectedRow.Cells[2];
            string ID = ID_info.Text;
            //código para descobrir o nome do user introduzido
            string user_status = "Select Status FROM JANELA_CSD_USERS WHERE ID=@ID";
            SqlCommand USTATUS = new SqlCommand(user_status, sqlconn);
            USTATUS.Parameters.AddWithValue("@ID", Convert.ToInt32(ID.ToString()));
            USTATUS.ExecuteNonQuery();
            int User_status = Convert.ToInt32(USTATUS.ExecuteScalar());
            if (User_status == 1)
            {


                SqlCommand sqlcomm = new SqlCommand("UPDATE JANELA_CSD_INFO SET Status=0, DisabledDate=@DisabledDate WHERE ID=@ID", sqlconn);
                sqlcomm.Parameters.Add("@DisabledDate", SqlDbType.SmallDateTime).Value = DateTime.Now.Date;
                sqlcomm.Parameters.Add("@ID", SqlDbType.Int).Value = Convert.ToInt32(ID.ToString());
                sqlcomm.ExecuteNonQuery();

            }
            else
            {
            }
            sqlconn.Close();
        }
        if (e.CommandName == "Expand/Collapse")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow gvrow =gvMajor.Rows[index];
            if (((ImageButton)gvrow.Controls[0].Controls[0]).ImageUrl == "~/Images/1340791822_delete.png")
                ((ImageButton)gvrow.Controls[0].Controls[0]).ImageUrl = "~/Images/1340791835_add.png";
            else
                ((ImageButton)gvrow.Controls[0].Controls[0]).ImageUrl = "~/Images/1340791822_delete.png";
        }
    }

0 个答案:

没有答案