protected void Page_Load(object sender, EventArgs e) { DataTable dt = new DataTable(); //For Fill Data in DataTable dt = clsemp.GetData(); //For DataList DataList1.DataSource = dt; DataList1.DataBind(); //for GridView GridView1.DataSource = dt; GridView1.DataBind(); //for repeater Repeater1.DataSource = dt; Repeater1.DataBind(); } protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { if (e.Item.DataItem != null) { int intEmployeeId = Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "EmployeeId")); string strFName = Convert.ToString(DataBinder.Eval(e.Item.DataItem, "FirstName")); string strLName = Convert.ToString(DataBinder.Eval(e.Item.DataItem, "LastName")); //work do as per require } } } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { if (e.Row.DataItem != null) { int intEmployeeId = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "EmployeeId")); string strFName = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "FirstName")); string strLName = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "LastName")); //work do as per require } } } protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { if (e.Item.DataItem != null) { int intEmployeeId = Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "EmployeeId")); string strFName = Convert.ToString(DataBinder.Eval(e.Item.DataItem, "FirstName")); string strLName = Convert.ToString(DataBinder.Eval(e.Item.DataItem, "LastName")); //work do as per require } } }
Monday, November 15, 2010
get items data at run time using the ItemDataBound, RowDataBound event
Tuesday, November 9, 2010
Paging ListView With DataPager
<asp:ListView ID="EmpListView" runat="server"> <LayoutTemplate> <tr> <td align="center">Employee Data</td> </tr> </LayoutTemplate> <ItemTemplate> <tr> <td> <br /> Employee Id: <b><%# Eval("employeeId") %>'></b> <br /><br /> Employee Name:: <b><%# Eval("FirstName") %>'> <%# Eval("LastName") %>'></b> <br /> </td> </tr> </ItemTemplate> </asp:ListView> </table> <br /> <asp:DataPager ID="PagerRow" runat="server" PagedControlID="EmpListView" OnPreRender="EmpList_PreRender"> <Fields> <asp:TemplatePagerField> <PagerTemplate> Page <asp:Label runat="server" ID="lbCurrentPage" Text="<%# Container.TotalRowCount>0 ? (Container.StartRowIndex / Container.PageSize) + 1 : 0 %>" /> of <asp:Label runat="server" ID="lbTotalPages" Text="<%# Math.Ceiling ((double)Container.TotalRowCount / Container.PageSize) %>" /> (Total: <asp:Label runat="server" ID="lbTotalItems" Text="<%# Container.TotalRowCount%>" /> records) <br /><br /> </PagerTemplate> </asp:TemplatePagerField> <asp:NextPreviousPagerField ButtonType ="Button" ShowFirstPageButton="True" ShowPreviousPageButton="True" ShowNextPageButton="True" ShowLastPageButton="True" /> <asp:TemplatePagerField OnPagerCommand="PagerNextPrevious_OnPagerCommand"> <PagerTemplate> <br /><br /> <asp:LinkButton ID="lbtnFirst" runat="server" CommandName="First" Text="First" Visible='<%# Container.StartRowIndex > 0 %>' /> <asp:LinkButton ID="lbtnPrevious" runat="server" CommandName="Previous" Text='<%# (Container.StartRowIndex - Container.PageSize + 1) + " - " + (Container.StartRowIndex) %>' Visible='<%# Container.StartRowIndex > 0 %>' /> <asp:Label ID="lbtnCurrent" runat="server" Text='<%# (Container.StartRowIndex + 1) + "-" + (Container.StartRowIndex + Container.PageSize > Container.TotalRowCount ? Container.TotalRowCount : Container.StartRowIndex + Container.PageSize) %>' /> <asp:LinkButton ID="lbtnNext" runat="server" CommandName="Next" Text='<%# (Container.StartRowIndex + Container.PageSize + 1) + " - " + (Container.StartRowIndex + Container.PageSize*2 > Container.TotalRowCount ? Container.TotalRowCount : Container.StartRowIndex + Container.PageSize*2) %>' Visible='<%# (Container.StartRowIndex + Container.PageSize) < Container.TotalRowCount %>' /> <asp:LinkButton ID="lbtnLast" runat="server" CommandName="Last" Text="Last" Visible='<%# (Container.StartRowIndex + Container.PageSize) < Container.TotalRowCount %>' /> </PagerTemplate> </asp:TemplatePagerField> <asp:TemplatePagerField OnPagerCommand = "PagerNo_OnPagerCommand"> <PagerTemplate> <br /><br /> <asp:TextBox ID="txtPageNo" runat="server" Width="30px" onKeyUp = "value = value.replace(/[^0-9]/g,'')" Text="<%# Container.TotalRowCount>0 ? (Container.StartRowIndex / Container.PageSize) + 1 : 0 %>" ></asp:TextBox> <asp:Button ID="btnsubmit" runat="server" Text="Go" /> </PagerTemplate> </asp:TemplatePagerField> </Fields> </asp:DataPager>
private void DataBindList() { connectionlayer dl = new connectionlayer(); DataSet emp = new DataSet(); dl.LoadDataSet(CommandType.Text, "SELECT employeeId,LastName,FirstName FROM Employees", ref emp, "emp"); EmpListView.DataSource = emp; EmpListView.DataBind(); } protected void PagerNextPrevious_OnPagerCommand(object sender, DataPagerCommandEventArgs e) { switch (e.CommandName) { case "First": e.NewStartRowIndex = 0; e.NewMaximumRows = e.Item.Pager.MaximumRows; break; case "Previous": e.NewStartRowIndex = e.Item.Pager.StartRowIndex - e.Item.Pager.PageSize; e.NewMaximumRows = e.Item.Pager.MaximumRows; break; case "Next": int NewIndexNo = e.Item.Pager.StartRowIndex + e.Item.Pager.PageSize; if (NewIndexNo <= e.TotalRowCount) { e.NewStartRowIndex = NewIndexNo; e.NewMaximumRows = e.Item.Pager.MaximumRows; } break; case "Last": e.NewStartRowIndex = e.TotalRowCount - e.TotalRowCount % e.Item.Pager.PageSize; e.NewMaximumRows = e.Item.Pager.MaximumRows; break; } } protected void PagerNo_OnPagerCommand(object sender, DataPagerCommandEventArgs e) { int NewPageNo = Convert.ToInt32((e.Item.FindControl("txtPageNo") as TextBox).Text); int NewIndexNo = e.Item.Pager.PageSize * (NewPageNo - 1); if (NewIndexNo <= e.TotalRowCount) { e.NewStartRowIndex = NewIndexNo; e.NewMaximumRows = e.Item.Pager.MaximumRows; } } protected void EmpList_PreRender(object sender, EventArgs e) { DataBindList(); PagerRow.PageSize = 5; }
Subscribe to:
Posts
(
Atom
)