Getting the recreated control's id's by viewstate?


How can i get the automaticly created fileupload control's id's by stroring them in the viewstate? Is it possible? or any other way to get them on the run-time?
I can store button's click number and then create dynamic controls on the run-time and if i look at the source code i can see the auto-created id's but when i click submit button to upload files in fileupload controls i get an NullReferenceException. I guess when i click submit button all the id's gone because the page posts back. So i write this to read id's and assign to my fileupload control, here is the code for it ,


1    for (int i = 1; i <= ButtonClick; i++)
2                {
3                    myUpload = ((FileUpload)pnlUpload.FindControl("upload_" + i.ToString()));
4                    if (myUpload.HasFile)
5                     {
6                        upload(myUpload.PostedFile.FileName);
7                        lblSonuc.Text = "basari";
8                      }
9                }

 For creating dynamic controls i wrote this, 


1    public int ButtonClick
2        {
3            get
4            {
5                return (int)ViewState["ButtonClick"];
6            }
7            set
8            {
9                ViewState["ButtonClick"] = value;
10           }
11       }
13       protected void lnkEkle_Click(object sender, EventArgs e)
14       {
15           ++ButtonClick;
16           for (int i = 0; i < ButtonClick; i++)
17           {
18               myUpload = new FileUpload();
19               myUpload.ID = "upload_" + i.ToString();
20               this.pnlUpload.Controls.Add(myUpload);
21           }
22           lblSonuc.Visible = true;
23       }

 What else i have to do? I'm totally stuck to this subject and never let it to adn just want to learn is it possible? It throws that exception on line #4 because i guess this control is not created and i found that i can make it on OnInit but i don't know how to use it.


3/16/2007 11:19:09 AM 93655 articles. 5 followers. Follow

3 Replies

Similar Articles

[PageSpeed] 40

in if you are creating controls dynamically and if u want to persist these controls

during postback then in Page_Init() event you must have to create controls again with the same ids.

 So you have to put this code in Page_Init() event

for (int i = 0; i < ButtonClick; i++)
17 {
18 myUpload = new FileUpload();
19 myUpload.ID = "upload_" + i.ToString();
20 this.pnlUpload.Controls.Add(myUpload);
21 }
I already face this problem in past. 


With Regards,
Mayank Prajapati
MCP, MCTS .Net 2.0

If this is a correct answer then please mark this as answer.
3/16/2007 12:56:46 PM
Well this time i get a NullReferenceException in  
1    public int ButtonClick
2        {
3            get
4            {
5                return (int)ViewState["ButtonClick"];
6            }
7            set
8            {
9                ViewState["ButtonClick"] = value;
10           }
11       }
line 5, so do i also have to put property definition into Page_Init() ? 
3/16/2007 1:07:46 PM


You can create them in Page_load which occurs after the viewstate has been loaded.

Hope it helps.

3/19/2007 5:48:13 AM

Similar Artilces:

