如何在listview的侧面布局模板中找到并给出一个控件的css类?

时间:2012-08-09 12:35:55

标签: c# asp.net listview

我有一个listview控件,在布局模板中,我有链接按钮。现在我想做什么。我有每个链接按钮的跨度。我想点击链接按钮时给css类。我的HTML代码如下:

<asp:ListView ID="lst_CallType" runat="server" ItemPlaceholderID="tr" OnItemDataBound="lst_CallType_ItemDataBound">
                            <LayoutTemplate>
                                <table cellspacing="0">
                                    <tr class="hdrRowColor1">
                                        <td align="left" width="500px">
                                            <asp:LinkButton ID="lnk_Name" runat="server" ValidationGroup="vgSearch" OnClientClick="changeSortState();"
                                                CommandArgument="tblCallTypenew.CallType" OnClick="lnk_Sort">Name</asp:LinkButton>
                                            <span id="imgSortPosition" class="sortNotSelected"></span>
                                        </td>
                                        <td align="left" width="80px">
                                            <asp:LinkButton ID="lnk_Status" runat="server" CommandArgument="tblCallTypenew.isactive"
                                                ValidationGroup="vgSearch" OnClick="lnk_Sort">Status</asp:LinkButton>
                                                <span id="Span1" class="sortNotSelected"></span>
                                        </td>
                                        <td align="left" width="200px">
                                            <asp:LinkButton ID="lnk_CreatedOn" runat="server" CommandArgument="tblCallTypenew.CreatedDate"
                                                ValidationGroup="vgSearch" OnClick="lnk_Sort">Created On</asp:LinkButton>
                                                <span id="Span2" class="sortNotSelected"></span>
                                        </td>
                                        <td align="left" width="200px">
                                            <asp:LinkButton ID="lnk_LastModfiedOn" runat="server" CommandArgument="tblCallTypenew.ModifiedDate"
                                                ValidationGroup="vgSearch" OnClick="lnk_Sort">Last Modified On</asp:LinkButton>
                                                <span id="Span3" class="sortNotSelected"></span>
                                        </td>
                                        <td align="left" width="200px">
                                            <asp:LinkButton ID="lnk_CreatedBy" runat="server" CommandArgument="tblUserNew.FirstName"
                                                ValidationGroup="vgSearch" OnClick="lnk_Sort">Created By</asp:LinkButton>
                                                <span id="Span4" class="sortNotSelected"></span>
                                        </td>
                                        <td align="left" width="200px">
                                            <%--<asp:LinkButton ID="lnkCreatedDate" runat="server" CommandArgument="tblUserActivities.CreatedDate"
                                        OnClick="lnk_Sort">Created Date</asp:LinkButton>--%>
                                            <asp:LinkButton ID="lnk_LastModfiedBy" runat="server" CommandArgument="v.FirstName"
                                                ValidationGroup="vgSearch" OnClick="lnk_Sort">Modified By</asp:LinkButton>
                                                <span id="Span5" class="sortNotSelected"></span>
                                        </td>
                                        <td align="left" style="border-right: 1px solid #6398cc">
                                            Activity
                                            <div style="width: 50px; float: right;">
                                            </div>
                                        </td>
                                    </tr>
                                    <tr id="tr" runat="server">
                                    </tr>
                                </table>
                            </LayoutTemplate>
                            <ItemTemplate>
                                <tr class='<%# Convert.ToBoolean(Container.DataItemIndex % 2) ? "EvenRowColor" : "OddRowColor" %>'>
                                    <td align="left">
                                        <asp:Label ID="lblDeptId" runat="server" Text='<%# Eval("ID")%>' Visible="false"></asp:Label>
                                        <%# Eval("Calltype")%>
                                    </td>
                                    <td align="left">
                                        <asp:Label ID="lbl_Status" runat="server" Style="display: none;" Text='<%# Eval("IsActive")%>'></asp:Label>
                                        <asp:ImageButton ID="imgbtnStatus" runat="server" CommandArgument='<%# Eval("id") %>'
                                            OnClick="imgbtnStatus_Onclick" />
                                    </td>
                                    <td align="left">
                                        <%# Eval("CreatedDate")%>
                                    </td>
                                    <td align="left">
                                        <%# Eval("ModifiedDate") %>
                                    </td>
                                    <td align="left">
                                        <%# Eval("CreatedBy")%>
                                    </td>
                                    <td align="left">
                                        <%# Eval("ModifiedBy")%>
                                    </td>
                                    <td>
                                        <asp:Label ID="lblCallType" runat="server" Style="display: none;" Text='<%# Eval("Calltype")%>'></asp:Label>
                                        <asp:ImageButton ID="imgbtnEdit" runat="server" ImageUrl="~/App_Themes/ThemeNew/Images/edit.png"
                                            ToolTip="Edit Details" CommandArgument='<%# Eval("ID") %>' OnClick="imgbtnEdit_OnClick" />
                                        <asp:ImageButton ID="imgbtnDelete" runat="server" ImageUrl="~/App_Themes/ThemeNew/Images/delete.png"
                                            ToolTip="Delete" Style="display: none;" CommandArgument='<%# Eval("id") %>' OnClientClick="return confirm('Are you sure you want to delete the Call type?');"
                                            OnClick="imgbtnDelete_OnClick" />
                                    </td>
                                </tr>
                            </ItemTemplate>
                        </asp:ListView>

