?
Solved

asp.net c# ascx

Posted on 2011-10-03
12
Medium Priority
?
517 Views
Last Modified: 2012-05-12
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
Comment
Question by:Webboy2008
  • 6
  • 3
  • 3
12 Comments
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 36906674
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
 

Author Comment

by:Webboy2008
ID: 36906696
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
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 36906706
I understand, but I need to know where is your BtnSubmit_Click (WebForm?) and where is your textboxes (Web Control?)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:Webboy2008
ID: 36906737
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
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 36906863
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
 

Author Comment

by:Webboy2008
ID: 36906934
I don't understand your recommendation. Please provide your codes if you know how...Thanks
0
 
LVL 10

Expert Comment

by:P1ST0LPETE
ID: 36906997
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
 

Author Comment

by:Webboy2008
ID: 36907017
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
 
LVL 10

Expert Comment

by:P1ST0LPETE
ID: 36907101
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
 

Author Comment

by:Webboy2008
ID: 36907296
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
 

Author Comment

by:Webboy2008
ID: 36907327
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
 
LVL 10

Accepted Solution

by:
P1ST0LPETE earned 2000 total points
ID: 36907641
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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question