Click here to Skip to main content
15,886,806 members
Home / Discussions / ASP.NET
   

ASP.NET

 
AnswerRe: asp.net toolbox Pin
Shah Rizal13-Sep-11 22:19
Shah Rizal13-Sep-11 22:19 
GeneralRe: asp.net toolbox Pin
classy_dog14-Sep-11 4:07
classy_dog14-Sep-11 4:07 
GeneralRe: asp.net toolbox Pin
Shah Rizal21-Sep-11 22:12
Shah Rizal21-Sep-11 22:12 
Questionwindow.open? Pin
murali_utr13-Sep-11 7:51
murali_utr13-Sep-11 7:51 
QuestionUsing Cookies in Custom Role Provider in ASP.NET 2.0 Pin
Rahil31113-Sep-11 4:11
Rahil31113-Sep-11 4:11 
Questionproblem with modalpopupextender Pin
Ali Al Omairi(Abu AlHassan)13-Sep-11 2:33
professionalAli Al Omairi(Abu AlHassan)13-Sep-11 2:33 
Questionasp.net pdf download problem Pin
UD(IA)13-Sep-11 2:30
UD(IA)13-Sep-11 2:30 
QuestionTwo ways of adding dynamic controls and get their events on run time Pin
basharat8113-Sep-11 1:04
basharat8113-Sep-11 1:04 
During the web development some time we need to add dynamic controls to a web page. Mostly developer’s face problems with dynamically added controls that they can’t get the event as the page post back.
I was also struggling with this issue and at the end I have found two possible solutions
1:- Add the control on page_Init
private void Page_Init(object sender, System.EventArgs e)
{
if (!IsPostBack)
AddControls();
}

private void Page_Load(object sender, System.EventArgs e)
{
}
private void AddControls()
{
TextBox dynamictextbox = new TextBox();
dynamictextbox.Text = "(Enter some text)";
dynamictextbox.ID = "dynamictextbox";
Button dynamicbutton = new Button();
dynamicbutton.Click += new System.EventHandler(dynamicbutton_Click);
dynamicbutton.Text = "Dynamic Button";
Panel1.Controls.Add(dynamictextbox);
Panel1.Controls.Add(new LiteralControl("<BR>"));
Panel1.Controls.Add(new LiteralControl("<BR>"));
Panel1.Controls.Add(dynamicbutton);
ViewState["controlsadded"] = true;
}

private void dynamicbutton_Click(Object sender, System.EventArgs e)
{
TextBox tb = new TextBox();
tb = (TextBox) (Panel1.FindControl("dynamictextbox"));
Label1.Text = tb.Text;
}

2:-Override method LoadViewState
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
AddControls();
}

protected override void LoadViewState(object savedState)
{
base.LoadViewState(savedState);
if (ViewState["controsladded"] == null)
AddControls();
}

private void AddControls()
{
TextBox dynamictextbox = new TextBox();
dynamictextbox.Text = "(Enter some text)";
dynamictextbox.ID = "dynamictextbox";
Button dynamicbutton = new Button();
dynamicbutton.Click += new System.EventHandler(dynamicbutton_Click);
dynamicbutton.Text = "Dynamic Button";
Panel1.Controls.Add(dynamictextbox);
Panel1.Controls.Add(new LiteralControl("<BR>"));
Panel1.Controls.Add(new LiteralControl("<BR>"));
Panel1.Controls.Add(dynamicbutton);
ViewState["controlsadded"] = true;
}

private void dynamicbutton_Click(Object sender, System.EventArgs e)
{
TextBox tb = new TextBox();
tb = (TextBox) (Panel1.FindControl("dynamictextbox"));
Label1.Text = tb.Text;
}


Hope will work for you

Thanks to MSDN

http://msdn.microsoft.com/en-us/library/aa287574%28v=vs.71%29.aspx
AnswerRe: Two ways of adding dynamic controls and get their events on run time Pin
Morgs Morgan14-Sep-11 10:29
Morgs Morgan14-Sep-11 10:29 
QuestionWhat language/tool would you use to create... Pin
Mike Doner12-Sep-11 13:59
Mike Doner12-Sep-11 13:59 
AnswerRe: What language/tool would you use to create... Pin
Abhinav S13-Sep-11 20:04
Abhinav S13-Sep-11 20:04 
QuestionC# designer tool Pin
classy_dog12-Sep-11 12:12
classy_dog12-Sep-11 12:12 
AnswerRe: C# designer tool Pin
Pravin Patil, Mumbai12-Sep-11 21:10
Pravin Patil, Mumbai12-Sep-11 21:10 
QuestionProblems with Site.Master Pin
Rick van Woudenberg12-Sep-11 0:38
Rick van Woudenberg12-Sep-11 0:38 
AnswerRe: Problems with Site.Master Pin
GenJerDan12-Sep-11 3:39
GenJerDan12-Sep-11 3:39 
AnswerRe: Problems with Site.Master Pin
RichardGrimmer12-Sep-11 5:41
RichardGrimmer12-Sep-11 5:41 
GeneralRe: Problems with Site.Master Pin
Rick van Woudenberg13-Sep-11 12:50
Rick van Woudenberg13-Sep-11 12:50 
Questionasp .net IE9 autocomplete Pin
scarface2112-Sep-11 0:12
scarface2112-Sep-11 0:12 
QuestionURL Rewritting rule Pin
Gaurav Goel (Team Lead)11-Sep-11 21:54
professionalGaurav Goel (Team Lead)11-Sep-11 21:54 
AnswerRe: URL Rewritting rule Pin
David C# Hobbyist.12-Sep-11 13:29
professionalDavid C# Hobbyist.12-Sep-11 13:29 
QuestionSys._SriptLoader._currentTask is null on Google Chrome [modified] Pin
Ali Al Omairi(Abu AlHassan)10-Sep-11 21:01
professionalAli Al Omairi(Abu AlHassan)10-Sep-11 21:01 
QuestionWork in offline mode when internet is offline Pin
md_refay10-Sep-11 20:44
md_refay10-Sep-11 20:44 
AnswerRe: Work in offline mode when internet is offline Pin
Blue_Boy11-Sep-11 5:46
Blue_Boy11-Sep-11 5:46 
QuestionDynamically generate Textboxes with Ajax AutoCompleteExtender how? Pin
anand kumbham9-Sep-11 23:42
anand kumbham9-Sep-11 23:42 
AnswerRe: Dynamically generate Textboxes with Ajax AutoCompleteExtender how? Pin
Ali Al Omairi(Abu AlHassan)11-Sep-11 20:04
professionalAli Al Omairi(Abu AlHassan)11-Sep-11 20:04 

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.