Click here to Skip to main content
15,915,975 members
Home / Discussions / ASP.NET
   

ASP.NET

 
AnswerRe: Format String for Decimal {0:D2} Pin
Guffa3-Oct-06 9:51
Guffa3-Oct-06 9:51 
GeneralRe: Format String for Decimal {0:D2} Pin
OldDog.Net4-Oct-06 2:56
OldDog.Net4-Oct-06 2:56 
AnswerRe: Format String for Decimal {0:D2} Pin
Guffa4-Oct-06 6:07
Guffa4-Oct-06 6:07 
QuestionProblem with ButtonEvents on dynamic UserContols Pin
Ibana3-Oct-06 7:44
Ibana3-Oct-06 7:44 
AnswerRe: Problem with ButtonEvents on dynamic UserContols Pin
e-laj3-Oct-06 11:33
e-laj3-Oct-06 11:33 
GeneralRe: Problem with ButtonEvents on dynamic UserContols Pin
nguyenvhn3-Oct-06 17:31
nguyenvhn3-Oct-06 17:31 
GeneralRe: Problem with ButtonEvents on dynamic UserContols Pin
Ibana5-Oct-06 12:52
Ibana5-Oct-06 12:52 
AnswerRe: Problem with ButtonEvents on dynamic UserContols Pin
e-laj5-Oct-06 21:45
e-laj5-Oct-06 21:45 
Sorry - I have a little mistake, well i implement dynamic control creation long time ago - i forgot an important point.

You should override the LoadViewState instead of TrackViewState.
The TrackViewState is executed before the view state is populated with the previous value, LoadViewState is the right point to initialize dynamic controls according to view state values - in this point the view state has its previous values.

Here is a full running example. In this example you can create dynamic buttons and executes their events.
I wrote this example for you and any other who may need it.

The DynamicExample.aspx
=======================

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DynamicExample.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="buttonCreateButtons" runat="server" OnClick="buttonCreateButtons_Click" Text="Create" />&nbsp; &nbsp;<asp:TextBox ID="textBoxTotalButtons" runat="server" Width="53px"></asp:TextBox>&nbsp; buttons<br />
<br />
<asp:Panel ID="panelButtons" runat="server" Height="85px" Width="319px">
</asp:Panel>
<br />
Click Message:<br />
<asp:Label ID="labelClickMessage" runat="server" BackColor="#80FF80" Height="25px" Width="325px"></asp:Label><br />
<br />
Command Message:<br />
<asp:Label ID="labelCommandMessage" runat="server" Height="25px" Width="325px" BackColor="#80FF80"></asp:Label><br />
<br />
</div>
</form>
</body>
</html>



The DynamicExample.aspx.cs
==========================

/*******************************************************************************
*
* Filename: DynamicExample.cs
*
* Class: DynamicExample
*
* Creation Date: 06/Oct/2006
*
* Update list:
* -----------
* 1) Date:
* Description:
*
* Writer: Ilan Amoyal (e-laj)
*
******************************************************************************/

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

/// <summary>
/// Loads the view state - and initializes dynamic controls according to the view state.
/// </summary>
/// <param name="savedState">The saved state.</param>
protected override void LoadViewState(object savedState)
{
/* Call the base calss implementation */
base.LoadViewState(savedState);

/* Create any dynamic */
/* Recall the int? is .NET 2 nulleable integer */
int? totalButtons = (int?)ViewState["totalDynamicButtons"];
if(totalButtons != null)
{
CreateDynamicButtons((int)totalButtons);
}
}

/// <summary>
/// Creates dynamic buttons.
/// </summary>
/// <param name="totalButtonToBeCreated">The total number of buttons to be created.</param>
private void CreateDynamicButtons(int totalButtonToBeCreated)
{
/* Removes any previous buttons */
panelButtons.Controls.Clear();

for(int i = 1; i <= totalButtonToBeCreated; i++)
{
CreateDynamicButton(i);
}

/* Save the number of buttons that were created. */
ViewState["totalDynamicButtons"] = totalButtonToBeCreated;
}

