Repeater control usage notes in asp.net

Source: Internet
Author: User
Tags eval first row

We may all be familiar with the DataGrid, but if we have a large amount of data, we have to consider using repeater as our data-bound control. The main difference between a repeater control and a DataGrid (and DataList) control is how HTML is handled. Asp. NET creates HTML code to display the DataGrid control, but repeater allows developers to decide how to display the data. So, you can choose to display the data in an HTML table or in a sequential list. This depends largely on your choice, but you must insert the correct HTML into the ASP. NET page.


Templates are the same as DataList, and the Repeater control supports only templates. The following templates are available to choose from:


AlternatingItemTemplate: Specifies how each additional option is displayed.


ItemTemplate: Specifies how options are displayed. (AlternatingItemTemplate can overwrite this template.) )


HeaderTemplate: Establishes how the caption is displayed.


FooterTemplate: Create how to display the footer.


SeparatorTemplate: Specifies how to display the separator between different options.


You can use these templates to display the data you want. The only mandatory template is ItemTemplate, and all other templates are selective.


For processing a data source, the Repeater control has the same properties as the DataGrid and DataList:





DataMember: Gets or sets the table of the corresponding DataSource property that is bound to the Repeater control.





DataSource: Gets or sets the data source that provides the data for the Repeater display.





In addition to this, there is an items attribute that you can programmatically access to a single option in repeater data. It returns a Repeateritemcollection object, a set of RepeaterItem objects representing each row of the Repeater data.





asp.net Web Data controls have another common denominator: they all use the DataBind method to generate the user interface. Call this method to return and display the data (assuming the DataSource and DataMember properties are set correctly). Before viewing the DataBind method, let's look at how to use a Repeater control in a Web page.


Using the Repeater control


The first step in using the Repeater control is to determine which data source and field we will use. For example, we will use the Employees list in the SQL Server Northwind database. The Web page displays the full name, address, and phone number of the employee. HTML uses div tags to separate the content with the Repeater template. The following is the HTML content of the Web page:


Front Code

The code is as follows Copy Code

<asp:repeater id= "Repeater1" runat= "Server" >


<headertemplate><table cellpadding= "0" cellspacing= "0" border= "1" align= "center" class= "Auto-style1" >


<tr align= "Left" >


<th> number </th>


<th> name </th>


<th> Age </th>


<th> class </th>


<th> pocket Money </th>


</tr>


</HeaderTemplate>


<ItemTemplate>


<tr >


<td><% #Eval ("id")%></td>


<td><% #Eval ("name")%></td>


<td><% #Eval ("Age")%></td>


<td><% #Eval ("ClassID")%></td>


<td><% #Eval ("salary")%></td>


</tr>


</ItemTemplate>


<FooterTemplate></table></FooterTemplate>


</asp:Repeater>

Background code

The code is as follows Copy Code
String sql = string. Format ("SELECT * from person");
DataTable dt = sqlhelper.helper.ExecuteQuery (SQL);
Repeater1.datasource = DT;
Repeater1.databind ();

How to use Aspnetpager:

First Register <%@ Register assembly= "Aspnetpager" namespace= "Wuqi.webdiyer" tagprefix= "Webdiyer"%>

Front desk:

The code is as follows Copy Code
&lt;webdiyer:aspnetpager id= "AspNetPager1" runat= "custominfohtml="%currentpageindex% page, total%pagecount% page, per page% Pagesize% Bar "


firstpagetext= "Home" lastpagetext= "last" layouttype= "Table" nextpagetext= "Next Page"


onpagechanging= "aspnetpager1_pagechanging" pageindexboxtype= "DropDownList"


Pagingbuttonlayouttype= "Span" prevpagetext= "prev" showcustominfosection= "left"


showpageindexbox= "Always" submitbuttontext= "Go" textafterpageindexbox= "page"


Textbeforepageindexbox= "Go to" width= "450px" pagesize= "3" &gt;


&lt;/webdiyer:AspNetPager&gt;

Background:

The code is as follows Copy Code
private void Addpages (Repeater rpt,wuqi.webdiyer.aspnetpager anp,datatable DT)


