Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / Building Controls / November 2005

Tip: Looking for answers? Try searching our database.

ViewState and posted back data

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tumurbaatar S. - 27 Nov 2005 22:13 GMT
I'm adding a template column to a DataGrid dynamically:

private void CreateColumn()
{
   TemplateColumn col = new TemplateColumn();
   ...//set some col properties
   col.EditItemTemplate = new MyTemplate();
   MyGrid.Columns.Add(col);
}

The MyTemplate class implementes InstantiateIn() method as:

public void InstantiateIn(Control container)
{
   TableCell cell = container as TableCell;
   TextBox txt = new TextBox();
   txt.ID = "MyTextBox";
   container.Controls.Add(txt);
   RequiredFieldValidator req = new RequiredFieldValidator();
   req.ID = "MyReqVal";
   req.ControlToValidate = txt.ID;
   container.Controls.Add(req);
}

Inside DataGrid.ItemDataBound event handler I set initial value
of the above MyTextBox as:

private void MyGrid_ItemDataBound(object sender, DataGridItemEventArgs e)
{
   if (e.Item.ItemType == ListItemType.EditItem)
   {
       (e.Item.Cells[N].Controls[0] as TextBox).Text = SomeValue;
   }
}

And I call CreateColumn() from Page_Load() on the first visit
and from overrided LoadViewState() of the Page on every postback:

protected override void LoadViewState(object savedState)
{
   base.LoadViewState(savedState);
   CreateColumns();
}

And this works well. The MyGrid switches to edit mode normally and
renders the MyTextBox with a bounded value. The MyReqVal validates
MyTextBox properly at a client side.
But when I postback the page to save a text entered in MyTextBox,
the Page.IsValid is always FALSE. Also, MyTextBox is always EMPTY:

private void SaveBtn_Click(object sender, System.EventArgs e)
{
   //at this point the IsValid is false and MyTextBox.Text is blank
   if (IsValid)
   {
       ...// cannot reach here!
   }
}

What am I doing wrong?
Sundararajan - 29 Nov 2005 15:10 GMT
Please check whether the control ids remain the same when u post back the
data. u can do this by downloading viewstate decoders and analysing ur view
state.
because i was facing a similar problem days before which was solved after
analysing the viewstate information by viewstate decoders
regards
Signature

S.Sundararajan

> I'm adding a template column to a DataGrid dynamically:
>
[quoted text clipped - 56 lines]
>
> What am I doing wrong?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.