Click here to Skip to main content
15,897,371 members
Home / Discussions / C#
   

C#

 
QuestionGenerate Textbox on runtime Pin
Saim Ullah 20219-Mar-22 22:47
Saim Ullah 20219-Mar-22 22:47 
Hi Folks,
i have created a simple page which adds textbox dynamically. 1st textbox creates successfully but afterwards it showes error of object reference not set to an instance of an object.

it is not getting the ID of the panel which i contains the textbox.

my aspx file is....

<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="~/Header.Master" CodeFile="testpage.aspx.cs"
Inherits="testpage" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<asp:content id="HeaderContent" runat="server" contentplaceholderid="head">

<asp:content id="BodyContent" runat="server" contentplaceholderid="ContentPlaceHolder1">
<asp:panel id="pnlTextBoxes" runat="server">



<asp:button id="btnAdd" runat="server" text="Add New" onclick="AddTextBox">
<asp:button id="btnGet" runat="server" text="Get Values" onclick="GetTextBoxValues">



and .cs file is....

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

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

    }
     protected void Page_PreInit(object sender, EventArgs e)
    {
        List<string> keys = Request.Form.AllKeys.Where(key => key.Contains("txtDynamic")).ToList();
        int i = 1;
        foreach (string key in keys)
        {
            this.CreateTextBox("txtDynamic" + i);
            i++;
        }
    }

    protected void AddTextBox(object sender, EventArgs e)
    {
        int index = pnlTextBoxes.Controls.OfType<TextBox>().ToList().Count + 1;
        this.CreateTextBox("txtDynamic" + index);
    }

    private void CreateTextBox(string id)
    {
        TextBox txt = new TextBox();
        txt.ID = id;
        pnlTextBoxes.Controls.Add(txt);

        Literal lt = new Literal();
        lt.Text = "<br />";
        pnlTextBoxes.Controls.Add(lt);
    }

    protected void GetTextBoxValues(object sender, EventArgs e)
    {
        string message = "";
        foreach (TextBox textBox in pnlTextBoxes.Controls.OfType<TextBox>())
        {
            message += textBox.ID + ": " + textBox.Text + "\\n";
        }
        ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('" + message + "');", true);
    }
}

QuestionRe: Generate Textbox on runtime Pin
Richard MacCutchan9-Mar-22 23:38
mveRichard MacCutchan9-Mar-22 23:38 
AnswerRe: Generate Textbox on runtime Pin
Saim Ullah 202110-Mar-22 0:23
Saim Ullah 202110-Mar-22 0:23 
GeneralRe: Generate Textbox on runtime Pin
Richard MacCutchan10-Mar-22 0:31
mveRichard MacCutchan10-Mar-22 0:31 
GeneralRe: Generate Textbox on runtime Pin
Saim Ullah 202110-Mar-22 0:41
Saim Ullah 202110-Mar-22 0:41 
GeneralRe: Generate Textbox on runtime Pin
Richard MacCutchan10-Mar-22 0:45
mveRichard MacCutchan10-Mar-22 0:45 
AnswerRe: Generate Textbox on runtime Pin
OriginalGriff10-Mar-22 0:12
mveOriginalGriff10-Mar-22 0:12 
GeneralRe: Generate Textbox on runtime Pin
Saim Ullah 202110-Mar-22 0:34
Saim Ullah 202110-Mar-22 0:34 
GeneralRe: Generate Textbox on runtime Pin
OriginalGriff10-Mar-22 1:08
mveOriginalGriff10-Mar-22 1:08 
GeneralRe: Generate Textbox on runtime Pin
Saim Ullah 202110-Mar-22 7:59
Saim Ullah 202110-Mar-22 7:59 
AnswerRe: Generate Textbox on runtime Pin
RedDk10-Mar-22 8:26
RedDk10-Mar-22 8:26 
GeneralRe: Generate Textbox on runtime Pin
Richard MacCutchan10-Mar-22 22:21
mveRichard MacCutchan10-Mar-22 22:21 
GeneralRe: Generate Textbox on runtime Pin
RedDk11-Mar-22 8:06
RedDk11-Mar-22 8:06 
QuestionC# File.Create terminating WimForm app wiith no error thrown ? Pin
BillWoodruff8-Mar-22 5:03
professionalBillWoodruff8-Mar-22 5:03 
AnswerRe: C# File.Create terminating WimForm app wiith no error thrown ? Pin
Pete O'Hanlon8-Mar-22 5:24
mvePete O'Hanlon8-Mar-22 5:24 
GeneralRe: C# File.Create terminating WimForm app wiith no error thrown ? Pin
BillWoodruff8-Mar-22 6:27
professionalBillWoodruff8-Mar-22 6:27 
GeneralRe: C# File.Create terminating WimForm app wiith no error thrown ? Pin
Pete O'Hanlon8-Mar-22 20:42
mvePete O'Hanlon8-Mar-22 20:42 
GeneralRe: C# File.Create terminating WimForm app wiith no error thrown ? Pin
BillWoodruff8-Mar-22 21:19
professionalBillWoodruff8-Mar-22 21:19 
AnswerRe: C# File.Create terminating WimForm app wiith no error thrown ? Pin
Richard Deeming8-Mar-22 21:49
mveRichard Deeming8-Mar-22 21:49 
GeneralRe: C# File.Create terminating WimForm app wiith no error thrown ? Pin
BillWoodruff8-Mar-22 22:08
professionalBillWoodruff8-Mar-22 22:08 
AnswerRe: C# File.Create terminating WimForm app wiith no error thrown ? Pin
Randor 9-Mar-22 9:05
professional Randor 9-Mar-22 9:05 
GeneralRe: C# File.Create terminating WimForm app wiith no error thrown ? Pin
BillWoodruff9-Mar-22 9:29
professionalBillWoodruff9-Mar-22 9:29 
GeneralRe: C# File.Create terminating WimForm app wiith no error thrown ? Pin
Randor 9-Mar-22 9:36
professional Randor 9-Mar-22 9:36 
GeneralRe: C# File.Create terminating WimForm app wiith no error thrown ? Pin
BillWoodruff9-Mar-22 22:00
professionalBillWoodruff9-Mar-22 22:00 
GeneralRe: C# File.Create terminating WimForm app wiith no error thrown ? Pin
Randor 9-Mar-22 22:56
professional Randor 9-Mar-22 22:56 

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.