{


PagedDataSource PDS = new PagedDataSource ();


Pds. AllowPaging = true;


Pds. DataSource = dt. DefaultView;





Anp. RecordCount = dt. rows.count;//assign the current total to a paging control


Pds. PageSize = ANP. pagesize;//to determine pagination, each page has n items


Pds. CurrentPageIndex = ANP. currentpageindex-1;//determine the current number of pages





Rpt. DataSource = pds;//bound data source


Rpt. DataBind ();


}


protected void Aspnetpager1_pagechanging (Object src, Wuqi.Webdiyer.PageChangingEventArgs e)


{


Aspnetpager1.currentpageindex = E.newpageindex;


Bindpersondata ();


}


Example

Repeater control to add and delete a row dynamically

The code is as follows Copy Code

&lt;script type= "Text/javascript" &gt;


var txtequipmentidsidarray = new Array ();


function Openwindow (clientId, typeID, Reqnum) {


var url = ".. /equipmentissue/equipmentrequestissue.aspx? Typeid= "+ typeid +" &amp;reqnum= "+ reqnum;


var widths = 600;


var heigths = 450;


var winpar = window.showmodaldialog (url, window, ' dialogwidth= ' + widths + ' px;dialogheight= ' + heigths + ' Px;status=no;ce Nter=yes;scroll=no;help:no; ');


if (winpar!= undefined) {


var txtequipmentids = document.getElementById (clientId);


Txtequipmentids.value = Winpar;

To approve the equipmentid of a release in a user control


var isexistequipmentidsid = false;


for (var i = 0; i &lt; txtequipmentidsidarray.length; i++) {


if (txtequipmentidsidarray[i] = = clientId) {


Isexistequipmentidsid = true;


//                    }


//                }


if (Isexistequipmentidsid = = False) {


Txtequipmentidsidarray.push (CLIENTID);


//                }


var tempequipmentid = "";


for (var i = 0; i &lt; txtequipmentidsidarray.length; i++) {


Tempequipmentid = Tempequipmentid + document.getElementById (txtequipmentidsidarray[i)). Value;


var hiddenequipmentids = document.getElementById ("Uc_approvalaction1_hfequipmentids");


Hiddenequipmentids.value = Tempequipmentid;


//                }


alert (Hiddenequipmentids.value);


}

var tempreturnvalue = "";


var tb_request = document.getElementById ("Tb_request");


var tr = tb_request.getelementsbytagname ("tr");


for (var i = 0; i &lt; tr.length; i++) {


if (tr[i].id!= "") {


var span_equrequestitemid = Tr[i].getelementsbytagname ("span"); Get the Idequrequestitemid of the application details


var select_ddlstation; Get use of workstation Stationid


var option = Tr[i].getelementsbytagname ("select") [1].getelementsbytagname ("option");


for (var j = 0; J &lt; Option.length; J + +) {


if (option[j].selected)


{


Select_ddlstation = Option[j]


}


}


var textarea_equipmentids = tr[i].getelementsbytagname ("textarea"); Obtain the issued asset number Equipmentno


Tempreturnvalue = tempreturnvalue + Span_equrequestitemid[0].innertext + ":" + Select_ddlstation.value + ":" + textarea_Eq Uipmentids[0].innertext + "|";


}


}


var hiddenequipmentids = document.getElementById ("Uc_approvalaction1_hfequipmentids");


Hiddenequipmentids.value = Tempreturnvalue;


alert (Hiddenequipmentids.value);





}


&lt;/script&gt;

Repeater:

The code is as follows Copy Code

&lt;div id= "Div_repeater" &gt;


&lt;asp:hiddenfield id= "Hfrptcolumns" runat= "value=" guid,equrequestitemid,equipmenttype,station, Equipmentnum,equipmentids "/&gt;


&lt;table id= "Tb_request" cellpadding= "1" "cellspacing=" 0 "width=" 100% "style=" Background-color: #DFE8F6; font-size:12px; padding:10px; " &gt;


&lt;thead&gt;


&lt;tr&gt;


&lt;th&gt; Serial Number &lt;/th&gt;


&lt;th&gt; Detail Number &lt;/th&gt;


&lt;th&gt;


Asset type


&lt;/th&gt;


&lt;th&gt;


Use station


&lt;/th&gt;


&lt;th&gt;


Number of applications


&lt;/th&gt;


&lt;th&gt;


Issued Asset number &lt;font color= "Red" &gt; (completed by asset manager) &lt;/font&gt;


&lt;/th&gt;


&lt;/tr&gt;


&lt;/thead&gt;


&lt;tbody&gt;


&lt;asp:repeater id= "rptrequest" runat= "Server" onitemcommand= "Rptrequest_itemcommand"


onitemdatabound= "Rptrequest_itemdatabound" &gt;


&lt;ItemTemplate&gt;


&lt;tr id= "Tr_request" &gt;


&lt;td&gt;


&lt;%# container.itemindex+1%&gt;


&lt;asp:label id= "Lblguid" runat= "server" text= ' &lt;% #Eval ("Guid")%&gt; ' visible= ' false ' &gt;&lt;/asp:Label&gt;


&lt;/td&gt;


&lt;td&gt;&lt;asp:label id= "Lblequrequestitemid" runat= "server" text= ' &lt;% #Eval ("Equrequestitemid")%&gt; ' &gt; &lt;/asp:Label&gt;&lt;/td&gt;


&lt;td&gt;


&lt;asp:dropdownlist id= "Ddlequipmenttype" runat= "Server" &gt;&lt;/asp:DropDownList&gt;


&lt;asp:label id= "Lblequipmenttype" runat= "server" text= ' &lt;% #Eval ("Equipmenttype")%&gt; ' visible= ' false ' &gt;&lt; /asp:label&gt;


&lt;/td&gt;


&lt;td&gt;


&lt;asp:dropdownlist id= "ddlstation" runat= "Server" &gt;&lt;/asp:DropDownList&gt;


&lt;asp:label id= "lblstation" runat= "text= ' &lt;% #Eval (" station ")%&gt; ' visible= ' false ' &gt;&lt;/asp:label &gt;


&lt;/td&gt;


&lt;td&gt;&lt;asp:textbox id= "Txtreqequipmentnum" runat= "server" text= ' &lt;% #Eval ("Equipmentnum")%&gt; ' &gt;&lt;/ Asp:textbox&gt;&lt;/td&gt;


&lt;td&gt;&lt;asp:textbox id= "Txtequipmentids" runat= "server" text= ' &lt;% #Eval ("Equipmentids")%&gt; ' TextMode= ' MultiLine "&gt;&lt;/asp:TextBox&gt;&lt;/td&gt;


&lt;td&gt;&lt;asp:button id= "Btnaddrow" runat= "Server" text= "New Row" commandname= "Add"/&gt;


&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;asp:button id= "Btndeleterow" runat= "server" Text= "delete Bank" CommandName= " Delete "/&gt;&lt;/td&gt;


&lt;/tr&gt;


&lt;/ItemTemplate&gt;


&lt;/asp:Repeater&gt;


&lt;/tbody&gt;


&lt;/table&gt;


&lt;/div&gt;

Background:

The code is as follows Copy Code

&lt;summary&gt;


Data source for binding repeater


&lt;/summary&gt;


private void Repeaterbinddata ()


{


DataTable dt = Definedatatableschema (Hfrptcolumns.value);


if (request["businessno"] = = null)


{


LoadData (DT);


}


Else


{


LoadData (request["Businessno"). ToString (), DT);


}

Rptrequest.datasource = DT;
Rptrequest.databind ();
}


private void LoadData (string businessno, DataTable DT)
{
String strSQL = "SELECT * from Ems_equipmentrequestitem where Bussinessno = '" + Businessno + "'";
DataTable Dt_equipmentrequestitem = DBUtility.DbHelperSQL.Query (strSQL). Tables[0];

for (int i = 0; i &lt; Dt_EquipmentRequestItem.Rows.Count; i++)


foreach (DataRow Dr in Dt_equipmentrequestitem.rows)


{


DataRow row = dt. NewRow ();


row["Guid" = Guid.NewGuid ();


row["Equrequestitemid"] = dr["Equrequestitemid"]. ToString ();


row["Equipmenttype"] = dr["typeID"]. ToString ();


row["station"] = dr["Stationid"]. ToString ();


row["Equipmentnum"] = dr["Equipmentnum"]. ToString ();


row["equipmentids"] = dr["Equipmentids"]. ToString ();


Dt. Rows.Add (row);


}


}


       ///<summary>
        ///repeater data is loaded by default
       ///</summary>
        ///<param name= "DT" ></PARAM>
        private void LoadData (DataTable dt)
        {
            //default display 1 lines
             for (int i = 0; i < 1; i++)
            {
& nbsp;               DataRow row = dt. NewRow ();
                dt. Rows.Add (row);
           }

Load some data for the first row
DataRow row0 = dt. Rows[0];
row0["Guid" = Guid.NewGuid ();
row0["Equrequestitemid"] = "";
row0["Equipmenttype"] = "";
row0["station"] = "";
row0["Equipmentnum"] = "1";//default initial is 1
row0["equipmentids"] = "";
}

&lt;summary&gt;


Defines the schema of the data source DataTable based on the column name corresponding to the repeater


&lt;/summary&gt;


&lt;param name= "Columns" &gt; Column name &lt;/param&gt;


&lt;returns&gt;&lt;/returns&gt;


Public DataTable Definedatatableschema (string columns)


{


DataTable dt = new DataTable ();


string[] columnsary = columns. Split (', ');


foreach (String str in columnsary)


{


Dt. Columns.Add (str);


}


return DT;


}

protected void Rptrequest_itemcommand (object source, RepeaterCommandEventArgs e)
{
if (E.commandname = = "Add")
{
System.Web.UI.WebControls.Label Lblguid = (System.Web.UI.WebControls.Label) e.item.findcontrol ("Lblguid");

First, recover the data source


DataTable dt = Definedatatableschema (Hfrptcolumns.value);


foreach (RepeaterItem item in Rptrequest.items)


{


DataRow newrow = dt. NewRow ();


newrow["Guid" = (System.Web.UI.WebControls.Label) item. FindControl ("Lblguid")). Text;


newrow["Equrequestitemid" = (System.Web.UI.WebControls.Label) item. FindControl ("Lblequrequestitemid")). Text;


newrow["Equipmenttype" = (DropDownList) item. FindControl ("Ddlequipmenttype")). SelectedValue;


newrow["Station" = (DropDownList) item. FindControl ("Ddlstation")). SelectedValue;


newrow["Equipmentnum"] = ((TextBox) item. FindControl ("Txtreqequipmentnum")). Text;


newrow["Equipmentids"] = ((TextBox) item. FindControl ("Txtequipmentids")). Text;


Dt. Rows.Add (NewRow);

if (Lblguid.text = = (System.Web.UI.WebControls.Label) item. FindControl ("Lblguid")). Text)


{


Add a row


DataRow row = dt. NewRow ();


row["Guid" = Guid.NewGuid ();


row["Equipmenttype"] = "";


row["station"] = "";


row["Equipmentnum"] = "1";//default initial is 1


row["equipmentids"] = "";


Dt. Rows.Add (row);


}


}

Rptrequest.datasource = DT;
Rptrequest.databind ();
}
else if (e.commandname = "Delete")
{
System.Web.UI.WebControls.Label Lblguid = (System.Web.UI.WebControls.Label) e.item.findcontrol ("Lblguid");

First, recover the data source


DataTable dt = Definedatatableschema (Hfrptcolumns.value);


foreach (RepeaterItem item in Rptrequest.items)


{


if (Lblguid.text!= (System.Web.UI.WebControls.Label) item. FindControl ("Lblguid")). Text)


{


DataRow newrow = dt. NewRow ();


newrow["Guid" = (System.Web.UI.WebControls.Label) item. FindControl ("Lblguid")). Text;


newrow["Equrequestitemid" = (System.Web.UI.WebControls.Label) item. FindControl ("Lblequrequestitemid")). Text;


newrow["Equipmenttype" = (DropDownList) item. FindControl ("Ddlequipmenttype")). SelectedValue;


newrow["Station" = (DropDownList) item. FindControl ("Ddlstation")). SelectedValue;


newrow["Equipmentnum"] = ((TextBox) item. FindControl ("Txtreqequipmentnum")). Text;


newrow["Equipmentids"] = ((TextBox) item. FindControl ("Txtequipmentids")). Text;


Dt. Rows.Add (NewRow);


}


}

Rptrequest.datasource = DT;
Rptrequest.databind ();
}
}

protected void Rptrequest_itemdatabound (object sender, RepeaterItemEventArgs e)
{
if (E.item.itemtype = = ListItemType.Item | | e.item.itemtype = = listitemtype.alternatingitem)
{
DropDownList ddlequipmenttype_temp = E.item.findcontrol ("Ddlequipmenttype") as DropDownList;

String sqlstr = @ "SELECT distinct B.typeid,b.equipmentname + ' _ ' + b.equipmenttype as ' Equipmentname_equipmenttype '


From Ems_equipmentinfo A


Join Ems_equipmenttype b on a.typeid = B.typeid and b.status = ' 1 '


ORDER by Equipmentname_equipmenttype ";


DataSet ds = DBUtility.DbHelperSQL.Query (SQLSTR);


Ddlequipmenttype_temp. DataTextField = "Equipmentname_equipmenttype";


Ddlequipmenttype_temp. DataValueField = "typeID";


Ddlequipmenttype_temp. DataSource = ds;


Ddlequipmenttype_temp. DataBind ();


Ddlequipmenttype_temp. SelectedValue = (E.item.findcontrol ("Lblequipmenttype") as System.Web.UI.WebControls.Label). Text;


DropDownList ddlstation_temp = E.item.findcontrol ("Ddlstation") as DropDownList;

String sqlstr2 = @ "Select Nodeid,line + ' _ ' + stationname as ' line_stationname ' from v_stations where status= ' 1 ' Line_stationname ";


DataSet ds2 = DBUtility.DbHelperSQL.Query (SQLSTR2);


Ddlstation_temp. DataTextField = "Line_stationname";


Ddlstation_temp. DataValueField = "NodeId";


Ddlstation_temp. DataSource = DS2;


Ddlstation_temp. DataBind ();


Ddlstation_temp. SelectedValue = (E.item.findcontrol ("Lblstation") as System.Web.UI.WebControls.Label). Text;




TextBox txtequids = E.item.findcontrol ("Txtequipmentids") as TextBox;


TXTEQUIDS.ATTRIBUTES.ADD ("ReadOnly", "true");





if (request["Businessno"]!= null)


{


(E.item.findcontrol ("Btnaddrow") as System.Web.UI.WebControls.Button). Visible = false;


(E.item.findcontrol ("Btndeleterow") as System.Web.UI.WebControls.Button). Visible = false;

String Businessno = request["Businessno"]. ToString ();


BOOL Isapplyuser = Is_applyuser (session["Currentuserid"). ToString (), Businessno, "Ems_equipmentrequest", "requester")/whether the applicant himself


if (Isapplyuser = = True)


{


String requeststatus = Httputility.urldecode (request["RequestStatus"). ToString (), System.Text.Encoding.UTF8);//The current status of the requisition form


if (RequestStatus = "Draft" | | requeststatus = = "returned")


{


(E.item.findcontrol ("Btnaddrow") as System.Web.UI.WebControls.Button). Visible = true;


(E.item.findcontrol ("Btndeleterow") as System.Web.UI.WebControls.Button). Visible = true;


}


}


Else


{


BOOL Iscurrentappprovaler = Is_currentappprovaler (session["Currentuserid"). ToString (), businessno);//is the person currently able to approve


if (Iscurrentappprovaler = = True)


{


String sql = "SELECT top 1 layername from Ems_approvaler where bussinessno= '" + Businessno + "" "


+ "and approvalstatus= ' N ' ORDER by appsequence";


DataTable dt = DBUtility.DbHelperSQL.Query (SQL). Tables[0];

if (dt. Rows.Count!= 0 &amp;&amp; dt. rows[0]["LayerName"]. ToString () = "asset manager")


{


String reqnum = (E.item.findcontrol ("Txtreqequipmentnum") as TextBox). Text.trim ();


TXTEQUIDS.ATTRIBUTES.ADD ("onclick", "Openwindow" (' + Txtequids.clientid + "', '" + ddlequipmenttype_temp.) SelectedValue + "', '" + Reqnum + "");


}


}


}





}

}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.