• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 530
  • Last Modified:

asp.net c# ascx

Please view my attached codes...how can i capture the value from the attached code
and save into the database?

protected void BtnSubmit_Click(object sender, EventArgs e)
    {
      //How can I capture the textbox value?
    }

I tried the following codes and it does not work...


TextBox tb = (TextBox)this.Page.FindControl("TxtItem_1");
            if (tb != null)
                Label2.Text = tb.Text;
            else
            {
                Label2.Text = "Nothing";
            }
protected void DdlTotalItem_SelectedIndexChanged(object sender, EventArgs e)
    {
        int TotalItemCount = int.Parse(DdlTotalItem.SelectedValue);
        if (TotalItemCount >= 1)
        {
            // DdlTotalItem.Visible = true;
            MasterListItem.Visible = true;
            MasterListItemTable.Visible = true;
            for (int i = 0; i < TotalItemCount; i++)
            {
                Obout.Interface.OboutTextBox ItemTextBox = new Obout.Interface.OboutTextBox();
                ItemTextBox.ID = "TxtItem_" + i;
                ItemTextBox.Width = 150;

              

                DivItemCode.Controls.Add(ItemTextBox);
                
            }
        }
        else
        {
            MasterListItem.Visible = false;
            MasterListItemTable.Visible = false;
        }
    }

Open in new window

0
Webboy2008
Asked:
Webboy2008
  • 6
  • 3
  • 3
1 Solution
 
Carlos VillegasFull Stack .NET DeveloperCommented:
What if...:
TextBox tb = (TextBox)this.DivItemCode.FindControl("TxtItem_1");
if (tb != null)
    Label2.Text = tb.Text;
else
{
    Label2.Text = "Nothing";
}

Open in new window

Where is located the previous code? inside the web control (ascx file)?
0
 
Webboy2008Author Commented:
protected void BtnSubmit_Click(object sender, EventArgs e)
    {
      //How can I capture the textbox value?
    }

I have for loop to generate the textbox...how can i get all in the button click event?
0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
I understand, but I need to know where is your BtnSubmit_Click (WebForm?) and where is your textboxes (Web Control?)
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
Webboy2008Author Commented:
Yes. it is a web form.
Where is the textbox? You can see in my questions.
it is created in dropdown changed event.


  Obout.Interface.OboutTextBox ItemTextBox = new Obout.Interface.OboutTextBox();
                ItemTextBox.ID = "TxtItem_" + i;
                ItemTextBox.Width = 150;
0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Ok, I see, you are creating yours controls dynamically, you must understand that every time your page do a postback you will need to recreate these controls, with the same ID, otherwise these will disappear from your page with every postback. The correct place to do that is in your Page Init event.

I recommend you read this:
http://aspadvice.com/blogs/joteke/archive/2006/01/06/14568.aspx
0
 
Webboy2008Author Commented:
I don't understand your recommendation. Please provide your codes if you know how...Thanks
0
 
P1ST0LPETECommented:
Your issue is that you are trying to access controls that you have created dynamically.  When you do a postback in asp.net, the page is completely made a new, and the only elements/controls that are on the page, are the ones that were created during design-time.  Any controls that were added programmatically during run-time have to be re-added programmatically on each postback.

So you need to separate your Textbox creation code into it's own method so it can be more reusable like this:
private void CreateItemTextBoxes(int TotalItemCount)
{
     for (int i = 0; i < TotalItemCount; i++)
     {
         Obout.Interface.OboutTextBox ItemTextBox = new Obout.Interface.OboutTextBox();
         ItemTextBox.ID = "TxtItem_" + i;
         ItemTextBox.Width = 150;
         DivItemCode.Controls.Add(ItemTextBox);                
     }
}

Open in new window


