Use of Custom button controls in datalist and repeater

Source: Internet
Author: User
Learning content:Create an event handler for itemcommand eventCodeAssociated with clicking a button.

Datalist and repeater can contain many buttons, linkbuttons, and imagebuttons in templates. When these buttons are hit, they will cause PostBack and trigger the itemcommand event. Create an event handler for the itemcommand event to associate the server code with the click button.In this event handler, first check the input commandname value to determine which button is clicked. Other information can be provided through the commandargument attribute. // The commandeventargs class has two common attributes: commandname (used to obtain the command name) and commandargument (used to obtain command parameters ).

At any time, when the buttons, linkbuttons, and imagebutton in the template of datalist or repeater are hit, PostBack is generated and the itemcommand event of datalist or repeater is triggered. Except itemcommand, if the commandname of the button is set("Delete", "edit", "cancel", "Update", "select ")When one of them is used, datalist will trigger another event. However, itemcommand is always triggered.

When a button in the template of datalist or repeater is clicked, you usually need to obtain which button is clicked (a control may contain multiple buttons, such as editing and deleting ), you may also need some other information (for examplePrimary Key of item).Buttons, linkbuttons, and imagebutton provide two attributes. Their values can be passed to itemcommand event handler:

1. commandname-indicates the string of each button identity in the template.
2. commandargument-usually used to save some values, such as primary keys.

 

Configure objectdatasource
Add a datalist or repeater, and add the objectdatasource of categoriesdatasource as the data source. Use the getcategories () method of the categoriesbll class to configure it. Visual Studio creates a default itemtemplate for datalist based on the data source, and the templates of repeater needs to be manually defined.

Run the server code when clicking "show products" linkbutton.
Set the commandname of the linkbutton to "showproducts" and bind the primary key-categoryid-of the current recordCommandargument. (Categoryargument = '<% # eval ("categoryid") %>'// In each itemCategoryid is assigned to categoryargument in one way.)

When a button is hit, a PostBack event is generated and the itemcommand event of datalist or repeater is triggered. The commandname and commandargument values of the button are uploaded to the event handler.

Create an event handler for the itemcommand event. Note that the second parameter of event handler (Name: E ). This parameter is of the repeatercommandeventargs type and has the following four attributes:

1. commandargument-value of commandargument property of the clicked button
2. commandname property value of commandname-button
3. commandsource-referenced by the clicked button
4. item-reference of the repeateritem containing the clicked button; each record bound to the repeater is indicated as a repeateritem
Because the categoryid of the selected category is passed in through commandargument, you can get the related products in itemcommand event handler. These products are bound to a bulletedlist in the itemtemplate (added. The rest is to add bulletedlist, reference it in itemcommand event handler, and then bind the products of the selected category to bulletedlist (for details, see the following msdn content)

Note: The itemcommand event handler of datalist imports an object of the datalistcommandeventargs type, which provides four attributes identical to repeatercommandeventargs.
Display the products of the selected category
Use bulletedlist. Return to the declaration code on the custombuttons. ASPX page, and add a bulletedlist after "show products" linkbutton. The itemcommand event handler references this control through E. Item. findcontrol ("productsincategory") and binds it to products.

C #
Protected void categories_itemcommand (Object source, repeatercommandeventargs E)
{
If (E. commandname = "showproducts ")
{
// Determine the categoryid
Int categoryid = convert. toint32 (E. commandargument );
// Get the associated products from the proudctsbll and bind
// Them to the bulletedlist
Bulletedlist products = (bulletedlist) E. Item. findcontrol ("productsincategory ");
Productsbll productsapi = new productsbll ();
Products. datasource = productsapi. getproductsbycategoryid (categoryid );
Products. databind ());
}
}
In itemcommand event handlerBefore performing any operationsCheck the input commandname first. Because itemcommand event handler is executed when any button is hit, if there are multiple buttons in the template, the commandname value is used to identify the operation to be taken. In the previous tutorial on using the button in datalist, The datakeys set is used to obtain the primary key of a given item. This method is useful in datalist, Repeater does not have the datakeys attribute . Therefore, you need to use another method to provide the value of the primary key, such Commandargument of the button Or use a hidden label in the template, and then use E. Item. findcontrol ("labelid ") Read its value in itemcommand event handler.

Note: To modify the behavior of this report, for example, to list only one category products at a time, you only needEnableviewstate of bulletedlistSet property to false.

Content from msdn:

Bulletedlist class: Creates a control that generates a list of items in the project symbol format. Namespace: system. Web. UI. webcontrols
Remarks
Use the bulletedlist control to create a list of items in the project symbol format. To specify individual list items that you want to display in the bulletedlist control, place a listitem object between the start mark and end mark of the bulletedlist control.

To specify the project symbol type used to display list items in the bulletedlist control, set the bulletstyle attribute to one of the project symbol types defined by bulletstyle enumeration. The following table lists the available project symbol styles.

Project symbol Style
Description
Notset is not set.
Numbered number.
Loweralpha lowercase letters.
Upperalpha uppercase letters.
Lowerroman lowercase Roman numerals.
Upperroman upper-case Roman numerals.
Disc solid circle.
Circle.
Square: solid square.
Custom image.

 

Figure: bulletedlist Style

Use the firstbulletnumber attribute to specify the value of the Start list item number in the SORT bulletedlist control. If the bulletstyle attribute is set to the disc, square, circle, or customimage field, the value assigned to the firstbulletnumber attribute is ignored. If you set the bulletstyle attribute to the value of customimage to specify the User-Defined image of the Project symbol, you must also set the bulletimageurl attribute to specify the location of the image file.

 

To specify the display behavior of the list items in bulletedlist, set the displaymode attribute to one of the values defined by bulletedlistdisplaymode enumeration. The display behaviors are text, hyperlink, and linkbutton.

When you click a hyperlink, the corresponding URL is located. Use the value attribute to specify the URL to which the hyperlink locates. You can use the target attribute to specify a frame or window. When you click a hyperlink, a webpage is displayed in the frame or window.

When the displaymode attribute is set to linkbutton, The bulletedlist control displays the list items as links and click these links to send them back to the server. To programmatically control the operations performed when you click the link button, provide event processing for the Click EventProgram. Note that the selectedindex and selecteditem attributes are inherited from the listcontrol class and are not applicable to bulletedlist controls. Use the event data of the bulletedlisteventargs class to determine the index of the link button in the bulletedlist you clicked.

The bulletedlist control also supports data binding. To bind bulletedlist to a data source, you can use any data binding mechanism provided.

 

 

button. the commandargument attribute gets or sets an optional parameter, which is passed to the command event together with the associated commandname. Namespace: system. web. UI. webcontrols
remarks
use the commandargument attribute to specify parameters that supplement the commandname attribute.
note
although you can set the commandargument attribute separately, this attribute is generally used only when the commandname attribute is also set.
the commandargument attribute allows you to provide additional information about the command to be executed, supplement the commandname attribute. for example, if the commandname attribute is set to sort and the commandargument attribute is set to ascending, the command is sorted in ascending order. the commandeventargs class has two common attributes: commandname (used to obtain the command name) and commandargument (used to obtain command parameters ).

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.