![]() |
Custom compound WebControl accessing elements on postback...?
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 |
Re: Custom compound WebControl accessing elements on postback...?
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" <elitecommerceserver@hotmail.com> wrote in message news:%23K3WKGzcEHA.3664@TK2MSFTNGP12.phx.gbl... > 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 > > |
Re: Custom compound WebControl accessing elements on postback...?
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" <no-email@nodomain.net> wrote in message news:uUXEtazcEHA.3476@tk2msftngp13.phx.gbl... > 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" <elitecommerceserver@hotmail.com> wrote in message > news:%23K3WKGzcEHA.3664@TK2MSFTNGP12.phx.gbl... > > 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 > > > > > > |
Re: Custom compound WebControl accessing elements on postback...?
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" <usa_NO8SPAM004@yahoo.com> wrote in message news:0obNc.46060$qa2.26129@fe2.texas.rr.com... > 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" <elitecommerceserver@hotmail.com> wrote in message > news:%23K3WKGzcEHA.3664@TK2MSFTNGP12.phx.gbl... > > 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 > > > > > > |
Re: Custom compound WebControl accessing elements on postback...?
Oops! It should have read
if(IsPostBack)... not if(!IsPostBack)... "kw" <elitecommerceserver@hotmail.com> wrote in message news:e74JxhzcEHA.3716@TK2MSFTNGP11.phx.gbl... > 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" <no-email@nodomain.net> wrote in message > news:uUXEtazcEHA.3476@tk2msftngp13.phx.gbl... > > 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" <elitecommerceserver@hotmail.com> wrote in message > > news:%23K3WKGzcEHA.3664@TK2MSFTNGP12.phx.gbl... > > > 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 > > > > > > > > > > > > |
Re: Custom compound WebControl accessing elements on postback...?
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 elitecommerceserver@hotmail.com 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" <usa_NO8SPAM004@yahoo.com> wrote in message news:ptcNc.47028$qa2.34482@fe2.texas.rr.com... > 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" <elitecommerceserver@hotmail.com> wrote in message > news:O1fih0zcEHA.2840@TK2MSFTNGP11.phx.gbl... > > 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" <usa_NO8SPAM004@yahoo.com> wrote in message > > news:GLbNc.46385$qa2.4249@fe2.texas.rr.com... > > > 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" <elitecommerceserver@hotmail.com> wrote in message > > > news:Otnc1nzcEHA.2544@TK2MSFTNGP10.phx.gbl... > > > > 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" <usa_NO8SPAM004@yahoo.com> wrote in message > > > > news:0obNc.46060$qa2.26129@fe2.texas.rr.com... > > > > > 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" <elitecommerceserver@hotmail.com> wrote in message > > > > > news:%23K3WKGzcEHA.3664@TK2MSFTNGP12.phx.gbl... > > > > > > 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 > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > |
Re: Custom compound WebControl accessing elements on postback...?
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. |
| All times are GMT. The time now is 12:57 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.