Using FindControl To Locate TextBox In Dynamically Created Template Field?
Jun 2, 2010
My problem is as follows. I have a custom GridView control where I generate the Template fields for each column. I'm using someone elses class that extends the Itemplate and returns lables for ItemTemplate, and TextBoxes for EditTemplate.
I need this in order to implement bulk edit for the gridview where users press button Edit and all fields become textboxes, they can then change the values, and click Update button at which point I need to be able to retrieve the values from the TextBoxes in these Template Fields.
What I have tried thus far: when user clicks Edit button, my dynamic code builds the columns and specifies that the Template field is an EditTemplate, it then adds these columns to the grid view. The gridview shows the textboxes with all values retrieved from sqldatasource.
When I press the Update button, the page does a postback, as I can see Page_Load event fire, I check for a flag that I previously set when the Edit button was pressed (I store it's value in View State) and call the Update method.
In it I iterate throught the gridview rows collection, check that the RowType of each row is DataRow and call GridView.UpdateRow(i, false), passing each rows index to the UpdateRow method.At this point I have tried everything I can to find the values stored in the textboxes, but I cannot find any of the textbox controls.
I've tried using the current row's FindControl method and specifying the name of the textbox field,I've tried using Request.Params method where I can tried passing the client id, as it seen in the View Source of the page such as this
Request.Params["grid1$gvData$ctl02$MMCODE"]
Still no luck, I've tried going up an down the Row's cells, controls collections, it seem to list the counts for rows correctly, the same goes for the amount of cells for each row, but at no point was I able to actually locate any controls such as textboxes or anything else and retrieve any values from them.
I have this gridview made.But it gets populated with columns, column names and template fields dynamically. The template field tht I am using throught the field in each colum is a textbox. The user when loads this page sees a gridview with texboxes in all the columns. I need to be able to retrieve the data entered by the user in the textboxes on a button click.
public class GridViewTemplate : ITemplate { private DataControlRowType templateType; private string columnName; public GridViewTemplate(DataControlRowType type, string colName) { templateType = type; columnName = colName; public void InstantiateIn(System.Web.UI.Control container) { switch (templateType) [code]...
I have a select all function and I want to use an if statement to only select items if there is something in this templatefield:
Protected Sub chkSelectAll_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkSelectAll.CheckedChanged For Each itm As GridViewRow In Me.gvLink.Rows
'insert if statement here to check for linkback url. if so then enable checkbox else disble checkbox to prevent user from selecting.
CType(itm.FindControl("chkSelect"), CheckBox).Checked = CType(sender, CheckBox).Checked Next Me.lblMsg.Text = "" End Sub
I would do something like the above for the checkbox, but I'm not sure how to reference the control. It's a templatefield right?
I have a label control inside of repeater and I can't seem to get FindControl to locate the label inside of the repeater or even the repeater itself. I'm not sure what I'm doing wrong here. OnItemDataBound is firing because "hello1" is being written but if I put anything inside of the For Next loop of rptMarketFundamentals, nothing is run inside of it.
<asp:Repeater id="rptMarketFundmentals" runat="server" DataSourceID="dsMarketFundamentals" OnItemDataBound="rptMarketFundamentals_ItemDataBound"> <asp:Label ID="lblOwnershipCategory" runat="server" Text='<%# Eval("OwnershipCategory") %>' /> </asp:Repeater> Sub rptMarketFundamentals_ItemDataBound(ByVal Sender As Object, ByVal e As RepeaterItemEventArgs) Response.Write("hello1") 'this works Dim dataItem As RepeaterItem For Each dataItem In rptMarketFundmentals.Items Response.Write("hello2") 'this does not work Dim lblOwnershipCategory As Label = CType(dataItem.FindControl("lblOwnershipCategory"), Label) If lblOwnershipCategory.Text.ToLower = "family firm" Then blOwnershipCategory.CssClass = "highlight" End If Next End Sub
I have created a gridview dynamically from scratch and added it to my aspx page. This works fine, but i have a button in the footer of on of the columns, which when clicked on makes the gridview disappear and doesn't fire the onclick event that it should. This should obviously not be the case. My question is why does the event not fire and why does the gridview vanish? Bear in mind i have never created aa gridview in the code behind before, so excuse me if i am being dense
Code creating columns Dim gvTownships As New GridView 'COLUMNS Dim tmpCategory As New TemplateField tmpCategory.ItemTemplate = New cGridViewTemplate(DataControlRowType.DataRow, "Category") tmpCategory.HeaderTemplate = New cGridViewTemplate(DataControlRowType.Header, "Category") tmpCategory.FooterTemplate = New cGridViewTemplate(DataControlRowType.Footer, "Calculation") 'INFORMATION gvTownships.AutoGenerateColumns = False gvTownships.ID = cmbChoice.SelectedItem.Text gvTownships.ShowFooter = True gvTownships.CssClass = "mGrid" gvTownships.RowStyle.Wrap = False gvTownships.HeaderStyle.CssClass = "mgrid" gvTownships.HeaderStyle.Wrap = True gvTownships.FooterStyle.CssClass = "mGridtf" gvTownships.AlternatingRowStyle.CssClass = "alt" gvTownships.GridLines = GridLines.Both 'add columns gvTownships.Columns.Add(tmpCategory) gvTownships.DataSource = dstSuburbTowhShip gvTownships.DataBind()
Code to add button to footer
Sub InstantiateIn(ByVal container As System.Web.UI.Control) _ Implements ITemplate.InstantiateIn Case DataControlRowType.Footer If columnName = "Calculation" Then Dim btnCalculate As New Button btnCalculate.Text = "Calculate" btnCalculate.CssClass = "button" 'add handler AddHandler btnCalculate.Click, AddressOf btnCalculate_Clicked container.Controls.Add(btnCalculate) End If Case Else ' Insert code to handle unexpected values. End Select End Sub Private Sub btnCalculate_Clicked(ByVal sender As Object, ByVal e As EventArgs) Try Dim btnCalculate As Button = CType(sender, Button) Dim gvRow As GridViewRow = CType(btnCalculate.NamingContainer, GridViewRow) btnCalculate.CommandArgument = "Calculate" Catch ex As Exception End Try End Sub End Class
I have an asp:table control that I construct dynamically. I place a few textboxes in it within a for loop and assign their IDs in code. I'll include an abbreviated code fragment showing how I'm doing this:
[Code]....
The page then displays correctly and the ID is set correctly when I view the source, but when I try to access the textbox later when the form is submitted the call to FindControl(idStr) returns null no matter what I do.
I need to generate a gridview with dynamic columns, on each cell I need a button that inserts-deletes a relationship in a database. The relationship is of course between what is represented by each column and row. I decided to use a gridview and create templatefields which I add programmatically to the table. I successfully got to that point but when i decided to raise a click event from the buttons or a rowcommand from the gridview I could not do it. I´m looking into doing this with button fields right now but I´m curious on how this can be accomplished?
I need to access the controls dynamically created template fields at run time in datagrid in post back event. Is there any way to retrieve the values in post back event? Or else give the ideas to acheive my requirement. I need to create no of rows and columns as text box as per user input. After fill the values to text box , i need to access the values in submit button.
i am implementing a update query module.i am displaying all fields from a table for a term searched. well now i am implementing update option for the record which are displayed, i have like 70 columns in my table, and i want to know how to restrict some selected fields to be only read only while the form can be updated ?Like if user select to update a record then some selected field such as "Timestamp, UID etc some selected fields" remains READ ONLY !
i am creating Textboxes at Runtime with something like this
Code: Dim cnt As New TextBox cnt.ID = "cont_" & x If multiline Then cnt.TextMode = TextBoxMode.MultiLine End If
These are dynamically created fields. so what i want to do is to use this following JQuery on this textbox
Code: $(document).ready(function() { $("#contentbox").keyup(function() { var box=$(this).val(); var main = box.length *100; var value= (main / 145); var count= 145 - box.length; if(box.length <= 145) { $('#count').html(count); $('#bar').animate({"width": value+'%',}, 1); } return false; }); });
and what this function does it count the number of Characters that have been typed in a Textbox. so i am displaying the remaining characters. So that is working fine on my example because the Element is known at Early Binding. Now my question is what if the textbox is created dynamically ?
Code:
contentbox
this is the name of the Textbox in my example. how do we use a J Query in Dynamically created Textbox.
I'm using multiple file upload. I'm following this link [URL] . I can able to save multiple files. Now i want add textbox to all file upload. I can add HTML Text box. I don't know how to find that? I'm using following code to upload multiple files..
I have a script which creates a dynamic textbox (and more) in an AJAX async post back. But when I try to acess the textbox I am told "Object reference not set to an instance of an object". I have been strugleing with this for a long time. This is written in C# .Net 4. The line causing the problem is the very last one where I have tbGameName.Text.Trim()
I dynamically create gridview, and in this grid I have template field also
field.HeaderTemplate = New GridViewTemplate(ListItemType.Header, col.ColumnName) field.ItemTemplate = New GridViewTemplate(ListItemType.Item, col.ColumnName) grdEmpty.Columns.Add(bfield)
but when enter some value in text box in this template field i lose value on postback. And also on postback I lose all template field and i must re-create this grid.
My goal is: I have button and i want to add new row in this grid, but i want to have old value also.
I want to dynamically create asp textbox objects (as well as other types) with id's named sequentially up to a number the user decides but I'm having a problem getting my code behind to recognize these dynamically created objects. In "div1" below I manually create the objects and I have no problem getting the values in Sub cmdSaveDiv1Info_Click. My dynamically created code generated in Page_Load routine is identical to that in "div1" except for the textbox ID's yet when I try to retrieve the values the same way in cmdSaveDiv2Info_Click I get an "object reference not set to an instance of an object" error on the "Dim d2Year as String.." line.
I have a page that contains many textboxes. It is like 9 textboxes for everyday in a month, and their numbers and IDs are changing dynamically for every month change on the page. I'm creating them in many for loops like;
[Code]....
This was just an example not real code. Now I want to add those textboxes the same TextChange event. How can i do it? I'm a little bit newby at asp.net. Sorry about that...
I am trying to create a comment system in which the comments will be displayed and will have one level of reply to the comment.
I now have text boxes and buttons dynamically created under each comment for users to submit replies to each individual comment.
I am having difficulty obtaining the contents entered by the user within the text boxes.
[Code]....
The GetComment method is called at pageload. I am able to obtain the Id of the comments that the reply corresponds to, but not able to obtain contents entered by user in the reply text boxes, they show up as blank.
how can I allow only alpha characters into a dynamically created textbox? I don't think I can use a regular expression validator for this issue. Unfortunately, I cannot use Ajax for this issue either.
I have a detailsview data control connected to a datasource. i converted all the bound fields to item templates and they have all been converted to textboxes. i need to compare values entered in one textbox and output the value in the other. I have attached a snippet of the code below. but its not working.
i have a WSS 3.0 Webpart that contains an UpdatePanel with a customized SPGridView. I want to provide instant filtering of data by adding a textbox below the column header when the column shall be filterable. There is a jQuery based solution for this on the net, but it's totally client side and doesn't work with paging, so i tried it on my own.
In the CreateChildControls of my SPGridViewExt class i have :
[Code]....
I've also overridden the DoFilterPostBackEventProcessing method to create a "[ColumnName] LIKE '%[textbox.value]%'" filterExpression for my datasource which also creates the LIKEFILTER_COLUMN and LIKEFILTER_VALUE ViewState Items so i can identify the textbox that should refocus on after the postback. The filtering itself works great, but whatever i try, i am not able to set the focus to the textbox after entering the first letter.
[Code]....
All these four methods don't work. The registerstartupscript call doesn't even render the script when i search the page source after postback. I also tried to add a HiddenField to my webpart and set it's value to the control id of the textbox in onfocus, then register a startupscript in OnLoad that focuses the control if the HiddenField has a value, but to no avail.