ItemCommand事件中获取控件值

ItemCommand事件中获取控件值

本文介绍了如何在ListView.ItemCommand事件中获取控件值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello All



我有以下ListView



Hello All

I have the following ListView

<asp:ListView ID="lvPausedJobs" runat="server" EnableViewState="false" onitemcommand="lvPausedJobs_ItemCommand">
        <LayoutTemplate>
            <table border="0" cellpadding="5" width="600">
                <tr style="background-color:#E5E5FE">
                    <th align="left"><asp:Literal ID="Literal9" runat="server" EnableViewState="false">Job ID</asp:Literal></th>
                    <th align="left"><asp:Literal ID="Literal1" runat="server" EnableViewState="false">Name</asp:Literal></th>
                    <th align="left"><asp:Literal ID="Literal2" runat="server" EnableViewState="false">Job Number</asp:Literal></th>
                    <th align="left"><asp:Literal ID="Literal3" runat="server" EnableViewState="false">Task</asp:Literal></th>
                    <th align="left"><asp:Literal ID="Literal4" runat="server" EnableViewState="false">Start Time</asp:Literal></th>
                    <th align="left"><asp:Literal ID="Literal5" runat="server" EnableViewState="false">Pause Time</asp:Literal></th>
                    <th align="left"><asp:Literal ID="Literal6" runat="server" EnableViewState="false">Resume Time</asp:Literal></th>
                    <th align="left"><asp:Literal ID="Literal8" runat="server" EnableViewState="false">Resume</asp:Literal></th>
                </tr>
                <tr id="itemPlaceholder"  runat="server"></tr>
            </table>
        </LayoutTemplate>
        <ItemTemplate>
            <tr  runat="server">
                <td><asp:Label runat="server" ID="lblID"> <%# Eval("JobID") %> </asp:Label></td>
                <td><asp:Label runat="server" ID="lblName"> <%# Eval("CustomerName") %> </asp:Label></td>
                <td><asp:Label runat="server" ID="lblJob"> <%# Eval("JobNumber") %> </asp:Label></td>
                <td><asp:Label runat="server" ID="lblTask"> <%# Eval("Task") %> </asp:Label></td>
                <td><asp:Label runat="server" ID="lblStart"> <%# Eval("StartTime") %> </asp:Label></td>
                <td><asp:Label runat="server" ID="lblEnd"> <%# Eval("PauseTime") %> </asp:Label></td>
                <td><asp:Label runat="server" ID="lblDur"> <%# Eval("ResumeTime") %> </asp:Label></td>
                <td><asp:LinkButton ID="LinkButton1" runat="server" EnableViewState="false" CommandName="Resume" CausesValidation="false">Resume</asp:LinkButton></td>
            </tr>
        </ItemTemplate>
        <EmptyDataTemplate>
            <table border="0" cellpadding="5" width="600">
                <tr style="background-color:#E5E5FE" >
                    <th align="left"><asp:Literal ID="Literal9" runat="server" EnableViewState="false">Job ID</asp:Literal></th>
                    <th align="left"><asp:Literal ID="Literal1" runat="server" EnableViewState="false">Name</asp:Literal></th>
                    <th align="left"><asp:Literal ID="Literal2" runat="server" EnableViewState="false">Job Number</asp:Literal></th>
                    <th align="left"><asp:Literal ID="Literal3" runat="server" EnableViewState="false">Task</asp:Literal></th>
                    <th align="left"><asp:Literal ID="Literal4" runat="server" EnableViewState="false">Start Time</asp:Literal></th>
                    <th align="left"><asp:Literal ID="Literal5" runat="server" EnableViewState="false">Pause Time</asp:Literal></th>
                    <th align="left"><asp:Literal ID="Literal6" runat="server" EnableViewState="false">Resume Time</asp:Literal></th>
                </tr>
                <tr style="background-color:#EFEFEF">
                    <td colspan="7"><asp:Label runat="server" ID="lblName"> No Items to show </asp:Label></td>
                </tr>
            </table>
        </EmptyDataTemplate>
    </asp:ListView>





Now as you can see the link button has a command name. Now what is the correct way to get the controls text values during the ItemCommand event ? I have tried a lot of things like ListViewDataItem

ListView.DataKeys

FindControl



And I get one of two things, NullReferenceException or an empty string.



Thanks folks!



Now as you can see the link button has a command name. Now what is the correct way to get the controls text values during the ItemCommand event ? I have tried a lot of things like ListViewDataItem
ListView.DataKeys
FindControl

And I get one of two things, NullReferenceException or an empty string.

Thanks folks!

推荐答案

<asp:Label runat="server" ID="lblID"> <%# Eval("JobID") %> </asp:Label>



Do like...


Do like...

<asp:Label runat="server" ID="lblID" Text='<%# Eval("JobID") %>'></asp:Label>



Then inside the ItemCommand Event, you can code like below to get the Control.


Then inside the ItemCommand Event, you can code like below to get the Control.

Label LabelEmail = (Label)e.Item.FindControl("lblID");


这篇关于如何在ListView.ItemCommand事件中获取控件值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 23:39