Last post Dec 11, 2007 05:12 PM by jgilmore
Dec 11, 2007 03:41 PM|jgilmore|LINK
I have an aspx page. On this page I have a textbox that askes the user how many items they are going to enter. Each item must represent one row of data the user will input. The user could enter a value from 1 to 100. The default value is 10 and is autopouplated
in the textbox for them when the page is built. The textbox id is "txtTripsToAdd" and was created in the design view hence it is not a dynamic control.
Now the complexity arrises:
Based on the value entere for txtTripsToAdd, the app will dynamicaly create the requested number of rows for the user. I do this by creating the textbox controls dynamicaly in the code behind. I do this in a method called RenderTopPreview(int trips, bool
readonly, Table tbl, string tab)
The first parameter is the number of trips to add. The rest of the parameteres dont matter for this question.
I call RenderTopPrview in the OnInit method since the textbox controls created in the RenderTopPreview() are dynamiclay and I must use FindControl to get their values when the Save button is clicked.
The OnInit Method:
The value of this.txtTripsToAdd.Text is 10 even if the user changes this. I beleive the reason this is so is because when I am calling the OnInit and getting the value it has not yet been updated and is using the orginal value.
I need to be able to use FindControl to get the values from the dynamically created controls BUT I also need to get the value from the textbox in order to create the right number of controls. What method should I override and put this in to accomplish my
goal. I know there are render methods and other methods as well, I just dont know where.
Dec 11, 2007 04:21 PM|richiej|LINK
I dont think u can from OInit as viewstate wont have been applied to your textbox control. I believe ViewState is applied AFTER Page Init. Do you have to determine how many trips u need at the Page Init event or can you not do this later in the page lifecyle,
say at Page_Load or Page_PreRender?
Dec 11, 2007 04:26 PM|Haissam|LINK
What you can do is when the user enters for example 10, you can send this value by the Querystring which can be read from the Oninit() event in order for you to create the dynamic controls
in the below example, i added the code inside the page_load event to handle the onclick event of a button with id Button1 to send the value of the txtTripsToAdd via querystring
Button1.Attributes.Add("onclick","window.location.href = '?num=' + document.getElementById('txtTripsToAdd').value;");
Now oninit() event you can use
if(Request.QueryString["num"] != null)
// Create the controls.
Though it is not tested.
Dec 11, 2007 04:27 PM|Charles Asbornsen|LINK
It would probably be best to store the number of rows in a session variable that gets updated when you change the textbox's text value. Then you don't have to go through the whole ASP page cycle to figure out where your values are - just get your number
of trips to add in your control's prerender, eg:
protected void GV1_PreRender(object sender, EventArgs e)
GV1.Rows = (Int32)Session["RowsToAdd"];
Dec 11, 2007 05:02 PM|jgilmore|LINK
Thanks for your response. My Control doesn't have a prerender (It is not a gridview, it is a server table that has dynamic rows, td's, and controls added to them) i.e:
tr.CssClass = strCssClass;
tr.ID = intRow.ToString();
Some td cells contain litterals, others contain textboxes while others include image buttons.
Additionaly, I would have to do a postback to store the value in the session as I do not know of a way you can store values in a session via client side code (as I think is what you are suggesting using an onchage js script for the textbox.) At that point,
I dont need to use a session since I can already access the value and use it whereever I need it (the value of the tripsToAdd textbox)
Dec 11, 2007 05:06 PM|jgilmore|LINK
is somethign that I will only know by looking at the code.
Dec 11, 2007 05:12 PM|jgilmore|LINK
Thanks for everyones ideas and help!