 |
| ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

August 4th, 2006, 11:08 AM
|
|
Registered User
|
|
Join Date: Oct 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Creating ctrls at runtime, not available@postback
Hi All,
I am creating controls at runtime, and they show up on the page, but
when the page is submitted, I can't find them. What am I doing wrong?
Adding the dynamic controls to a datalist code goes here:
----------------------------------------------------------
private void dlRankList_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
{
try
{
int itemIndex = e.Item.ItemIndex;
//DbDataRecord DR = ( DbDataRecord ) e.Item.DataItem;
//string ad_tag = DR["ad_tag"].ToString().Trim();
//DictionaryEntry DR = (DictionaryEntry)e.Item.DataItem;
DataRowView DR = (DataRowView)e.Item.DataItem;
string ad_tag = DR.Row.ItemArray[0].ToString();
SqlDataReader drOfferDetails = ADTemplate.GetOfferDetails();
dtFields = DataReaderToDataTable.Convert(drOfferDetails);
drOfferDetails.Close();
//int j;
//j=itemIndex;
DataSet adsData1 = new DataSet();
adsData1.Tables.Add(dtFields);
Table detailTable = (Table) e.Item.FindControl("tblDetails");
detailTable.ID="detailTable"+itemIndex;
//Table detailTable = new Table();
detailTable.EnableViewState=true;
DataView dvOffer = new DataView(adsData1.Tables[0]);
dvOffer.RowFilter = "ad_tag = '"+ ad_tag +"'";
if (dvOffer.Count > 0)
{
//Header row for details asp:table
TableRow detailHeader = new TableRow();
TableCell detailHeaderCell = new TableCell();
Label lbloffer=new Label();
lbloffer.Text="Offer Code";
lbloffer.Width=Unit.Pixel(200);
Label lblStartDate=new Label();
lblStartDate.Text="Start Date";
lblStartDate.Width=Unit.Pixel(200);
Label lblEndDate=new Label();
lblEndDate.Text="End Date";
lblEndDate.Width=Unit.Pixel(200);
Label lblDaysEff=new Label();
lblDaysEff.Text="Days Effected";
lblDaysEff.Width=Unit.Pixel(200);
Label lblEmpty=new Label();
lblEmpty.Text="";
lblEmpty.Width=Unit.Pixel(302);
Label lblUpdate=new Label();
lblUpdate.Text=""+" "; //Update
lblUpdate.Width=Unit.Pixel(60); //50
Label lblDelete=new Label();
lblDelete.Text="Delete";
lblDelete.Width=Unit.Pixel(50);
detailHeaderCell.Controls.Add(lbloffer);
detailHeaderCell.Controls.Add(lblStartDate);
detailHeaderCell.Controls.Add(lblEndDate);
detailHeaderCell.Controls.Add(lblDaysEff);
detailHeaderCell.Controls.Add(lblEmpty);
detailHeaderCell.Controls.Add(lblUpdate);
detailHeaderCell.Controls.Add(lblDelete);
detailHeader.Cells.Add(detailHeaderCell);
detailHeader.BackColor=System.Drawing.Color.SandyB rown;
detailHeader.Font.Size=System.Web.UI.WebControls.F ontUnit.Point(11);
detailHeader.Font.Bold=true;
detailTable.Rows.Add(detailHeader);
}
for(int i=0;i<=dvOffer.Count-1;i++)
{
//Filling the details in asp:table
TableRow detailRow = new TableRow();
//j=RandomNumber.getRandomNumber(1,10000);
detailRow.ID="detailRow"+i.ToString();
if(dvOffer[i].Row["ad_tag"] != System.DBNull.Value)
{
TextBox txtOfferCode=new TextBox();
txtOfferCode.ID = "txtOfferCode" + i;
//popup for selecting the offercode
HtmlButton hBtnChoose = new HtmlButton();
Button btnChoose = new Button();
//string popupUrl = "popup.aspx?currentAdSource=" + dvOffer[i].Row["offer_code"].ToString().Trim() +
// "&displayFunctionName=DisplayAdSource" + itemIndex.ToString() + i;
string popupUrl = "PopupOffer.aspx?currentAdSource=" + dvOffer[i].Row["offer_code"].ToString().Trim() +
"&displayFunctionName=DisplayAdSource" + itemIndex.ToString() + i;
string jsScript = "window.open('" + popupUrl + "',null,'height=230, width=520, left=200,top=100,status= no, resizable= yes, scrollbars=yes, toolbar=no,location=no,menubar=no ');";
txtOfferCode.Text = dvOffer[i].Row["offer_code"].ToString().Trim();
//txtOfferCode.Enabled = false;
hBtnChoose.InnerText = "...";
hBtnChoose.Attributes.Add("onclick", jsScript);
//---------------
//popup calendar for selecting the startdate
TextBox txtStartDate=new TextBox();
txtStartDate.ID = "txtStartDate" + i;
HyperLink lbtnCalendar=new HyperLink();
lbtnCalendar.ImageUrl="images/icon_calendar.gif";
lbtnCalendar.NavigateUrl = "javascript:calendar_window=window.open('calendar. aspx?formname=frmMain.dlRankList__ctl" + itemIndex.ToString() + "_txtStartDate" + i +"','DatePicker','width=154,height=188,left=360,to p=180');calendar_window.focus()";
txtStartDate.Text = dvOffer[i].Row["start_date"].ToString().Trim();
//----------------
//popup calendar for selecting the EndDate
TextBox txtEndDate=new TextBox();
txtEndDate.ID = "txtEndDate" + i;
HyperLink lbtnCalendar1=new HyperLink();
lbtnCalendar1.ImageUrl="images/icon_calendar.gif";
lbtnCalendar1.NavigateUrl = "javascript:calendar_window=window.open('calendar. aspx?formname=frmMain.dlRankList__ctl" + itemIndex.ToString() + "_txtEndDate" + i +"','DatePicker','width=154,height=188,left=360,to p=180');calendar_window.focus()";
txtEndDate.Text = dvOffer[i].Row["end_date"].ToString().Trim();
//----------------
CheckBox chkMon=new CheckBox();
CheckBox chkTue=new CheckBox();
CheckBox chkWed=new CheckBox();
CheckBox chkThu=new CheckBox();
CheckBox chkFri=new CheckBox();
CheckBox chkSat=new CheckBox();
Label lblMon=new Label();
lblMon.Text=" "+"Mon";
Label lblTue=new Label();
lblTue.Text="Tue";
Label lblWed=new Label();
lblWed.Text="Wed";
Label lblThu=new Label();
lblThu.Text="Thu";
Label lblFri=new Label();
lblFri.Text="Fri";
Label lblSat=new Label();
lblSat.Text="Sat";
string dayseff = dvOffer[i].Row["days_effective"].ToString().Trim();
switch(dayseff)
{
case "1":
chkMon.Checked=true;
break;
case "2":
chkTue.Checked=true;
break;
case "3":
chkWed.Checked=true;
break;
case "4":
chkThu.Checked=true;
break;
case "5":
chkFri.Checked=true;
break;
case "6":
chkSat.Checked=true;
break;
case "0":
chkMon.Checked=true;
chkTue.Checked=true;
chkWed.Checked=true;
chkThu.Checked=true;
chkFri.Checked=true;
chkSat.Checked=true;
break;
}
//Deleting a offercode
HyperLink btnDelete=new HyperLink();
btnDelete.ImageUrl="Images/Delete.PNG";
btnDelete.ID="imgDelete"+i;
btnDelete.NavigateUrl="ADRanking.aspx?DeleteCode=" + dvOffer[i].Row["offer_code"].ToString().Trim() + "";
btnDelete.Attributes.Add("onclick","javascript:ret urn confirm('Are you sure you want to DELETE Offer Code [ "+ dvOffer[i].Row["offer_code"].ToString().Trim() + " ]');");
//Updating a offercode
//HyperLink btnUpdate=new HyperLink();
//btnUpdate.ImageUrl="Images/SaveUp.PNG";
//btnUpdate.ID="ImageUpd"+i;
//btnUpdate.NavigateUrl="ADRanking.aspx?UpdateCode=" + dvOffer[i].Row["offer_code"].ToString().Trim() + "";
//btnUpdate.Attributes.Add("onclick","javascript:ret urn confirm('Are you sure you want to UPDATE Offer Code [ "+ dvOffer[i].Row["offer_code"].ToString().Trim() + " ]');");
Button btnUpdate=new Button();
btnUpdate.ID="ImageUpd"+i;
btnUpdate.Attributes.Add("onclick","return assignValupd();");
btnUpdate.Visible=false;
Label lblEmpty1=new Label();
lblEmpty1.Text="";
lblEmpty1.Width=Unit.Pixel(302);
Label lblEmpty2=new Label();
lblEmpty2.Text="";
lblEmpty2.Width=Unit.Pixel(85); //35
//LinkButton lnkUpd = new LinkButton();
//lnkUpd.Text="Edit";
//lnkUpd.CommandName="Edit";
TableCell detailCell = new TableCell();
detailCell.ID="detailCell"+i.ToString();
detailCell.Controls.Add(txtOfferCode);
detailCell.Controls.Add(hBtnChoose);
detailCell.Controls.Add(txtStartDate);
detailCell.Controls.Add(lbtnCalendar);
detailCell.Controls.Add(txtEndDate);
detailCell.Controls.Add(lbtnCalendar1);
detailCell.Controls.Add(lblMon);
detailCell.Controls.Add(chkMon);
detailCell.Controls.Add(lblTue);
detailCell.Controls.Add(chkTue);
detailCell.Controls.Add(lblWed);
detailCell.Controls.Add(chkWed);
detailCell.Controls.Add(lblThu);
detailCell.Controls.Add(chkThu);
detailCell.Controls.Add(lblFri);
detailCell.Controls.Add(chkFri);
detailCell.Controls.Add(lblSat);
detailCell.Controls.Add(chkSat);
detailCell.Controls.Add(lblEmpty1);
detailCell.Controls.Add(btnUpdate);
//detailCell.Controls.Add(lnkUpd);
detailCell.Controls.Add(lblEmpty2);
detailCell.Controls.Add(btnDelete);
detailRow.Cells.Add(detailCell);
}
detailRow.BackColor=System.Drawing.Color.Silver;
detailRow.Font.Size=System.Web.UI.WebControls.Font Unit.Smaller;
detailRow.Font.Bold=true;
detailTable.Rows.Add(detailRow);
}
}
Accessing datalist controls after page submitssion the code goes here:
----------------------------------------------------------
private void btnupd_Click(object sender, System.EventArgs e)
{
string str1;
foreach(DataListItem dgItem in dlRankList.Items)
{
Table tblDetails= (Table)dgItem.FindControl("tblDetails");
foreach (TableRow r in tblDetails.Rows)
{
foreach (TableCell c in r.Cells)
{
TextBox txtOfferCode= (TextBox)c.FindControl("txtOfferCode");
str1=txtOfferCode.Text.ToString();
}
}
}
I am spent quite much time on this, unable to figure it out.
Your reply to this very much appreciated.
Thanks..
Regards,
Guptha
|
|

August 6th, 2006, 10:25 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
|
|
When you create controls dynamically, they need to be created on each post back. To do this, create any dynamic controls in the Page_Init event.
|
|

August 8th, 2006, 11:57 PM
|
|
Authorized User
|
|
Join Date: Oct 2004
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
When you are creating dynamic controls, you have have to recreate it in each post back. you can create it in page load without looking whether its a post back or not.
Tell me what you need and i will tell you how to get along without it.
-----------------------------------------------------------------
Ratheesh
Bangalore, INDIA
[email protected]
|
|

August 9th, 2006, 08:48 AM
|
|
Registered User
|
|
Join Date: Oct 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks Ratheesh for replying to my post. my problem is i am creating controls like textbox,checkbox etc at runtime loding the data in to created controls and it is working fine showing the controls and data, i have a update button on the screen onclick of the button the page is posting and reaching the event hander i.e btnupdate_Onclik but there i am unable to get the controls (which i have created at runtime) data, why these controls not available after postback and how i can get these controls data.
your help on this very much appreciated.
thanks..
|
|

August 9th, 2006, 10:25 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
|
|
Read my post. I do this in applications. Page_Load will not work due to view state issues. In order to see the controls and the data entered into them on a post back, you need to use the Page_Init event of the page.
|
|

August 9th, 2006, 11:34 AM
|
|
Registered User
|
|
Join Date: Oct 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
i have tried Page_Init event, can you please send me any samplecode or explain in detail how can i do this.
thanks..
Regards,
Dayanand
|
|

August 9th, 2006, 04:00 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
|
|
Any code where you add controls dynamically to the page, put in the Page_Init event:
Page_Init....
textbox tb = new textbox();
tb.name = "myTextBox";
Page.FindControl("Form1").Controls.Add(tb);
End sub
|
|

August 9th, 2006, 10:20 PM
|
|
Registered User
|
|
Join Date: Oct 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi, Thanks for your reply on this, i have tried as below but i could not get the textbox value it gives error "Object reference not set to an instance of an object",
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
TextBox tb = new TextBox();
tb.Text = "myTextBox";
Page.FindControl("Form1").Controls.Add(tb);
}
private void Button1_Click(object sender, System.EventArgs e)
{
string a;
TextBox tb=(TextBox)Page.FindControl("tb");
a=tb.Text.ToString();
}
Please let me know how to assign control onInit if the above is not correct and how to access the control on button click event.
appreciate your help on this.
Regards,
Dayanand
|
|

August 10th, 2006, 10:31 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
|
|
YOu almost have it. When you create the control assign an ID value to the tb.
tb.ID = "myTextBox"
Then you can do this:
TextBox tb=(TextBox)Page.FindControl("myTextBox");
|
|
 |