How to get value from a listview Asp.Net -
i have listview in asp.net this:
<asp:listview id="lvloads" datakeynames="bookingid" onitemcommand="lvloads_onitemcommand" runat="server"> <emptydatatemplate> <table> <tr> <td>no data returned.</td> </tr> </table> </emptydatatemplate> <grouptemplate> <asp:placeholder id="itemplaceholder" runat="server"></asp:placeholder> </grouptemplate> <itemtemplate> <fieldset> <table class="table table-striped table-responsive"> <thead> <tr class="hidden-lg"> <th width="15%">type</th> <th width="31%">details</th> <th width="25%">distance/duration</th> <th width="21%"> </th> </tr> </thead> <tbody> <tr> <td width="15%"> <asp:label cssclass="hide" id="lblbokingid" runat="server"><%# eval("bookingid") %></asp:label> <asp:label id="lblloadtypename" runat="server"> <%# eval("loadtypename") %></asp:label> <br> </td> <td width="21%"> <asp:linkbutton id="btnviewmore" cssclass="btn btn-warning" commandname="viewmore" runat="server">view more</asp:linkbutton> </td> </tr> </tbody> </table> </fieldset> </itemtemplate> </asp:listview>
now on click of button viewmore wrote code extract listviewitem's value:
var id = ((label)e.item.findcontrol("lblbookingid")).text; var name = ((label)e.item.findcontrol("lblloadtypename")).text;
but e.item.findcontrol("lblbookingid")
comes null. checked page's html using inspect element of chrome, , shocked see id somehow got changed contentsection_ctl00_lblloadtypename_0
i stuck @ this. please me. thanx in advance
i had done work list view , 1 had created resemble problem.
default2.aspx:
<%@ page language="c#" autoeventwireup="true" codefile="default2.aspx.cs" inherits="default2" %> <!doctype html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <div> working example <asp:listview id="lvloads" runat="server"> <layouttemplate> <table border="0" cellpadding="1"> <tr style="background-color: #e5e5fe"> <th align="left">employeeid </th> <th align="left">lastname </th> <th align="left"></th> </tr> <tr runat="server" id="itemplaceholder" /> </table> </layouttemplate> <itemtemplate> <tr id="tr1" runat="server"> <td id="td1" runat="server"> <asp:label id="lblcustomerid" runat="server" text='<%# eval("bookingid") %>'></asp:label> </td> <td id="td2" runat="server"> <asp:label id="lbllastname" runat="server" text='<%# eval("loadtypename") %>'></asp:label> </td> <td id="td3" runat="server"> <asp:linkbutton id="lnkselect" runat="server" onclick="btnviewmore_click" text="select" /> </td> </tr> </itemtemplate> </asp:listview> </div> <div> error <asp:listview id="listview1" datakeynames="bookingid" onitemcommand="lvloads_onitemcommand" runat="server"> <emptydatatemplate> <table> <tr> <td>no data returned.</td> </tr> </table> </emptydatatemplate> <grouptemplate> <asp:placeholder id="itemplaceholder" runat="server"></asp:placeholder> </grouptemplate> <itemtemplate> <fieldset> <table class="table table-striped table-responsive"> <thead> <tr class="hidden-lg"> <th width="15%">type</th> <th width="31%">details</th> <th width="25%">distance/duration</th> <th width="21%"> </th> </tr> </thead> <tbody> <tr> <td width="15%"> <asp:label cssclass="hide" id="lblbokingid" runat="server"><%# eval("bookingid") %></asp:label> <asp:label id="lblloadtypename" runat="server"> <%# eval("loadtypename") %></asp:label> <br> </td> <td width="21%"> <asp:linkbutton id="btnviewmore" cssclass="btn btn-warning" onclick="btnviewmore_click1" runat="server">view more</asp:linkbutton> </td> </tr> </tbody> </table> </fieldset> </itemtemplate> </asp:listview> </div> </div> </form>
default2.aspx.cs:
public partial class default2 : system.web.ui.page { protected void page_load(object sender, eventargs e) { class1 obj = new class1(); lvloads.datasource = obj.bind(); lvloads.databind(); listview1.datasource = obj.bind(); listview1.databind(); } protected void lvloads_onitemcommand(object sender, listviewcommandeventargs e) { } protected void btnviewmore_click(object sender, eventargs e) { linkbutton btn = sender linkbutton; listviewdataitem item = (listviewdataitem)(sender control).namingcontainer; label lblstatus = (label)item.findcontrol("lbllastname"); string message = lblstatus.text; } protected void btnviewmore_click1(object sender, eventargs e) { linkbutton btn = sender linkbutton; listviewdataitem item = (listviewdataitem)(sender control).namingcontainer; label lblstatus = (label)item.findcontrol("lblloadtypename"); string message = lblstatus.text; } }
class1:
public class class1 { public int bookingid { get; set; } public string loadtypename { get; set; } public class1() { } public list<class1> bind() { list<class1> data = new list<class1>(); class1 obj = new class1(); obj.bookingid = 1; obj.loadtypename = "type name1"; data.add(obj); obj = new class1(); obj.bookingid = 2; obj.loadtypename = "type name2"; data.add(obj); obj = new class1(); obj.bookingid = 3; obj.loadtypename = "type name3"; data.add(obj); obj = new class1(); obj.bookingid = 4; obj.loadtypename = "type name4"; data.add(obj); return data; } }
as can see in both list view there problem related design using in list view. please change design per requirement.
you placing control in further other controls that's why id change @ run time.
hope you.
Comments
Post a Comment