How can I get out of my Mozilla's browser the Winamp browser and get back the Google's or Mozilla's browser.
Please I have got to my browser ( I don't know how.) the Winamp browser he behave like virus stick in.But he doesn't has uninstalation section so I can't do anything to get him away from Mozilla's file. I'm not happy of Winamp's behave its like advertisement against a customer's will. Please is there any way to get him away from Mozilla?Please send me exact direction I have tried many ways but he is deeply inside the systeme so when I install new Mozilla he is always here ready to pester me. Thank you for an answer. Richard ...

Replace 1's and 0's in a gridview column with Yes's and No's
Is it possible to change the display of a column in a gridview to show a Yes for all 1's and a No for 0's that display in a particular column of a gridview bound to a database?  If so, can someone share the way with me?  I am using ASP.NET 2.0 with VB Code Behind.  Thanks One way you can do is thrrough  CASE in your select statement: Select YesNoColumn= CASE yourColumn WHEN 1 THEN 'Yes' WHEN 0 THEN 'No' ELSE '' END FROM YourtableLimno <Columns>     <asp:TemplateField HeaderText="yourHeader" SortExpression="yourDataField"> ...

It's Linux !? No, it's BSD ! No, no, it's...
kFreeBSD a Debian distro using the FreeBSD kernel ! ObiWan wrote: > kFreeBSD > > > > a Debian distro using the FreeBSD kernel ! Why use a BSD kernel? I hope the reason is more than, just because they can. It's easy to understand why the would want to use Debian packages. :) -- Jimmy Johnson Registered Linux User #380263 >> kFreeBSD >> >> >> ...

how to control a RadioButtonList's Listitem's text show according database's field
Hi my page has a repeater control,in repeater has a RadioButtonList control now i want to let the RadioButtonList has four Listitem,every Listitem's text is a database's field so the listitem can show dynamic according every record how can i work out my problem thanks a lot  hello dearbound your RadioButtonList to database if i m not clear fell free 4 replySatyabrat SinghB.E.(C.Sc.)Web developer Livetek Solution Dont forget to click "Mark as Answer" on the post that helped you. yes.i bound the radiobuttonlist to a dataset but how to display the ...

Remove Control's ViewState when it's no longer needed
To improve performance, is it possible to remove a control's ViewState values when they will no longer needed?I've tried setting EnableViewState to false once the control will no longer be appearing, but this doesn't seem to reduce the ViewState's size. FYI if a control is hidden it does not have its value in ViewState.Long Live .NETKazi Manzur Rashid (Amit)_________________________Web: http //dotnetshoutout.comBlog:  I'm not sure if this is correct, if by "hidden" you mean Visible=false.I think I ca...

Mozilla get's no style tag's from .net server.
I made a webform in web matrix, and it looks fine in IE. If I open the same link whit Mozilla, it looks like a disaster. When I look at the HTML source, there are no style tags as in IE. But when I save the HTML source from IE (the one whit style tags) and open it in Mozilla, it looks perfect. So the onley reason the webform looks wrong in Mozilla, is because the .net server detects Mozilla and not IE and sends a style less HTML source. Wy? Mozilla does support style tags. Is there a solution for this bug? Or is this a uh.. feature? ..Mivz Update, I have got some more info. It is ...

Help! I cannot access the DataGrid's ItemTemplate's LinkButton's Text Atrribute 's Value!!!!!!!!!!!
I write a datagrid's ItemDataBound method as follows:   private void dgTable_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)  {   if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)   {    if(!isRole("Manager"))    {     LinkButton tmp = (LinkButton)e.Item.FindControl("lbPassed");     if(tmp.Text == "Passed")     {      ((LinkBu...

Hyperlink's, ImageURL's, and OnError's
I created an ASP:Hyperlink and set the ImageURL attribute so the link will be the image.If the image is missing, I would like it to display a 'missing image' pic that I have.  I know you can use the OnError attribute of the <img> tag to run some javascript to change it.Unfortunately I can't find a way to access the Hyperlink's Image that is inside it, so I can add an Attribute so the resulting <img> tag will have an OnError attribute inside of it.Any help?If my Hyperlink is called 'foo'....would I do...  ((Image) foo.Controls[0]).Attributes.Add(&...

Treeview control: how do you access client id's for each node's checkbox?
Is it possible to get the client id's for the checkboxes that are created by a treeview control?  I need to create some javascript that will manipulate the checked status of the boxes on the client side, but can not figure out how to determine the client id's for each checkbox. I am in a situation where the tree can have 4 levels of nodes, and they are not populated unless the user expands a node.  This causes the client id's to constantly change as they are numerically numbered from the top down. Thanks, Billy You can get the ClientID of a checkbox inside javascript as follow...

Who's show is SN? Steve's or Leo's?
I don't want to start anything (honestly)! Just want to ask a simple question. Does the Security Now podcast belong to Steve Gibson or Leo Laporte? Maybe I'm in a world all to myself, but when I listen to the excellent SN podcasts, I want to hear STEVE, not Leo. But especially in the past few shows, it seems Leo will repeatedly interrupt Steve in the middle of a sentence that I want to hear the rest of. I consider this very rude of Leo. Steve is making a point that I (we) want to hear, then looses his train of thought after Leo barges in and asks something. It...

DataSet's DataTable's and TableAdapter's Oh My!
Here is my situation.  I am using VB as my programming language.I am creating a part of a website that will be the admin section.  I want the admin to be able to add/edit data in a set of tables that normal users can't.  There are 20 lookup tables that I use in the website that I want the admin to be able to manage (i.e. add, edit, delete, insert).  I don't want to create 20 separate web pages (one for each table).  I have one page that has links to all the tables.  Each link has a "table" querystring variable associated with it.  When the...

Guru's..?? Any do's and/or dont's for Zfd4 and Citrix..?
We've got a nw6/sp5 network with GW 6.5, bm3.7 and zfd 4.01 up'n'running.. Now we're adding a citrix server ( win2003) to the mix, will supply a couple off app's to users over the internal VPN in another city. Probably we'll skip the AD phase since we can not see any app and/or user with the need for AD. So,, any special considorations..? tip's'tricks..? known issues...? t.i.a... On Mon, 16 Aug 2004 15:57:10 GMT, Emerson wrote: > So,, any special considorations..? tip's'tricks..? known issues...? biggest one... zfd4.x d...

Modify Web Control in Function by Passing in Web Control's ID as Parameter
Good day to all, I am trying to modify web controls through functions by passing in their ID as the parameter so that I can call the function whether a control's properties need to be modify. The rough idea I have is as follows: (Function, In VB) Function ModifyTextboxText(Byval SampleTextbox) SampleTextBox.text = "Textbox contents changed." End function (Calling the function) ModifyTextboxText( Has anyone got any idea how I could proceed with the coding? Appreciate any help around here. Its for my school project. Thanks!...

Master page id's and rendered id's not matching up
Hi all, I've got a question... I'm pretty new to masterpages and have a simple one setup that looks like this.<html xmlns="" ><head runat="server">    <title>Simple test</title></head><body>    <form id="form1" runat="server">    <strong>form</strong>    </form></body></html>however when I view the rendered source code the form line looks like this<form name="aspnetForm"...

Web resources about - Getting the recreated control's id's by viewstate? -

Resources last updated: 1/3/2016 9:27:58 PM