/// <summary>
/// Creates a button and adds ot to the panel controls collection.
/// </summary>
/// <param name="i">The ordinal position of this button.</param>
private void CreateDynamicButton(int i)
{
Button btn = new Button();
btn.ID = CreateButtonUniqueId(i);

/* Attaches events */
btn.Click += new EventHandler(btn_Click);
btn.CommandArgument = "This is button no " + i.ToString();
btn.Command += new CommandEventHandler(btn_Command);
btn.Text = "Button no " + i.ToString();

/* Adds the button to its container "Controls* collection. */
panelButtons.Controls.Add(btn);


/* If we have specific information related to this button that we wish to access
* on each post-back we can add this information to the view state.
*
* ViewState["some_unique_key"] = some_unique_info;
*/
}

/// <summary>
/// This function creates a uniques id according to the passed integer.
/// </summary>
/// <param name="ordinal">The integer related to the dynamic button.</param>
/// <returns>A unique id for that button.</returns>
private string CreateButtonUniqueId(int ordinal)
{
return "dynamic_button_no_" + ordinal.ToString().Trim();
}

/// <summary>
/// Here we create dynamic buttons according to the number assigned to the text box.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
protected void buttonCreateButtons_Click(object sender, EventArgs e)
{
int totalButtonToBeCreated;
if(int.TryParse(textBoxTotalButtons.Text, out totalButtonToBeCreated))
{
CreateDynamicButtons(totalButtonToBeCreated);
}
}

/// <summary>
/// Here is an implementation of the dynamic controls events.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Web.UI.WebControls.CommandEventArgs"/> instance containing the event data.</param>
void btn_Command(object sender, CommandEventArgs e)
{
labelCommandMessage.Text = (string)e.CommandArgument;
}

/// <summary>
/// Handles the Click event of the dynaic buttons.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
void btn_Click(object sender, EventArgs e)
{
/* Do what ever you want to. For example: */
Button btn = (Button)sender;
labelClickMessage.Text = btn.ID + " pressed!";
}
}


================
Have a nice day!
================
Questionhow make make sorting and paging both work togeather? Pin
jwrz200t3-Oct-06 6:27
jwrz200t3-Oct-06 6:27 
AnswerRe: how make make sorting and paging both work togeather? Pin
laocat20036-Oct-06 5:06
laocat20036-Oct-06 5:06 
Question(Ultra)WebTabs from Ingragistic Pin
julie763-Oct-06 6:22
julie763-Oct-06 6:22 
AnswerRe: (Ultra)WebTabs from Ingragistic Pin
Member 964-Oct-06 13:23
Member 964-Oct-06 13:23 
QuestionWrong button clicked when user presses "Enter" key Pin
Goalie353-Oct-06 5:28
Goalie353-Oct-06 5:28 
AnswerRe: Wrong button clicked when user presses "Enter" key Pin
ToddHileHoffer3-Oct-06 5:47
ToddHileHoffer3-Oct-06 5:47 
QuestionHow to get Focus on one Button,if two buttons exists on the same form Pin
ravindradonkada3-Oct-06 3:07
ravindradonkada3-Oct-06 3:07 
AnswerRe: How to get Focus on one Button,if two buttons exists on the same form Pin
Sathesh Sakthivel3-Oct-06 3:15
Sathesh Sakthivel3-Oct-06 3:15 
AnswerRe: How to get Focus on one Button,if two buttons exists on the same form Pin
ToddHileHoffer3-Oct-06 4:55
ToddHileHoffer3-Oct-06 4:55 
AnswerRe: How to get Focus on one Button,if two buttons exists on the same form Pin
vikram1383-Oct-06 17:36
vikram1383-Oct-06 17:36 
Questionpage load event not called Pin
Deepak the Cool3-Oct-06 1:23
Deepak the Cool3-Oct-06 1:23 
AnswerRe: page load event not called Pin
CWIZO3-Oct-06 6:37
CWIZO3-Oct-06 6:37 
QuestionWebservice and COM object Pin
Talal Sultan3-Oct-06 1:22
Talal Sultan3-Oct-06 1:22 
QuestionAvoid Datagrid Refresh Pin
kavithapuranik3-Oct-06 0:40
kavithapuranik3-Oct-06 0:40 
Questionhow to make web application DDA compliant ? Pin
King Shez3-Oct-06 0:32
King Shez3-Oct-06 0:32 
QuestionProblem With Hosting Pin
alokdotnet3-Oct-06 0:16
alokdotnet3-Oct-06 0:16 
QuestionFinding client side(browser side) DSN through ODBC Pin
rahul21nov2-Oct-06 23:43
rahul21nov2-Oct-06 23:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.