Access Values From Dynamic Generated Textbox In A Gridview?
Aug 31, 2010
In my asp page when press a button, a gridview will be populated with data from db, but beside the columns from database i need a new dynamic column in gridview, to contain only dropdownlist with items number from 0-n.
This far i managed to do it, with some research on internet (i used an intemerdiet datatable to apply ItemTemplate from GridViewTemplate class, and than merged it to datatable containg the result from query, so the gridview's datasource si the merge of this 2 datatables).
The issue is that i don't now how can i access the values selected by user in the dinamically dropdownlists, 'cause i have to save them in DB.
Attach is my GridViewTemplate class code, and .cs and .aspx code.
I have dynamically generated a button an its event too.Now want to access a textbox (Generated Dynamically) into the onClick event of the Button.How can i do this?
I have generated dynamic text boxes for each table row there are 6 text boxes
1. For the first two text boxes the values given by the user should be in valid range(this range comes from the database).User is allowed to enter the value within that specified values only. How to achieve this? I have taken drop down lists but if the values in drop down increases selecting required value is tedious to the user.. (if it is having 1000 values) and while I am retrieving the values I have written query such as Null values ignored. But I am getting NULL values in the dropdown here is the code what I have written,
(Database is Oracle) If Not (OledbCmd.Equals(String.Empty) Or OledbCmd Is Nothing) Then OledbDA.SelectCommand = OledbCmd OledbDA.Fill(ds, "textDB") dt = ds.Tables("testDb") If (dt.Rows.Count > 0) Then ddlist.DataSource = dt If UCase(s) = "NUMBER" Then ddlist.DataValueField = "num" ElseIf UCase(s) = "NAME" Then ddlist.DataValueField ="name" End If ddlist.DataBind() ddlist.Items.Insert(0, "Select") ddlist.SelectedIndex = 0 Else ddlist.Items.Insert(0,"Norecords") End If End If
2. In the rows generated when user presses functional key F3 then the values of the above row(previous row) have to be copied in present row. similarly for F4 column copying. This I achieved through the Java script. But I have many forms requirement is same. where as text boxes id differ. how can I generalize the One function to work for all forms. I wrote as like this.
i have d design for dynamic textbox control ..but now i want to save the data values from the dynamic textbox...refer my following codings for design and based on that suggest me the codings for saving the data...
I have a gridview which has autogenerate Columns Property "TRUE", and i'm dynamically binding this gridview with columns with sql database dataset. there is no data in sql but i am successful in showing blank gridview on form, now i want textboxes in gridview attached with each column, so i wrote code in RowDataBound which generates textbox automatically and assigns ID to each textbox, nw i want to insert that data from textbox to database but i'm not getting text which is entered in those textboxes, as i am accessing that textboxes using findcontrol and passing the id like gridview1_ctl03_text0. but i''m not getting any value from textbox?
In short i want to access auto generated textbox (with out any id predefined ) values from footer of gridview
I have a gridview each line has 4 linkbuttons - Edit , Update , Cancel , Delete and a few columns with data...For example TeamName and TeamEmail, and a hidden column that contains a validator (for ease of validating controls that are found on the same row , same naming container)
When Edit button is Clicked , the specific line goes into editing mode , Texboxes are created for TeamName and for TeamEmail .
I create dinamically (code behind ) a new textbox that reads the value from the TeamEmail textbox , i give it a new id (let's say "Email") , add that email to the controltovalidate property of my validator , and call the validate() method of the validator.
Everything works fine when i modify the value in a worng format , the validator err message appears , when i modify the TeamEmail value to a good email format , the validator property isvalid becomes true , and the update is running , but my textbox is empty :( updating the TeamEmail value to null.
This is some bad code but hope you understand what i meant :
I have uploaded a excelsheet to datatable and that datatable is bound to gridview.there is a save button present,i want to click the save button and all the gridview data will save in database.but that gridview has no boundfield or templatefield.
I have a gridview which is having coloumns Company name, Industry type, country, employees, revenue. i want to add dropdown at the top of each coloumn so that if user select industry as agriculture, all agriculture industry rows may be sorted, and same for other dropdown also.. i it may be possible,
i want to show selected row of gridview into textbox then we can change these value then we click update button then these value add into selected row., this row show the update value not previous using jquery . following my code.
<script type="text/javascript" src="~/Scripts/jquery-1.10.2.js"></script> <script type="text/javascript"> $(document).ready(function () { // function Edit() { var par = $(this).parent().parent(); var id = par.children("td:nth-child(1)"); var name = par.children("td:nth-child(2)");
I want to set desired ClientID in dynamic generated controls. ClientID is read only if you have any idea to set desired ClientID to controls than tell me
how to load Gridview HeaderText values from resource (.resx) files.I want to display headertext values for Gridview based on two different application dynamically.
I have gridview created dynamical and added text box to the footer so I can insert into the gridview but I'm using findcontrol but it saying the value is null.
I am using autocomplete jquery textbox in gridview , for first time when grid view is binded autocomplete jquery textbox works , when new row is dynamically added with textbox, the code is not working
I want to add inputs from textboxes, ddl to datagridview without saving it to database by clicking on add button as many rows i want and later save it to database when clicked on save button....
I have 3 textboxes , 1 dropdown , 1 Button and 1 gridview .
Textboxes ID are :ACode , ADesc , ASeq DropDown ID is :CPhase Button ID is :AddRowBTN GridView ID is :PhasesTempGrid
Now whats the story is ?After adding some data into textboxes , when a user clicks ADDRow BTN , that data gets saved into datatable , then that datatable gets binded to gridview and it shows my textboxes data in it .
What i want to achieve ?In my gridview i am use templateField and inside that template field there are textboxes ( Let the users edit the row without clicking any button ) .
Take an example of dummy data and problem persisting in there :for e.g i put some data in textboxes :ACode = "ABCD"ADesc = "EFGH"ASeq = "HIJK"Then i click Add BTN , it saves the data in datatable and show in gridview , now if i do this in gridview :ACode = "Edited ABCD"ADesc = "Edited EFGH too"ASeq = "WoW There"Then i click Add BTN , it removes the edited record in gridview and shows original data which had came earlier from textboxes :ACode = "ABCD"ADesc = "EFGH"ASeq = "HIJK"I don't want that to be done ... Simple is that user can enter data from textboxes , click the btn to submit into grid then he can edit the data like he want ... data must not get flashed , here's my coding i have done so far :
public void AddNewData() { var dt = new DataTable(); if (ViewState["myDatatable"] != null) { dt = (DataTable) ViewState["myDatatable"]; }
[CODE]...
here i havent added dropdown combo in gridview but you can do it so that it will save my time (Bind it just the way PhaseDropDown have bind)...
I'm new to web dev and c# so please bare with me. I am trying to create a dynamic gridview in a web form for users to to answer questions with (code below).
The dificulty im having is that i am nesting a dropdwonlist in the gridview and want to be able to dynamically set the list values based on parameters (of the row they are on). These values are in the main dataset for the gridview as each row represents a questionid and question text and then a ddl for the criteria...
I just don't know how to set the values for the dropdown all the code so far is below... just need to be able to populate the dropdowns with the relevant values.
I have created a stored proc to return the different criteria based on the questionid and questionGroupid which is the dataset that populates the other fields in the gridview: dbo.usp_QuestionCriteria @QuestionGroupId, @QuestionId
I have added this as a tableadapter called criteriaTableAdapter in a xsd file as well using the wizard... not sure if this is the right option though or just use the same method as i have for the other stored procedure as in the code below:
[CODE
]using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Sql"].ConnectionString); con.Open(); SqlCommand com = new SqlCommand("usp_QuestionGroupDS", con); com.CommandType = CommandType.StoredProcedure; SqlDataAdapter ada = new SqlDataAdapter(com); DataSet ds = new DataSet(); ada.Fill(ds); for (int i = 0; i < ds.Tables.Count; i++) { if (ds.Tables[i].Rows.Count > 0) { GridView gvDynamicQuestion = new GridView(); gvDynamicQuestion.Width = Unit.Pixel(700); gvDynamicQuestion.BorderWidth = Unit.Pixel(0); gvDynamicQuestion.Caption = "<div id="nifty" class="QuestionGroup"> <b class="rtop"><b class="r1"></b><b class="r2"></b><b class="r3"></b><b class="r4"></b></b>" + ds.Tables[i].Rows[0]["Category"].ToString() + " Questions<b class="rbottom"><b class="r4"></b><b class="r3"></b><b class="r2"></b><b class="r1"></b></b></div>"; gvDynamicQuestion.AutoGenerateColumns = false; gvDynamicQuestion.ShowFooter = true; TemplateField tf = null; tf = new TemplateField(); tf.HeaderTemplate = new DynamicGridViewTextTemplate("QuestionId", DataControlRowType.Header); tf.ItemTemplate = new DynamicGridViewTextTemplate("QuestionId", DataControlRowType.DataRow); tf.FooterTemplate = new DynamicGridViewTextTemplate(DataControlRowType.Footer, ds.Tables[i].Rows.Count); gvDynamicQuestion.Columns.Add(tf); tf = new TemplateField(); tf.HeaderTemplate = new DynamicGridViewTextTemplate("Question", DataControlRowType.Header); tf.ItemTemplate = new DynamicGridViewTextTemplate("Question", DataControlRowType.DataRow); gvDynamicQuestion.Columns.Add(tf); tf = new TemplateField(); tf.HeaderText = "Criteria"; tf.HeaderTemplate = new DynamicGridViewTextTemplate("Criteria", DataControlRowType.Header); tf.ItemTemplate = new DynamicGridViewDDLTemplate(); gvDynamicQuestion.Columns.Add(tf); ////tf = new TemplateField(); ////tf.HeaderText = "Criteria"; ////tf.ItemTemplate = new DynamicGridViewDDLTemplate(); ////gvDynamicQuestion.Columns.Add(tf); gvDynamicQuestion.DataSource = ds.Tables[i]; gvDynamicQuestion.DataBind(); phDynamicGridHolder.Controls.Add(gvDynamicQuestion); } } } protected void DynamicGrid_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.Footer) { // } } } public class DynamicGridViewTextTemplate : ITemplate { string _ColName; DataControlRowType _rowType; int _Count; public DynamicGridViewTextTemplate(string ColName, DataControlRowType RowType) { _ColName = ColName; _rowType = RowType; } public DynamicGridViewTextTemplate(DataControlRowType RowType, int QuestionCount) { _rowType = RowType; _Count = QuestionCount; } public void InstantiateIn(System.Web.UI.Control container) { switch (_rowType) { case DataControlRowType.Header: Literal lc = new Literal(); lc.Text = "<b>" + _ColName + "</b>"; container.Controls.Add(lc); break; case DataControlRowType.DataRow: Label lbl = new Label(); lbl.DataBinding += new EventHandler(this.lbl_DataBind); container.Controls.Add(lbl); break; case DataControlRowType.Footer: Literal flc = new Literal(); flc.Text = "<b>Total No of Questions:" + _Count + "</b>"; container.Controls.Add(flc); break; default: break; } } private void lbl_DataBind(Object sender, EventArgs e) { Label lbl = (Label)sender; GridViewRow row = (GridViewRow)lbl.NamingContainer; lbl.Text =DataBinder.Eval(row.DataItem, _ColName).ToString(); } } public class DynamicGridViewDDLTemplate : ITemplate { // Implementation of ITemplate public void InstantiateIn(System.Web.UI.Control container) { // Create a DDL DropDownList ddl = new DropDownList(); //Attach method to delegate ddl.DataBinding += new System.EventHandler(this.ddl_DataBind); container.Controls.Add(ddl); } //Method that responds to the DataBinding event private void ddl_DataBind(object sender, System.EventArgs e) { //DropDownList ddl = (DropDownList)sender; //DataGridItem container = (DataGridItem)ddl.NamingContainer; //ddl.Data.Checked = [Data binding expression]; } }