如何访问listview单个项目. [英] How to access the listview individual item.

查看:49
本文介绍了如何访问listview单个项目.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在项目中使用ListView,并在ListView Item中添加了一个链接按钮.现在我想在linkbutton的ClickEvent的文本框中显示listView的项目数据.
因此,请告诉我如何访问ListView数据以及如何访问listview单个项目.

I am using a ListView in my project and i added a link button in ListView Item.Now i want to display the item data of listView in the text boxes on the ClickEvent of linkbutton.
So please tell me How to acces the ListView data and How to access the listview individual item.

<asp:ListView ID="lvEmployee" runat="server" OnSelectedIndexChanging="lvEmployee_SelectedIndexChanging">
                <LayoutTemplate>
                    <table>
                        <tr>
                            <th>
                                <asp:Label ID="lblLayoutEmployeeId" runat="server" Text="Employee Id"></asp:Label>
                            </th>
                            <th>
                                <asp:Label ID="lblLayoutEmployeeName" runat="server" Text="Employee Name"></asp:Label>
                            </th>
                            <th>
                                <asp:Label ID="lblLayoutContactNo" runat="server" Text="Contact No"></asp:Label>
                            </th>
                            <th>
                                <asp:Label ID="lblLayoutAddress" runat="server" Text="Address"></asp:Label>
                            </th>
                            <th>
                                <asp:Label ID="lblLayoutSalary" runat="server" Text="Salary"></asp:Label>
                            </th>
                        </tr>
                        <tbody id="itemPlaceHolder" runat="server">
                        </tbody>
                    </table>
                </LayoutTemplate>
                <ItemTemplate>
                    <tr>
                        <td>
                            <asp:Label ID="lblDataEmployeeId" runat="server"><%#Eval("Employee_ID")%></asp:Label>
                        </td>
                        <td>
                            <asp:LinkButton ID="btnDataEmployeeName" runat="server" CommandName="Select"><%#Eval("Employee_Name") %></asp:LinkButton>
                        </td>
                        <td>
                            <asp:Label ID="lblDataContactNo" runat="server"> <%#Eval("Contact_No") %></asp:Label>
                        </td>
                        <td>
                            <asp:Label ID="lblDataAddress" runat="server"><%#Eval("Address") %></asp:Label>
                        </td>
                        <td>
                            <asp:Label ID="lblDataSalary" runat="server"><%#Eval("Salary") %></asp:Label>
                        </td>
                    </tr>
                </ItemTemplate>
</asp:ListView>

推荐答案

好吧,您的UI设计实现对我来说还不太清楚,但是您似乎拥有标题并控制该标题以获取数据-全部在列表视图中.

如果任何时候需要访问控件,则需要使用在列表视图的行级别公开的FindControl方法.
对于任何listview事件,都可以获取rowItem,然后使用Findcontrol获取控件,例如:
Well, your UI design implementation is not too clear to me but it looks like you have caption and control for that caption to get data - all in a listview.

If at any time you need to access a control, you need to use FindControl method exposed at the row level of your listview.
For any listview event, you can get the rowItem and then use Findcontrol to get the control, sample:
protected void ContactsListView_ItemCreated(object sender, ListViewItemEventArgs e)
  {
    // Retrieve the current item.
    ListViewItem item = e.Item;

    // Verify if the item is a data item.
    if (item.ItemType == ListViewItemType.DataItem)
    {
      // Get the EmailAddressLabel Label control in the item.
      Label EmailAddressLabel = (Label)item.FindControl("EmailAddressLabel");

      // Display the e-mail address in italics.
      EmailAddressLabel.Font.Italic = true;
    }
  }


参考: MSDN:ListViewItem [


Refer: MSDN: ListViewItem[^]


我正在使用以下代码,它给出了是我准确的结果...

受保护的void lvEmployee_SelectedIndexChanging(对象发送者,ListViewSelectEventArgs e)
{
DataTable表=(DataTable)(Session ["EmployeeDB"]));
int Emp_Id = Convert.ToInt32(lvEmployee.DataKeys [e.NewSelectedIndex] .Value);
DataRow行= table.Rows.Find(Emp_Id);
txt.txt.txt = row ["Employee_Id"].ToString();
txt.txt.txt = row ["Employee_Name"].ToString();
txtContactNo.Text = row ["Contact_No"].ToString();
txtAddress.Text = row ["Address"].ToString();
txtSalary.Text = row ["Salary"].ToString();
btnAdd.Text =更新";
}
I am using following code and it give''s me accurate result......

protected void lvEmployee_SelectedIndexChanging(object sender, ListViewSelectEventArgs e)
{
DataTable table = (DataTable)(Session["EmployeeDB"]);
int Emp_Id =Convert.ToInt32(lvEmployee.DataKeys[e.NewSelectedIndex].Value);
DataRow row = table.Rows.Find(Emp_Id);
txtEmployeeId.Text = row["Employee_Id"].ToString();
txtEmployeeName.Text = row["Employee_Name"].ToString();
txtContactNo.Text = row["Contact_No"].ToString();
txtAddress.Text = row["Address"].ToString();
txtSalary.Text = row["Salary"].ToString();
btnAdd.Text = "Update";
}


这篇关于如何访问listview单个项目.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