Making your DdlTotalItem_SelectedIndexChanged() method look like this:
protected void DdlTotalItem_SelectedIndexChanged(object sender, EventArgs e)
{
     int TotalItemCount = int.Parse(DdlTotalItem.SelectedValue);
     if (TotalItemCount >= 1)
     {
         // DdlTotalItem.Visible = true;
         MasterListItem.Visible = true;
         MasterListItemTable.Visible = true;
         CreateItemTextBoxes(TotalItemCount);
         
         //store DdlTotalItem.SelectedValue in database
     }
     else
     {
         MasterListItem.Visible = false;
         MasterListItemTable.Visible = false;
     }
}

Open in new window


Then on a postback, you need to re-create these controls inside of the Page_Init() method, which will fire before the Page_Load() method.  You use Page_Init() to re-construct dynamic controls, because during the Page_Load() event the asp.net engine rolls through all values in ViewState and tries to assign the values to their matching controls.  So you need your controls to exists as part of the page object before the Page_Load() event triggers.

Also, notice above that I added  "//store DdlTotalItem.SelectedValue in database" to your SelectedIndexChanged method.  You need to store this value in the database, because you wont be able to access it from your dropdownlist in Page_Init() beause the values from ViewState won't be assigned yet.

So the re-creating of your dynamic controls in Page_Init() will look something like this:
 
protected void Page_Init(object sender, EventArgs e)
{
    //re-create dynamic controls:
    int TotalItemCount = GetDatabaseValue(); //get stored DdlTotalItem.SelectedValue from database
    if (TotalItemCount >= 1)
    {
        CreateItemTextBoxes(TotalItemCount);
    }
}

Open in new window


Once you re-create these controls and add them to your page object, then you should be able to access them using the code you've already come up with:

TextBox tb = (TextBox)this.Page.FindControl("TxtItem_1");


See these links for more info on dynamic controls:
- http://msdn.microsoft.com/en-us/library/ms178472.aspx
- http://geekswithblogs.net/shahed/archive/2008/06/26/123391.aspx
- http://support.microsoft.com/kb/317515 (in VB, but you'll get the point)
0
 
Webboy2008Author Commented:
Thank you for your detail information. But I still don't know how to access the  value from textboxes.


TextBox tb = (TextBox)this.Page.FindControl("TxtItem_1");

I may have more than one "Txtitem_2..3,4.5.6....

How can I do  that?
0
 
P1ST0LPETECommented:
Well you should have your TotalItemCount value.  You'll want to loop through and do the same thing on each textbox:

for( int i = 0; i < TotalItemCount; i++)
{
       TextBox tb = (TextBox)this.Page.FindControl("TxtItem_" + i);
       // do something with the textbox
}
0
 
Webboy2008Author Commented:
protected void Page_Init(object sender, EventArgs e)
{
    //re-create dynamic controls:
    int TotalItemCount = GetDatabaseValue(); //get stored DdlTotalItem.SelectedValue from database
    if (TotalItemCount >= 1)
    {
        CreateItemTextBoxes(TotalItemCount);
    }
}

Get  GetDatabaseValue()? What it is? I never stored any total item in the database....

0
 
Webboy2008Author Commented:
And also alert message shown: Multiple controls with the same ID 'TxtItem_0' were found. FindControl requires that controls have unique IDs.

When I have the following codes as you suggested:


protected void Page_Init(object sender, EventArgs e)
{
    //re-create dynamic controls:
    int TotalItemCount = GetDatabaseValue(); //get stored DdlTotalItem.SelectedValue from database
    if (TotalItemCount >= 1)
    {
        CreateItemTextBoxes(TotalItemCount);
    }
}
0
 
P1ST0LPETECommented:
GetDatabaseValue() was just an example of you needing to create a database method to retrieve the TotalItems value from the database.  As I said above, you need to store the TotalItems value in the database (or some other means of storage), because you wont be able to get the current value in the Page_Init() function.

All the code I have above is just rough examples that you will need to tweak to make work.  It shouldn't be too hard, just set breakpoints in your code and analyse where you need to make adjustments.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

  • 6
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now