protected void Page_Load(object sender, EventArgs e)
{
Team T = new Team();
string[] TLeaders = T.GetAllTeamLeaders(Session["USER_EMAIL"].ToString(), Session["ProjectID"].ToString());
for (int i = 0; i < TLeaders.Length; i++)
{
System.Web.UI.HtmlControls.HtmlGenericControl createDiv =new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
createDiv.Style.Add(HtmlTextWriterStyle.BorderStyle, "Solid");
createDiv.Style.Add(HtmlTextWriterStyle.BorderColor, "lightblue");
createDiv.Style.Add(HtmlTextWriterStyle.BorderWidth, "1px");
createDiv.Style.Add(HtmlTextWriterStyle.Height, "100px");
createDiv.Style.Add(HtmlTextWriterStyle.Width, "1350px");
createDiv.Style.Add(HtmlTextWriterStyle.MarginTop, "20px");
createDiv.Style.Add(HtmlTextWriterStyle.BackgroundColor, "white");
DivRate.Controls.Add(createDiv);
Label LeaderName = new Label();
LeaderName.Text = TLeaders[i];
LeaderName.Style.Add("margin-left", "600px");
LeaderName.Style.Add("color","gray");
LeaderName.Style.Add("font-size","20px;");
LeaderName.Style.Add("margin-top", "10px");
LeaderName.Style.Add("position", "absolute");
createDiv.Controls.Add(LeaderName);
RadioButtonList Rate = new RadioButtonList();
ListItem bad = new ListItem();
ListItem fair = new ListItem();
ListItem good= new ListItem();
ListItem veryGood = new ListItem();
ListItem excellent = new ListItem();
bad.Value = "1";
bad.Text = "Bad";
fair.Value = "2";
fair.Text = "Fair";
good.Value = "3";
good.Text = "Good";
veryGood.Value = "4";
veryGood.Text = "Very Good";
excellent.Value = "5";
excellent.Text = "Excellent";
Rate.AutoPostBack = true;
Rate.Items.Add(bad);
Rate.Items.Add(fair);
Rate.Items.Add(good);
Rate.Items.Add(veryGood);
Rate.Items.Add(excellent);
Rate.Attributes.Add("onchange", "return LeaderName('"+TLeaders[i]+"');");
Rate.SelectedIndexChanged += new EventHandler(CheckChange("s"));
Rate.RepeatColumns=5;
Rate.Width = 10;
Rate.Height = 10;
Rate.CssClass = "RateClass";
createDiv.Controls.Add(Rate);
}
}
so in this code I create a div and I place inside it a label and a Radio Button List however what i want to do is to create an event handler and send a data to it when i call it i call it like this:
Rate.SelectedIndexChanged += new EventHandler(CheckChange("s"));
and this is the event handler:
protected void CheckChange(string s)
{
ScriptManager.RegisterStartupScript(Page, typeof(Page), "", "fun('" + s + "')", true);
}
It give me an error "method name expected" when I send the data like this
any solution?
Related
I am creating dynamic textboxes when clicking on a set of different radio button. Below is an example of two radio button onclick event.
protected void Checkbox1_CheckedChanged(object sender, EventArgs e)
{
string servicename = "service1";
if (checkbox1.Checked)
{
InputParameters.InputParameters aa= new InputParameters.InputParameters();
textbox = aa.GetInputFields(servicename);
for (int i=0;i<textbox.Count;i++)
{
// declare a textbox
TextBox CPDT = new TextBox();
CPDT.ID = servicename + i.ToString();
CPDT.CssClass = "form-control";
CPDT.EnableViewState = true;
Label lblCPD=new Label();
lblCPD.ID = "txtDynamiclbl" + servicename+ i.ToString();
lblCPD.CssClass= "form-control-label";
lblCPD.Text= textbox[i].ToString();
lblCPD.EnableViewState = true;
CPDPlaceHolder.Controls.Add(lblCPD);
CPDPlaceHolder.Controls.Add(CPDT);
//this.NumberOfControls++;
}
Button callSoap = new Button();
callSoap.ID = "txtDynamicSearch" + servicename;
callSoap.Text = "Search";
callSoap.CssClass = ".btn-info";
callSoap.CommandArgument = "test";
callSoap.Click += new EventHandler(btnsoap);
callSoap.EnableViewState = true;
CPDPlaceHolder.Controls.Add(callSoap);
}
else
{
}
}
protected void Checkbox2_CheckedChanged(object sender, EventArgs e)
{
string servicename = "service2";
if (checkbox2.Checked)
{
InputParameters.InputParameters aa = new InputParameters.InputParameters();
List<String> textbox = aa.GetInputFields("test1");
// textboxs.AddRange(textbox);
for (int i = 0; i < textbox.Count; i++)
{
// declare a textbox
TextBox CPDT = new TextBox();
CPDT.ID = servicename + i.ToString();
CPDT.CssClass = "form-control";
Label lblCPD = new Label();
lblCPD.ID = "txtDynamiclbl" + servicename + i.ToString();
lblCPD.CssClass = "form-control-label";
lblCPD.Text = textbox[i].ToString();
CPDPlaceHolder.Controls.Add(lblCPD);
CPDPlaceHolder.Controls.Add(CPDT);
}
Button callSoap = new Button();
callSoap.ID = "txtDynamicSearch" + servicename;
callSoap.Text = "Search";
callSoap.CssClass = ".btn-info";
callSoap.CommandArgument = "test1";
callSoap.Click += new EventHandler(btnsoap);
callSoap.EnableViewState = true;
CPDPlaceHolder.Controls.Add(callSoap);
}
else
{
}
}
The textboxes and search button appears as needed. The problem now is when i clicked on the search button a post back occur and all the controls are gone. I have been reading a lot about initialising the controls in page_preinit and i tried the code below.
protected void Page_PreInit(object sender, EventArgs e)
{
List<string> keys = Request.Form.AllKeys.Where(key => key.Contains("txtDynamic")).ToList();
int i = 1;
try
{
foreach (string key in keys)
{
TextBox CPDT = new TextBox();
CPDT.ID = "test" + i.ToString();
CPDT.CssClass = "form-control";
Label lblCPD = new Label();
lblCPD.ID = "txtDynamiclbl" + "test" + i.ToString();
lblCPD.CssClass = "form-control-label";
lblCPD.Text = textbox[i].ToString();
CPDPlaceHolder.Controls.Add(lblCPD);
CPDPlaceHolder.Controls.Add(CPDT);
i++;
}
}
catch
{
}
}
In the above function this line only returns the search button and not the texboxes. I am stuck on this issue.
List<string> keys = Request.Form.AllKeys.Where(key => key.Contains("txtDynamic")).ToList();
T
I have created dynamic controls on DropdownList's SelectedIndexChanged event. Button is one of those controls. I have also assigned event to that button but debugger is not coming on that click event. Following is my code.
protected void Page_Load(object sender, EventArgs e)
{
try
{
token = Session["LoginToken"].ToString();
if (!IsPostBack)
{
BindData();
fsSearch.Visible = false;
btnDownload.Visible = false;
}
else
{
foreach (HtmlTableRow row in (HtmlTableRowCollection)Session["dynamicControls"])
{
tblSearch.Rows.Add(row);
}
}
}
catch
{
}
}
private void BindData()
{
ddlReportName.DataSource = svcCommon.GetReports(token, out message);
ddlReportName.DataValueField = "Key";
ddlReportName.DataTextField = "Value";
ddlReportName.DataBind();
ddlReportName.Items.Insert(0, "--Select--");
}
protected void ddlReportName_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
string[] reportInfo = ddlReportName.SelectedValue.Split('|');
Session["dynamicControls"] = null;
tblSearch.Rows.Clear();
HtmlTableRow row = new HtmlTableRow();
HtmlTableCell cellFieldNameLbl = new HtmlTableCell();
HtmlTableCell cellFieldNameDdl = new HtmlTableCell();
HtmlTableCell cellOperatorLbl = new HtmlTableCell();
HtmlTableCell cellOperatorDdl = new HtmlTableCell();
HtmlTableCell cellValueLbl = new HtmlTableCell();
HtmlTableCell cellValueTxt = new HtmlTableCell();
HtmlTableCell cellOperatorRbtn = new HtmlTableCell();
HtmlTableCell cellAddMoreFilter = new HtmlTableCell();
Button btnAddMore = new Button();
DropDownList ddlColumn = new DropDownList();
DropDownList ddlOperator = new DropDownList();
TextBox txtValue = new TextBox();
RadioButtonList rbtnOperator = new RadioButtonList();
List<string> filterValues = svcCommon.GetSearchColumns(Convert.ToInt64(reportInfo[0]), token, out message);
fsSearch.Visible = btnDownload.Visible = filterValues.Count > 0 ? true : false;
ddlColumn.ID = "_ddlColumn0";
ddlOperator.ID = "_ddlOperator0";
txtValue.ID = "_txtValue0";
rbtnOperator.ID = "_rbtnOperator0";
btnAddMore.ID = "_btnAddMore0";
rbtnOperator.Items.Add("AND");
rbtnOperator.Items.Add("OR");
rbtnOperator.RepeatDirection = RepeatDirection.Horizontal;
btnAddMore.Text = "Add More";
btnAddMore.Click +=btnAddMore_Click;
ddlColumn.DataSource = filterValues;
ddlColumn.DataBind();
ddlOperator.DataSource = new List<string>()
{
"Equal",
"Not Equal",
"Less Than",
"Less Than Or Equal",
"Greater Than",
"Greater Than Or Equal",
"Start With",
"Not Start With",
"End With",
"Not End With",
"Contains",
"Not Contains",
"Between",
"Not Between",
"In",
"Not In"
};
ddlOperator.DataBind();
cellFieldNameLbl.InnerText = "Field Name:";
cellFieldNameDdl.Controls.Add(ddlColumn);
cellOperatorLbl.InnerText = "Operator";
cellOperatorDdl.Controls.Add(ddlOperator);
cellValueLbl.InnerText = "Value";
cellValueTxt.Controls.Add(txtValue);
cellOperatorRbtn.Controls.Add(rbtnOperator);
cellAddMoreFilter.Controls.Add(btnAddMore);
row.Cells.Add(cellFieldNameLbl);
row.Cells.Add(cellFieldNameDdl);
row.Cells.Add(cellOperatorLbl);
row.Cells.Add(cellOperatorDdl);
row.Cells.Add(cellValueLbl);
row.Cells.Add(cellValueTxt);
row.Cells.Add(cellOperatorRbtn);
row.Cells.Add(cellAddMoreFilter);
tblSearch.Rows.Add(row);
Session["dynamicControls"] = tblSearch.Rows;
}
catch (Exception ex)
{
}
}
protected void btnAddMore_Click(object sender, EventArgs e)
{
try
{
}
catch
{
}
}
The problem with dynamically created controls in asp.net webforms is that they aren't automatically added to the viewstate, so the postback event won't happen.
This should help you to understand adding controls dynamically, and managing them via the viewstate http://forums.asp.net/t/1900207.aspx?Creating+buttons+dynamically
Alternatively, a much easier way to manage this is to have the buttons on the page but not visible, then in the selected_index_changed event, just switch the visibility to true.
I hadn't enough time to try so many thing. The thing I did is stored the container on which dynamic controls are added into Session and on Page_Init event I have bound event and it is working fine now. :)
Current scenario: Main DDL of Decimal datatype input selection generates a new DDL. Upon input selection from new DDL it generates two labels and two textboxes with + button.
Current Issue: If i click the + button the recently generated dynamic web controls are hiding. How to avoid the hiding of controls and generate them continuously on every + button click event
My C# Code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindDropDownLists();
}
else
{
if (!String.IsNullOrEmpty(DropDownList5.SelectedValue))
{
if (DropDownList5.SelectedValue == "decimal")
{
createdynamiccontrols_decimal();
}
}
}
}
protected void DropDownList5_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void createdynamiccontrols_decimal()
{
int i = DropDownList5.SelectedIndex;
++i;
TableRow row = new TableRow();
row.ID = "TableRow_" + i.ToString();
TableCell cell1 = new TableCell();
DropDownList Range_DDL_Decimal = new DropDownList();
Range_DDL_Decimal.ID = "RandeDDL_Decimal" + i.ToString();
Range_DDL_Decimal.Items.Insert(0, new ListItem("--Select--", "--Select--"));
Range_DDL_Decimal.Items.Insert(1, new ListItem("Equal", "Equal"));
Range_DDL_Decimal.Items.Insert(2, new ListItem("NotEqual", "NotEqual"));
Range_DDL_Decimal.Items.Insert(3, new ListItem("greater than", "greater than"));
Range_DDL_Decimal.Items.Insert(4, new ListItem("lesser than", "lesser than"));
Range_DDL_Decimal.Items.Insert(5, new ListItem("greater than or equal to", "greater than or equal to"));
Range_DDL_Decimal.Items.Insert(6, new ListItem("lesser than or equal to", "lesser than or equal to"));
Range_DDL_Decimal.Items.Insert(7, new ListItem("Contains", "Contains"));
Range_DDL_Decimal.Items.Insert(8, new ListItem("Is Null", "Is Null"));
Range_DDL_Decimal.Items.Insert(9, new ListItem("Is Not Null", "Is Not Null"));
Range_DDL_Decimal.Items.Insert(10, new ListItem("Between", "Between"));
Range_DDL_Decimal.AutoPostBack = true;
Range_DDL_Decimal.SelectedIndexChanged += new System.EventHandler(Range_DDL_Decimal_SelectedIndexChanged);
cell1.Controls.Add(Range_DDL_Decimal);
//// Add the TableCell to the TableRow
row.Cells.Add(cell1);
dynamic_filter_table.Rows.Add(row);
dynamic_filter_table.EnableViewState = true;
ViewState["dynamic_filter_table"] = true;
}
protected void Range_DDL_Decimal_SelectedIndexChanged(object sender, EventArgs e)
{
int j = DropDownList5.SelectedIndex;
++j;
TableRow rowtwo;
rowtwo = new TableRow();
TableCell cell1 = new TableCell();
TableCell cell2 = new TableCell();
TableCell cell3 = new TableCell();
TextBox tb1 = new TextBox();
TextBox tb2 = new TextBox();
Label lbl1 = new Label();
Label lbl2 = new Label();
// Set a unique ID for each TextBox added
tb1.ID = "lowerbound_" + j.ToString();
tb2.ID = "upperbound_" + j.ToString();
lbl1.Text = "LowerBound:";
lbl1.Font.Size = FontUnit.Point(10);
lbl1.Font.Bold = true;
lbl1.Font.Name = "Arial";
lbl2.Text = "UpperBound:";
lbl2.Font.Size = FontUnit.Point(10);
lbl2.Font.Bold = true;
lbl2.Font.Name = "Arial";
Button addmore = new Button();
addmore.ID = "Button_Decimal" + j.ToString();
addmore.Text = "+";
addmore.Click += new System.EventHandler(Addmore_click);
**//If this button is clicked the Dynamic controls are vanished**
cell1.Controls.Add(lbl1);
cell1.Controls.Add(tb1);
cell2.Controls.Add(lbl2);
cell2.Controls.Add(tb2);
cell3.Controls.Add(addmore);
rowtwo.Cells.Add(cell1);
rowtwo.Cells.Add(cell2);
rowtwo.Cells.Add(cell3);
dynamic_filter_table.Rows.Add(rowtwo);
dynamic_filter_table.EnableViewState = true;
ViewState["dynamic_filter_table"] = true;
}
protected void Addmore_click(object sender, EventArgs e)
{
**//Generate dynamic controls on every click of + button
//How to procced**
}
protected void Page_PreInit(object sender, EventArgs e)
{
}
There are not hiding, you have to recreate the dynamically created controls on every request.
It has been a while, but I think you have to create the dynamic controls in the Page_Init event for them to be included in the view-state.
Some sources
I am grabbing some records from a database depending on the id number of the page, the amount of records that will display will vary, anywhere to 1 record to 50. I need to take these records then choose which ones I will be modifying, deleting, or won't change at all. I would like it to look like this:
Can't get image to show with stackoverflow image uploader so here: https://dl.dropboxusercontent.com/u/9446763/code/dropdownlist.jpg
I would like the text fields to be disabled if No Change or Remove is selected, and the fields enabled for revising if the option Modify is selected.
Below is what I have so far, the part I am struggling on is the selectedindex change I do not know how to code it so that when Modify is selected in the dropdown box the appropriate textboxes become enabled.
ASPX Page
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
CS Page
private void createControls()
{
var id = Request.Params["ID"];
System.Data.OleDb.OleDbConnection pcn;
System.Data.OleDb.OleDbCommand pcm;
System.Data.OleDb.OleDbDataReader prs;
pcn = new System.Data.OleDb.OleDbConnection("");
pcm = new System.Data.OleDb.OleDbCommand();
pcn.Open();
pcm.Connection = pcn;
var tableSql = #"select * FROM grouplist where ptid = '" + id + "'";
pcm.CommandText = tableSql;
prs = pcm.ExecuteReader();
var rowcount = 0;
while (prs.Read())
{
rowcount++;
PlaceHolder1.Controls.Add(new Literal() { Text = "<div class='row'><div class='span3'>" });
TextBox tx = new TextBox();
tx.ID = "txtData" + rowcount.ToString();
tx.Text = prs["name"].ToString().Trim();
tx.Width = 200;
tx.CssClass = "span2";
tx.Enabled = false;
PlaceHolder1.Controls.Add(tx);
PlaceHolder1.Controls.Add(new Literal() { Text = "</div><div class='span2'>" });
TextBox txa = new TextBox();
txa.ID = "amtData" + rowcount.ToString();
txa.Text = prs["amt"].ToString();
txa.CssClass = "span2";
txa.Enabled = false;
PlaceHolder1.Controls.Add(txa);
PlaceHolder1.Controls.Add(new Literal() { Text = "</div><div class='span3'>" });
DropDownList ddl = new DropDownList();
ddl.Items.Add("No Change");
ddl.Items.Add("Modify");
ddl.Items.Add("Remove");
ddl.Width = 200;
ddl.CssClass = "span2";
ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);
ddl.AutoPostBack = true;
ddl.ID = "ddlData" + rowcount.ToString();
PlaceHolder1.Controls.Add(ddl);
PlaceHolder1.Controls.Add(new Literal() { Text = "</div></div>" });
}
prs.Close();
}
void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
}
You need to have members in your class that hold references to your DropDownList and other controls that you want to enable/disable.
public class YourClass {
private DropDownList ddl;
private TestBox txa;
private void createControls {
// ...
TextBox txa = new TextBox();
txa.ID = "amtData" + rowcount.ToString();
// ...
DropDownList ddl = new DropDownList();
ddl.Items.Add("No Change");
// ... etc.
}
void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddl.SelectedIndex == 1)
txa.Enabled = true;
}
}
I have an AJAX accordion from the ajax control toolkit on a page with a datagrid inside on of the panels. I have a custom itemtemplate for the one column to create a multiline textbox when I click edit on the row. When I click update on the row, the original content of the textbox gets rendered into the textbox. It is supposed to update and go back to the literal not the textbox. When I put the DataBind() into a !IsPostBack, it doesnt get rendered when I click on the accordion pane. Any ideas?
Code:
protected void Page_Load(object sender, EventArgs e)
{
announ.HeaderStyle.CssClass = "header";
announ.Width = Unit.Percentage(100);
announ.GridLines = GridLines.None;
announ.AutoGenerateColumns = false;
announ.CellPadding = 10;
announ.CellSpacing = 0;
announ.HorizontalAlign = HorizontalAlign.Center;
announ.HeaderStyle.Font.Bold = true;
announ.EnableViewState = false;
announ.AlternatingItemStyle.BackColor = System.Drawing.Color.GhostWhite;
//announ.DeleteCommand += AnnounDeleteCommand;
announ.EditCommand += announ_EditCommand;
announ.UpdateCommand += announ_UpdateCommand;
announ.CancelCommand += announ_CancelCommand;
announ.DataKeyField = "id";
var tc1 = new TemplateColumn
{
HeaderTemplate = new
DataGridTemplate(ListItemType.Header, "Announcement"),
ItemTemplate = new DataGridTemplate(ListItemType.Item, "announcement_text"),
EditItemTemplate = new
DataGridTemplate(ListItemType.EditItem, "announcement_text")
};
var editColumn = new EditCommandColumn
{
ButtonType = ButtonColumnType.PushButton,
HeaderText = "Edit",
EditText = "Edit",
UpdateText = "Update",
CancelText = "Cancel"
};
var dateColumn = new BoundColumn {HeaderText = "Posted On", DataField = "date", ReadOnly = true};
var expirationColumn = new BoundColumn {HeaderText = "Expiration Date", DataField = "expiration_date"};
announ.Columns.Add(tc1);
announ.Columns.Add(dateColumn);
announ.Columns.Add(expirationColumn);
announ.DataSource = myAnnouncements;
announ.DataBind();
var deptMgtaccord = new Accordion
{
ID = "deptMgtaccord",
HeaderCssClass = "accordion-header",
HeaderSelectedCssClass = "accordion-headerSelected",
AutoSize = AutoSize.None,
SelectedIndex = 0,
FadeTransitions = true,
TransitionDuration = 250,
FramesPerSecond = 40,
RequireOpenedPane = false,
SuppressHeaderPostbacks = true
};
if (IsPostBack)
{
deptMgtaccord.SelectedIndex = selected;
}
var announcementPane = new AccordionPane {ID = "announcementPane"};
announcementPane.HeaderContainer.Attributes.Add("onmouseover", "this.style.backgroundColor='#e3e2e2';");
announcementPane.HeaderContainer.Attributes.Add("onmouseout", "this.style.backgroundColor='#ffffff';");
announcementPane.HeaderContainer.Controls.Add(new LiteralControl("Announcements >>"));
announcementPane.ContentContainer.Controls.Add(announ);
deptMgtaccord.Panes.Add(announcementPane);
var statsPane = new AccordionPane {ID = "statsPane"};
statsPane.HeaderContainer.Attributes.Add("onmouseover", "this.style.backgroundColor='#e3e2e2';");
statsPane.HeaderContainer.Attributes.Add("onmouseout", "this.style.backgroundColor='#ffffff';");
statsPane.HeaderContainer.Controls.Add(new LiteralControl("Statistics >>"));
statsPane.ContentContainer.Controls.Add(new LiteralControl("Stats"));
deptMgtaccord.Panes.Add(statsPane);
ph1.Controls.Add(deptMgtaccord);
}
protected void announ_CancelCommand(object source, DataGridCommandEventArgs e)
{
announ.EditItemIndex = -1;
announ.DataBind();
}
protected void announ_UpdateCommand(object source, DataGridCommandEventArgs e)
{
var dc = new MTCDataDataContext();
var announText = (TextBox) e.Item.Cells[1].Controls[1];
int announId = (int)announ.DataKeys[e.Item.ItemIndex];
var currentAnnoun = (from a in dc.announcements
where a.id == announId
select a).SingleOrDefault();
currentAnnoun.announcement_text = announText.Text;
dc.SubmitChanges();
announ.EditItemIndex = -1;
announ.DataBind();
}
protected void announ_EditCommand(object source, DataGridCommandEventArgs e)
{
announ.EditItemIndex = e.Item.ItemIndex;
announ.DataBind();
}
public class DataGridTemplate : ITemplate
{
ListItemType templateType;
string columnName;
public DataGridTemplate(ListItemType type, string colname)
{
templateType = type;
columnName = colname;
}
public void InstantiateIn(Control container)
{
Literal lc = new Literal();
TextBox tb = new TextBox();
switch (templateType)
{
case ListItemType.Header:
lc.Text = "<B>" + columnName + "</B>";
container.Controls.Add(lc);
break;
case ListItemType.Item:
lc.DataBinding += lc_DataBinding;
container.Controls.Add(lc);
break;
case ListItemType.EditItem:
tb.TextMode = TextBoxMode.MultiLine;
tb.Rows = 6;
tb.Columns = 57;
tb.DataBinding += tb_DataBinding;
container.Controls.Add(tb);
break;
case ListItemType.Footer:
lc.Text = "<I>" + columnName + "</I>";
container.Controls.Add(lc);
break;
}
}
void tb_DataBinding(object sender, EventArgs e)
{
TextBox tb = (TextBox)sender;
DataGridItem row = (DataGridItem)tb.NamingContainer;
tb.ID = "txt_" + row.ItemIndex;
tb.Text = DataBinder.Eval(row.DataItem, columnName).ToString();
}
void lc_DataBinding(object sender, EventArgs e)
{
Literal lc = (Literal)sender;
DataGridItem row = (DataGridItem)lc.NamingContainer;
lc.ID = "txt_" + row.ItemIndex;
lc.Text = DataBinder.Eval(row.DataItem, columnName).ToString();
}
}
You need to add your dynamic controls in PreInit on every request in order for the controls to get back into the ControlTree and raise events.
Page Event:
PreInit
Typical Use:
Raised after the start stage is complete and before the initialization stage begins.
Use this event for the following:
Check the IsPostBack property to determine whether this is the first
time the page is being processed. The
IsCallback and IsCrossPagePostBack
properties have also been set at this
time.
Create or re-create dynamic controls.
Set a master page dynamically.
Set the Theme property dynamically.
Read or set profile property values.
Note: If the request is a postback, the values of the controls have not yet been restored from view state. If you set a control property at this stage, its value might be overwritten in the next event.