Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Custom compound WebControl accessing elements on postback...?

Reply
Thread Tools

Custom compound WebControl accessing elements on postback...?

 
 
kw
Guest
Posts: n/a
 
      07-26-2004
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


 
Reply With Quote
 
 
 
 
David Alexander
Guest
Posts: n/a
 
      07-26-2004
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/is...e/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.


"kw" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
 
 
 
kw
Guest
Posts: n/a
 
      07-26-2004
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.C ells["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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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/is...e/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.
>
>
> "kw" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
> > 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
> >
> >

>
>



 
Reply With Quote
 
kw
Guest
Posts: n/a
 
      07-26-2004
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.Spreadshee t1.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.



"Scott Simes" <(E-Mail Removed)> wrote in message
news:0obNc.46060$(E-Mail Removed)...
> If you have the ID of a control, use the control's parent's FindControl
> method to access the control by control ID. It is important that you have
> the immediate parent of the textbox control when you do this. So, if your
> "WebControl" itself has a parent, you must dereference from that parent,

and
> so on... Here's some pseudocode:
>
> Textbox t = (Textbox)Parent3.Parent2.Parent1.FindControl("c0r0 ");
>
> string value = t.Text;
>
> "kw" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
> > 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
> >
> >

>
>



 
Reply With Quote
 
kw
Guest
Posts: n/a
 
      07-26-2004
Oops! It should have read
if(IsPostBack)...
not if(!IsPostBack)...
"kw" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.C ells["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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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/is...e/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.
> >
> >
> > "kw" <(E-Mail Removed)> wrote in message
> > news:%(E-Mail Removed)...
> > > 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
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
kw
Guest
Posts: n/a
 
      07-26-2004
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 http://www.velocityreviews.com/forums/(E-Mail 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" <(E-Mail Removed)> wrote in message
newstcNc.47028$(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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.Spreadshee t1.Cells["c1r0"].Value);//
> > returns OLD value
> >
> > }
> >
> > }
> >
> >
> >
> > "Scott Simes" <(E-Mail Removed)> wrote in message
> > news:GLbNc.46385$(E-Mail Removed)...
> > > Within what server-side function are you executing your "FindControl"

> > code?
> > > To save time and help with the issue, can you post aspx and aspx.cs

> code?
> > >
> > > "kw" <(E-Mail Removed)> wrote in message
> > > news:(E-Mail Removed)...
> > > > 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.Spreadshee t1.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.
> > > >
> > > >
> > > >
> > > > "Scott Simes" <(E-Mail Removed)> wrote in message
> > > > news:0obNc.46060$(E-Mail Removed)...
> > > > > If you have the ID of a control, use the control's parent's

> > FindControl
> > > > > method to access the control by control ID. It is important that

you
> > > have
> > > > > the immediate parent of the textbox control when you do this. So,

if
> > > your
> > > > > "WebControl" itself has a parent, you must dereference from that

> > parent,
> > > > and
> > > > > so on... Here's some pseudocode:
> > > > >
> > > > > Textbox t = (Textbox)Parent3.Parent2.Parent1.FindControl("c0r0 ");
> > > > >
> > > > > string value = t.Text;
> > > > >
> > > > > "kw" <(E-Mail Removed)> wrote in message
> > > > > news:%(E-Mail Removed)...
> > > > > > 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
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >

> >
> >
> >

>
>



 
Reply With Quote
 
brisers
Guest
Posts: n/a
 
      07-28-2004
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
XSLT: iterating all child elements and accessing homonymous childrenin sibling elements Gerald Aichholzer XML 2 06-27-2006 03:46 PM
Custom webcontrol that references another webcontrol Fabricio ASP .Net Building Controls 0 09-21-2005 02:55 AM
Custom WebControl, custom property list Curt_C [MVP] ASP .Net Web Controls 0 09-01-2005 04:09 PM
Trapping an Exception ocurring in a Webcontrol at the Page or parent WebControl level. ASP .Net 2 01-31-2005 06:34 PM
Can a webcontrol (webcontrol.dll) have a configuration file? Luis Ramírez. ASP .Net Building Controls 2 07-06-2004 04:35 PM



Advertisments