实际上我想在用户点击链接按钮时显示排序图像。我想通过后面的代码来实现。 我的.cs代码如下:

 protected void lnk_Sort(object sender, EventArgs e)
{
    LinkButton lnk = (LinkButton)sender;
    string arg = lnk.CommandArgument.ToString();
    ViewState["sortCol"] = arg;
    GetSortDirection();
    BindData(ViewState["sortCol"].ToString(), ViewState["sortDir"].ToString(), Convert.ToInt32(ViewState["nmbr"]), Pager.PageSize);
}


private void GetSortDirection()
{
    if (Convert.ToString(ViewState["sortDir"]) == "Desc")
    {
        ViewState["sortDir"] = "asc";

    }
    else
    {
        ViewState["sortDir"] = "Desc";
    }
}

2 个答案:

答案 0 :(得分:0)

您可以在Listview_sorting事件

中执行findcontrol

答案 1 :(得分:0)

我用愚蠢的把戏做了

我的HTML代码是:

<asp:ListView ID="lst_CallType" runat="server" ItemPlaceholderID="tr" OnItemDataBound="lst_CallType_ItemDataBound">
                            <LayoutTemplate>
                                <table cellspacing="0">
                                    <tr class="hdrRowColor1">
                                        <td width="35px" align="left">
                                            &nbsp;S.No
                                        </td>
                                        <td align="left" width="300px">
                                            <asp:LinkButton ID="lnk_Name" runat="server" ValidationGroup="vgSearch" CommandArgument="tblCallTypenew.CallType"
                                                OnClick="lnk_Sort">Name</asp:LinkButton>
                                            <asp:Image ID="img_lnk_Name" Visible="false" runat="server" />
                                        </td>
                                        <td align="left" width="150px">
                                            <asp:LinkButton ID="lnk_CreatedBy" runat="server" CommandArgument="tblUserNew.FirstName"
                                                ValidationGroup="vgSearch" OnClick="lnk_Sort">Created By</asp:LinkButton>
                                            <asp:Image ID="img_lnk_CreatedBy" Visible="false" runat="server" />
                                        </td>
                                        <td align="left" width="120px">
                                            <asp:LinkButton ID="lnk_CreatedOn" runat="server" CommandArgument="tblCallTypenew.CreatedDate"
                                                ValidationGroup="vgSearch" OnClick="lnk_Sort">Created On</asp:LinkButton>
                                            <asp:Image ID="img_lnk_CreatedOn" Visible="false" runat="server" />
                                        </td>
                                        <td align="left" width="150px">
                                            <%--<asp:LinkButton ID="lnkCreatedDate" runat="server" CommandArgument="tblUserActivities.CreatedDate"
                                        OnClick="lnk_Sort">Created Date</asp:LinkButton>--%>
                                            <asp:LinkButton ID="lnk_LastModfiedBy" runat="server" CommandArgument="v.FirstName"
                                                ValidationGroup="vgSearch" OnClick="lnk_Sort">Last Modified By</asp:LinkButton>
                                            <asp:Image ID="img_lnk_LastModfiedBy" Visible="false" runat="server" />
                                        </td>
                                        <td align="left" width="120px">
                                            <asp:LinkButton ID="lnk_LastModfiedOn" runat="server" CommandArgument="tblCallTypenew.ModifiedDate"
                                                ValidationGroup="vgSearch" OnClick="lnk_Sort">Last Modified On</asp:LinkButton>
                                            <asp:Image ID="img_lnk_LastModfiedOn" Visible="false" runat="server" />
                                        </td>
                                        <td align="center" width="55px">
                                            <asp:LinkButton ID="lnk_Status" runat="server" CommandArgument="tblCallTypenew.isactive"
                                                ValidationGroup="vgSearch" OnClick="lnk_Sort">Status</asp:LinkButton>
                                            <asp:Image ID="img_lnk_Status" Visible="false" runat="server" />
                                        </td>
                                        <td align="center" width="50px" style="border-right: 1px solid #6398cc">
                                            Activity
                                            <%-- <div style="width: 50px; float: right;">
                                            </div>--%>
                                        </td>
                                    </tr>
                                    <tr id="tr" runat="server">
                                    </tr>
                                </table>
                            </LayoutTemplate>
                            <ItemTemplate>
                                <tr class='<%# Convert.ToBoolean(Container.DataItemIndex % 2) ? "EvenRowColor" : "OddRowColor" %>'>
                                    <td align="left" valign="middle">
                                        &nbsp;<%# Container.DataItemIndex+1 %>.
                                    </td>
                                    <td align="left">
                                        <asp:Label ID="lblDeptId" runat="server" Text='<%# Eval("ID")%>' Visible="false"></asp:Label>
                                        <%# Eval("Calltype")%>
                                    </td>
                                    <td align="left">
                                        <%# Eval("CreatedBy")%>
                                    </td>
                                    <td align="left">
                                        <%# Convert.ToDateTime(Eval("CreatedDate")).ToString("MMM dd, yyyy")%>
                                    </td>
                                    <td align="left">
                                        <%# Eval("ModifiedBy")%>
                                    </td>
                                    <td align="left">
                                        <%# Convert.ToDateTime(Eval("ModifiedDate")).ToString("MMM dd, yyyy")%>
                                    </td>
                                    <td align="center">
                                        <asp:Label ID="lbl_Status" runat="server" Style="display: none;" Text='<%# Eval("IsActive")%>'></asp:Label>
                                        <asp:ImageButton ID="imgbtnStatus" runat="server" CommandArgument='<%# Eval("id") %>'
                                            OnClick="imgbtnStatus_Onclick" />
                                    </td>
                                    <td class="last" align="center">
                                        <asp:Label ID="lblCallType" runat="server" Style="display: none;" Text='<%# Eval("Calltype")%>'></asp:Label>
                                        <asp:ImageButton ID="imgbtnEdit" runat="server" ImageUrl="~/App_Themes/ThemeNew/Images/edit.png"
                                            ToolTip="Edit Details" CommandArgument='<%# Eval("ID") %>' OnClick="imgbtnEdit_OnClick" />
                                        <asp:ImageButton ID="imgbtnDelete" runat="server" ImageUrl="~/App_Themes/ThemeNew/Images/delete.png"
                                            ToolTip="Delete" Style="display: none;" CommandArgument='<%# Eval("id") %>' OnClientClick="return confirm('Are you sure you want to delete the Call type?');"
                                            OnClick="imgbtnDelete_OnClick" />
                                    </td>
                                </tr>
                            </ItemTemplate>
                        </asp:ListView>

代码背后的代码是:

 protected void lnk_Sort(object sender, EventArgs e)
{
    LinkButton lnk = (LinkButton)sender;
    string arg = lnk.CommandArgument.ToString();
    ViewState["sortCol"] = arg;
    GetSortDirection();
    BindData(ViewState["sortCol"].ToString(), ViewState["sortDir"].ToString(), Convert.ToInt32(ViewState["nmbr"]), Pager.PageSize);
    string name = lnk.ID;
    Image img = (Image)(lst_CallType.FindControl("img_" + name));
    if (img != null)
    {
        SetSortOrderImage(img, ViewState["sortDir"].ToString());
    }
}

private void SetSortOrderImage(Image image, String sortorder)
{
    if (sortorder == "asc")
    {
        image.Visible = true;
        image.ImageUrl = "../App_Themes/ThemeNew2/images/up.png";
    }
    else if (sortorder == "Desc")
    {
        image.Visible = true;
        image.ImageUrl = "../App_Themes/ThemeNew2/images/down.png";

    }
}