I have a gridview and on some condition basis, I am inserting text box in it in RowDataBound:
private void GetColumnWithValidation(GridViewRowEventArgs e, string columnName, int columnLength)
{
if (Convert.ToString(DataBinder.Eval(e.Row.DataItem, columnName)).Length > columnLength)
{
int colindex = GetColumnIndexByName(e.Row, columnName);
TextBox txt = new TextBox();
txt.Text = Convert.ToString(DataBinder.Eval(e.Row.DataItem, columnName));
txt.BorderColor = Color.Red;
txt.ID = "txt_" + i + "_" + colindex;
lstErrorTracker.Add(i + "_" + colindex);
//link.NavigateUrl = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "PV_INVOICE_LOCATION"));
e.Row.Cells[GetColumnIndexByName(e.Row, columnName)].Controls.Add(txt);
}
}
Now if I change some data in text box of gridview I want that to be fetch whenever I click Update button. Update button is placed outside of gridview as a normal asp.net button.
But when I am trying to fetch the data I am getting null.
foreach (GridViewRow row in GVUploadDetails.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
TextBox txt= (row.Cells[3].FindControl("txt_0_3") as TextBox);
var test = txt.Text;
}
}
I am getting text data as null. Please help how to get those values. And on other hand I want the entire data of the gridview as a Dataset or datatable how can I get that.
The NamingContainer is the GridViewRow not the cell, so you should use:
foreach (GridViewRow row in GVUploadDetails.Rows)
{
TextBox txt = row.FindControl("txt_0_3") as TextBox;
// ...
}
If this doesn't fix the issue, where in the page's life-cycle do you addd this TextBox programatically? You know that you always have to re-create it on every consecutive postback? Do that latest in Page_Load, better Page_Init.
If you add it from RowDataBound you have to re-create it in RowCreated, otherwise it's too late to retain the ViewState.
Related
I have a working autocomplete mode in my datagridview. The only problem is that when I select the value to fill the cell, the row switches to the one below.
I need the program to remain in the same row once the user selects the value given from the autocomplete. I show my code hopefully it can help.
private void entityLinesGrid_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
//this method helps to autocomplete the data in the field selected
int column = entityLinesGrid.CurrentCell.ColumnIndex;
string titleText = entityLinesGrid.Columns[column].HeaderText;//this line is getting the column name
TextBox autoText = e.Control as TextBox;
if (autoText != null)
{
autoText.AutoCompleteMode = AutoCompleteMode.Suggest;
//according to the column name the method will gather autocomplete data
autoText.AutoCompleteCustomSource = dbConnect.dataFeeder(titleText);
autoText.AutoCompleteSource = AutoCompleteSource.CustomSource;
}
}
I have a web application asp.net.
I am creating 54 text boxes dynamically in Page_Load
Here is the code
protected void Page_Load(object sender, EventArgs e)
{
for(i = 0, i<54, i++)
{
Textbox TestTextbox = new texbox();
TestTextBox.ID = "Reg" + i ;
TestTextBox.Attributes.add("runat","server");
TestTextBoxAttributes.Add("AutoPostBack", "true");
//display to a table called table1 created in the aspx page
}
}
On the page I have a button, called button1, and a on click event called "OnClickEvent", i want to capture the ID and the values of all the textboxes.
I have used Page.Controls.Count and I get only 1, the table that I have added to the aspx page, I get the IDs by using Request.Form but I`m not getting the values.
I am adding all the text boxes to a Table that I have created in the aspx file.
You can loop through the controls and check if they are of type TextBox:
for(int i = 0, i<54, i++)) {
var control = Page.FindControl("Reg" + i);
//get the value of the control
}
You are not adding the TextBox to the Page, so you cannot find it anyway. Second, adding runat=server and AutoPostBack=true as a string would not work either. (not to mention your snippet is full of errors)
//a loop uses ';', not ','
for (int i = 0; i < 54; i++)
{
//declare a new dynamic textbox = CaSe SeNsItIvE
TextBox TestTextbox = new TextBox();
TestTextbox.ID = "Reg" + i;
//if you want to add attibutes you do it like this
TestTextbox.AutoPostBack = true;
TestTextbox.TextChanged += TestTextbox_TextChanged;
//add the textbox to the page
PlaceHolder1.Controls.Add(TestTextbox);
}
And if you want to loop all the controls you can do something like this
//loop all the controls that were added to the placeholder
foreach (Control control in PlaceHolder1.Controls)
{
//is it a textbox
if (control is TextBox)
{
//cast the control back to a textbox to access it's properties
TextBox tb = control as TextBox;
string id = tb.ID;
}
}
I have a gridview in user control and added the controls dynamically using placeholder
now i find the controls from grid it give that controls are not exist in the grid
Code is as Following :
foreach (GridViewRow row in GridView1.Rows)
{
PlaceHolder plc = (PlaceHolder)row.FindControl("lblPlaceHolder");
TextBox txtTextBox1 = plc.FindControl("txtTextBox1") as TextBox; //its give Null
}
can anyone has answer plz
Added code from Comments:
foreach (GridViewRow dr in GridView1.Rows)
{ PlaceHolder placeHolder = dr.FindControl("lblPlaceHolder") as PlaceHolder;
TextBox txtTextBox1= new TextBox();
txtTextBox1.Width = 300;
placeHolder.Controls.Add(txtTextBox1);
}
Remove the placeholder, and You will be able to find your controls
and also find your controls using the onRowDatabound, or onRowCreated event from the grid.
btw: why is your placeholder called lblPlaceHolder, do You have a label in there and maybe you are asking the wrong contol to find yr textbox, thats why you are getting a null
void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewRow row = e.Row as GridViewRow;
if (row != null)
{
TableCell myCell = row.Cells[0] as TableCell
TextBox txtTextBox1= new TextBox();
txtTextBox1.Width = 300;
myCell.Controls.Add(txtTextBox1);
}
}
i m using parent child grid and on child grid i m doing Show / hide threw java script. and child grid i bind run time with Templatecolumns like
GridView NewDg = new GridView();
NewDg.ID = "dgdStoreWiseMenuStock";
TemplateField TOTAL = new TemplateField();
TOTAL.HeaderTemplate = new BusinessLogic.GridViewTemplateTextBox(ListItemType.Header, "TOTAL",e.Row.RowIndex );
TOTAL.HeaderStyle.Width = Unit.Percentage(5.00);
TOTAL.ItemTemplate = new BusinessLogic.GridViewTemplateTextBox(ListItemType.Item, "TOTAL", e.Row.RowIndex);
NewDg.Columns.Add(TOTAL);
NewDg.DataSource = ds;
NewDg.DataBind();
NewDg.Columns[1].Visible = false;
NewDg.Columns[2].Visible = false;
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
NewDg.RenderControl(htw);
Now I have one TextBox inside Grid named "TOTAL" I want to Find This TextBox and wanna get its value.
How can Get it ?
You could get the TextBox control inside the corresponding cell of the GridView, using the Controls property or the FindControl(string id) method:
TextBox txtTotal = gv.Rows[index].cells[0].Controls[0] as TextBox;
or
TextBox txtTotal = gv.Rows[index].cells[0].Controls[0].FindControl("TOTAL") as TextBox;
where index could be 0 for the first row, or an iterator inside a for loop.
Alternatively, you can use a foreach loop over the GridView's rows:
foreach(GridViewRow row in gv.Rows)
{
TextBox txtTotal = row.cells[0].Controls[0].FindControl("TOTAL") as TextBox;
string value = txtTotal.Text;
// Do something with the textBox's value
}
Besides, you have to keep in mind that, if you're creating the GridView dynamically (and not declaratively in the web form), you won't be able to get this control after a page postback.
There is a great 4 Guys from Rolla article on the subject: Dynamic Web Controls, Postbacks, and View State
As you know you may get TextBox value for example for the first row and the first cell:
((TextBox) dgdStoreWiseMenuStock.Rows[0].Cells[0].Controls[1]).Text;
or change the index of control put 0 if the above line dosen't work.
Try This
TextBox txtTotal = (TextBox)gv.Rows[index].cells[0].FindControl("TOTAL");
string value = txtTotal.Text;
The easiest way to find the control inside the gridview use foreach loop to find the rows
foreach (GridViewRow row in Gridname.Rows)
{
TextBox txttotal = (TextBox)row.FindControl("textboxid_inside_grid");
string var = txttotal.Text;
Response.Write("Textbox value = " + var);
}
This is a follow up to my previous question:
link text
In gridview's column i have a linkbutton and a label under it.
I want to hide/unhide label when linkbutton is clicked. I use javascript because i don't want any postbacks.
The code:
protected void gvwComments_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton lButton = ((LinkButton)e.Row.Cells[2].FindControl("lbtnExpand"));
Label label = ((Label)e.Row.Cells[2].FindControl("lblBody"));
lButton.Attributes.Add("onclick", string.Format("HideLabel('{0}'); return false;", label.ClientID));
}
}
function HideLabel(button) {
var rowObj = document.getElementById(button);
if (rowObj.style.display == "none") {
rowObj.style.display = "block";
}
else {
rowObj.style.display = "none";
}
}
The problem is that when I unhide the label by clicking on button, linkbutton is shifted a a bit upper it's original position in the cell.
Is it possible to preserve linkbutton's position in the gridviews cell?
I would suggest you change your CSS from display:none to display:hidden which will hide the control but maintain the space preventing the jumping around.
The trick is to use the keyword "this" and then get a reference to the row and change the label from there.
I have a post here, where I have a GridView with a CheckBox column and a Name column. When the CheckBox is checked the background color of the Name on that row changes. I do this starting with this attribute in the CheckBox's column:
onclick="toggleSelected(this)"
then I have a JavaScript function that finds the row and changes the next cell:
function toggleSelected(sender) {
// note that at this point the "this" is now "sender" -which is the checkbox
// get a reference to the row using the helper function - see below.
var row = GetParentElementByTagName(sender, "TR");
if (sender.checked)
row.cells[1].className = "selected";
else
row.cells[1].className = '';
}
This uses the helper function:
function GetParentElementByTagName(element, tagName) {
var element = element;
while (element.tagName != tagName)
element = element.parentNode;
return element;
}
You have a slightly different requirment so you would use something like:
var lbl = row.cells[1].childNodes[0].getElementsByTagName('label')
to get a reference to your label.