Custom compound WebControl accessing elements on postback...?

K

kw

I have a custom WebControl that creates an array of textboxes (naming them
dynamically), for example:

<INPUT id="c0r0" type="text" runat=server value="Item A" ><INPUT id="c1r0"
type="text" runat=server value="100" ><br>

Now let's say the user changed the value of c1r0 to from '100' to '200' and
clicks an 'Update' button. In the Page_Load postback, none of the controls
are accessible.

How can I access the value of the text input element c1r0?

Or do you think I'm going about this the wrong way?

Thanks!

Dan
 
D

David Alexander

You need to manage state so that when your user submits the page back to the
server, the server knows which controls it dynamically created and named
when it created the page, so that it knows which controls and values to look
for.

State management is a fairly complex subject, but Microsoft's MSDN article
at
http://msdn.microsoft.com/msdnmag/issues/03/04/ASPNETUserState/default.aspx
gives a good introduction. You need to store information about the controls
you created so that when it comes time to access information about them, you
have it squirreled away somewhere.
 
K

kw

Hi David, I got the state management thing down now, but the issue here is
that the postback recreates the control the way it was before the user made
an update.

In the example cited, on postback this prints '100' (the old value, not
'200', the new value):
if(!IsPostBack)Debug.WriteLine(this.Spreadsheet1.Cells["c1r0"].Value);//prin
ts the OLD value

The original form has these controls created by the webcontrol:
<INPUT id="c0r0" type="text" runat=server value="Item A" ><INPUT id="c1r0"
type="text" runat=server value="100" ><br>
And the user has changed it from 100 to 200.

On the postback, I see no way of getting the users NEW value. Also, putting
runat=server seems to have no effect.
 
K

kw

I had tried something like that and here are the results:
System.Web.UI.Control c2 = this.Spreadsheet1.FindControl("c1r0");//returns
null

System.Web.UI.Control c1 = this.Page.FindControl("c1r0" );//returns null

System.Web.UI.Control c0 = this.FindControl("c1r0" );//returns null

System.Diagnostics.Debug.WriteLine(this.Spreadsheet1.Cells["c1r0"].Value);//
returns OLD value

Here are source of the page that was created:

<HTML>
<HEAD>
...brevity...
</HEAD>
<body>
<form name="Form1" method="post" action="WebForm1.aspx" id="Form1">
<input type="hidden" name="__VIEWSTATE" value="...brevity..." />

<INPUT id="c0r0" type="text" runat=server value="Item A" >
<INPUT id="c1r0" type="text" runat=server value="100" ><br>

<input type="submit" name="btnSpreadsheetUpdate" value="Update"
id="btnSpreadsheetUpdate" />
</form>
</body>
</HTML>


I don't see why I can't access the values of the posted back form.
 
K

kw

Oops! It should have read
if(IsPostBack)...
not if(!IsPostBack)...
kw said:
Hi David, I got the state management thing down now, but the issue here is
that the postback recreates the control the way it was before the user made
an update.

In the example cited, on postback this prints '100' (the old value, not
'200', the new value):
if(!IsPostBack)Debug.WriteLine(this.Spreadsheet1.Cells["c1r0"].Value);//prin
ts the OLD value

The original form has these controls created by the webcontrol:
<INPUT id="c0r0" type="text" runat=server value="Item A" ><INPUT id="c1r0"
type="text" runat=server value="100" ><br>
And the user has changed it from 100 to 200.

On the postback, I see no way of getting the users NEW value. Also, putting
runat=server seems to have no effect.



David Alexander said:
You need to manage state so that when your user submits the page back to the
server, the server knows which controls it dynamically created and named
when it created the page, so that it knows which controls and values to look
for.

State management is a fairly complex subject, but Microsoft's MSDN article
at
http://msdn.microsoft.com/msdnmag/issues/03/04/ASPNETUserState/default.aspx
gives a good introduction. You need to store information about the controls
you created so that when it comes time to access information about them, you
have it squirreled away somewhere.


'200'
and
 
K

kw

Hmmm. I attached the spreadsheet control and it appears in my Outlook
Express newsgroup reader. I wrote the control All it's doing using using
Rows and Columns to create the array of <Input type='text' id=cXrY
runat=server> elements and persisting the cells collection via ViewState.

I'm not sure what I did wrong with the control. It creates and regenerates
the control just fine except it doesn't allow the values to be updated.

If you are interested, email me directly at (e-mail address removed)
and I'll send you the whole project. This is my first fairly complicated
server control, so I'm not sure what I'm really doing.


Scott Simes said:
Hi,

I didn't see the spreadsheet control (it was not attached). But from your
code it seems that you are not actually adding TextBox controls. If you're
trying to read the content of a cell within your MayerSoft control, you need
to follow the control's documented method for doing this. Control authors
can do all sorts of things to handle values. This control apparently uses a
"Cells" collection--but it is unclear whether each Cell object is a
container for other controls or if it is simply a non-container object with
properties. I would think you can use ...Cells["r1c1"].Value, or something
similar to obtain the value of the control.

I know you saw input boxes in the HTML output, but this has nothing to do
with how those boxes are represented SERVER-SIDE. Your spreadsheet object
has total ownership of how HTML is abstracted, so the only way to use the
object to parse client-side input is to refer to the control's own
documentation...

Does this help?

kw said:
Hi Scott, I tried it both from the Page_Load and from the
btnSpreadsheetUpdate_Click event, neither worked. Here is the test
page.
I
attached the actual WebControl (Spreadsheet.cs). Thanks so much for looking
at this...I'm really stumped as to how to get my values back.
public class WebForm1 : System.Web.UI.Page

{

protected System.Web.UI.WebControls.Button btnSpreadsheetUpdate;

protected MayerSoft.Web.Controls.Spreadsheet Spreadsheet1;


private void Page_Load(object sender, System.EventArgs e)

{

if(!IsPostBack)

{

this.Spreadsheet1.MaxRows=5;

ColumnCollection cols=new ColumnCollection();

//this.Spreadsheet1.Columns.Clear();

//this.Spreadsheet1.Columns.Add(new MayerSoft.Web.Controls.Column(150));

//this.Spreadsheet1.Columns.Add(new MayerSoft.Web.Controls.Column(75));

cols.Add(new Column(150));

cols.Add(new Column(75));

this.Spreadsheet1.Columns=cols;

CellCollection cells=new CellCollection();

cells.Add(new MayerSoft.Web.Controls.Cell(0,0,"Item
A",string.Empty,"Label",string.Empty,true));

cells.Add(new
MayerSoft.Web.Controls.Cell(1,0,"100",string.Empty,"Number",string.Empty,fal
se));

this.Spreadsheet1.Cells=cells;

}

else

{

System.Web.UI.Control c2 = this.Spreadsheet1.FindControl("c1r0");//returns
null

System.Web.UI.Control c1 = this.Page.FindControl("c1r0" );//returns null

System.Web.UI.Control c0 = this.FindControl("c1r0" );//returns null
System.Diagnostics.Debug.WriteLine(this.Spreadsheet1.Cells["c1r0"].Value);//
returns OLD value

}

}
System.Diagnostics.Debug.WriteLine(this.Spreadsheet1.Cells["c1r0"].Value);//
 
B

brisers

My problem was based on not creating my dynamic columns in the Init handler of the DataGrid control. When the dynamic controls are created here, ViewState and event routing work correctly. If they are created later, that 'plumbing' is missing. A different problem but related insofar as the timing of this stuff seems to be really critical in ASP.NET.

Thanks for your help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top