Custom compound WebControl accessing elements on postback...?

Discussion in 'ASP .Net Web Controls' started by kw, Jul 26, 2004.

  1. kw

    kw Guest

    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
    kw, Jul 26, 2004
    #1
    1. Advertising

  2. 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.


    "kw" <> wrote in message
    news:%...
    > 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
    >
    >
    David Alexander, Jul 26, 2004
    #2
    1. Advertising

  3. kw

    kw Guest

    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" <> wrote in message
    news:...
    > 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.
    >
    >
    > "kw" <> wrote in message
    > news:%...
    > > 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
    > >
    > >

    >
    >
    kw, Jul 26, 2004
    #3
  4. kw

    kw Guest

    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.



    "Scott Simes" <> wrote in message
    news:0obNc.46060$...
    > 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" <> wrote in message
    > news:%...
    > > 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
    > >
    > >

    >
    >
    kw, Jul 26, 2004
    #4
  5. kw

    kw Guest

    Oops! It should have read
    if(IsPostBack)...
    not if(!IsPostBack)...
    "kw" <> wrote in message
    news:...
    > 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" <> wrote in message
    > news:...
    > > 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.
    > >
    > >
    > > "kw" <> wrote in message
    > > news:%...
    > > > 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
    > > >
    > > >

    > >
    > >

    >
    >
    kw, Jul 26, 2004
    #5
  6. kw

    kw Guest

    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
    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" <> wrote in message
    news:ptcNc.47028$...
    > 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" <> wrote in message
    > news:...
    > > 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
    > >
    > > }
    > >
    > > }
    > >
    > >
    > >
    > > "Scott Simes" <> wrote in message
    > > news:GLbNc.46385$...
    > > > 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" <> wrote in message
    > > > news:...
    > > > > 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.
    > > > >
    > > > >
    > > > >
    > > > > "Scott Simes" <> wrote in message
    > > > > news:0obNc.46060$...
    > > > > > 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" <> wrote in message
    > > > > > news:%...
    > > > > > > 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
    > > > > > >
    > > > > > >
    > > > > >
    > > > > >
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >
    > >

    >
    >
    kw, Jul 26, 2004
    #6
  7. kw

    brisers Guest

    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.
    brisers, Jul 28, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest
    Replies:
    2
    Views:
    730
  2. Gerald Aichholzer
    Replies:
    2
    Views:
    2,502
    Gerald Aichholzer
    Jun 27, 2006
  3. Luis Ramírez.

    Can a webcontrol (webcontrol.dll) have a configuration file?

    Luis Ramírez., Jul 5, 2004, in forum: ASP .Net Building Controls
    Replies:
    2
    Views:
    197
    Luis Ramírez
    Jul 6, 2004
  4. Fabricio

    Custom webcontrol that references another webcontrol

    Fabricio, Sep 21, 2005, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    177
    Fabricio
    Sep 21, 2005
  5. Curt_C [MVP]

    Custom WebControl, custom property list

    Curt_C [MVP], Sep 1, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    176
    Curt_C [MVP]
    Sep 1, 2005
Loading...

Share This Page