This tutorial will demonstrate how add events to buttons within a repeater using ASP.NET 4.0 and C#.
Creating the Web Site
To demonstrate how we can add a button click event to our dynamic buttons within a repeater, we will need to create a simple web site. To do this, create a new ASP.NET Empty Web Site and:
Next, we will want to add in a button into the item template of our repeater. To do this, open up Default.aspx to source mode and add the following code to the repeater:
This simply adds a button to the item template while setting two important properties that we will need for our event. The CommandName and CommandArgument properties both get passed to theItemCommand event method when a button on the repeater is clicked. It is from this method that we can add in functionality for our dynamic buttons. It is important to understand that we can still add an event to this button using the OnClick property, however we don't have as much control while using that event.
Need help with cloud hosting? Try Server Intellect. We used them for our cloud hosting services and we are very happy with the results!
Adding the Events
First, we need to go ahead and add a data source with some data in it to our repeater so that we can view the buttons on our page. For this, I am going to create a data table with one row and bind it to our repeater. To do this, open up the Default.aspx.cs for editing and add the following code to thePage_Load event method:
To demonstrate how we can add a button click event to our dynamic buttons within a repeater, we will need to create a simple web site. To do this, create a new ASP.NET Empty Web Site and:
- Right click the project in your solution explorer.
- Select add new item...
- Select a web form.
- Name it 'Default.aspx'.
- Click add.
- Open Default.aspx up to design mode.
- Drag and drop a repeater onto the web form.
Next, we will want to add in a button into the item template of our repeater. To do this, open up Default.aspx to source mode and add the following code to the repeater:
<ItemTemplate> <asp:Button ID="Button1" runat="server" Text="Button" CommandName="HelloCommand" CommandArgument="5"/> </ItemTemplate> |
This simply adds a button to the item template while setting two important properties that we will need for our event. The CommandName and CommandArgument properties both get passed to theItemCommand event method when a button on the repeater is clicked. It is from this method that we can add in functionality for our dynamic buttons. It is important to understand that we can still add an event to this button using the OnClick property, however we don't have as much control while using that event.
Need help with cloud hosting? Try Server Intellect. We used them for our cloud hosting services and we are very happy with the results!
Adding the Events
First, we need to go ahead and add a data source with some data in it to our repeater so that we can view the buttons on our page. For this, I am going to create a data table with one row and bind it to our repeater. To do this, open up the Default.aspx.cs for editing and add the following code to thePage_Load event method:
if (!Page.IsPostBack) { DataTable dt = new DataTable(); DataRow dr = dt.NewRow(); dt.Rows.Add(dr); Repeater1.DataSource = dt; Repeater1.DataBind(); } |
Next, we need to add some code to our repeater's ItemCommand event method. In this method we have access to both the command name and argument that we set on our button in the repeater. We can use these values to add conditions to determine what button on our repeater has been clicked, and then add our functionality to this using the command argument if needed. Here is some sample code that you can add to the Repeater1_ItemCommand event method that uses our properties:
If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.
This simply outputs the values to the page, and then sets up the condition you would need to bind an event specifically to that button. This method of adding events to our repeater's buttons is extremely powerful because it gives you control over as many different buttons that you need, while allowing extra data to be passed to the method via the command argument.
int x = 4; Response.Write(e.CommandName); Response.Write("<br/>"); Response.Write(e.CommandArgument); if (e.CommandName == "HelloCommand") { //Button was clicked } |
If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.
This simply outputs the values to the page, and then sets up the condition you would need to bind an event specifically to that button. This method of adding events to our repeater's buttons is extremely powerful because it gives you control over as many different buttons that you need, while allowing extra data to be passed to the method via the command argument.
0 comments:
Post a Comment