Find textbox value in 2 nested gridviews - asp.net

I have a gridview2 which is inside gridview1.
gridview 2 has a textbox which i need to get the value of that text box.
Usually I do like this when I need to get the value from a textbox when it's inside a single gridview:
TextBox txb = (TextBox)GridView1.SelectedRow.FindControl("TextBox1");
I want to do something similar but this time getting a value from TextBox1 which is inside gridview2 and gridview2 inside gridview1.
Everything is done through TemplateField of course.

Try this...
foreach (GridViewRow row in grdSubClaimOuter.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
GridView gvChild = (GridView) row.FindControl("grdSubClaim");
// Then do the same method for Button control column
if (gvChild != null)
{
foreach (GridViewRow row in gvChild .Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
TextBox txb = (TextBox)row.FindControl("TextBox1");
if (txb != null )
{
// do your work
}
}
}
}
}
}

Related

Search grid view created dynamically inside a asp panel using id

i have created few dynamic grid view inside a panel
Method where i added control
pRelaese.Controls.Add(DynamicGrid(dt));
i'm saving the grid view id in a hidden variable
i added a button inside a update panel
string[] GridName = hdnGridName.Value.Split(',');
foreach (string GN in GridName)
{
GV = this.FindControl("pRelaese").FindControl(GN) as GridView;//tried this
pnl = (Panel)Page.FindControl("pRelaese");//tried this
GV = (GridView)pnl.FindControl(GN);//tried this
if (GV != null)
{
foreach (GridViewRow GVR in GV.Rows)
{
//codes
}
}
}
But every time null is returned to GV
i need to find control and insert it into GV to perform of the task

Checkbox checked length in gridview using asp .net server side

I am having one gridview and check box for each row of gridview and one button outside gridview.
If I click on button then it should check for atmost one checkbox should be checked, otherwise it will display a message and return from loop.
But how to get the checkbox selected length in gridview in serverside.
If you just want to check if there is a checkbox checked in the gridview you can do this in the button:
bool isChecked = theGridview.Rows
.Cast<GridViewRow>()
.Any(a => ((CheckBox)a.FindControl("yourCheckbox")).Checked);
Do not forget using System.Linq;
You need to loop trough the row of the gridview and look for the checkbox as follows
foreach (GridViewRow r in YourGridView.Rows)
{
foreach(string cnt in controls){
int value = Convert.ToInt32(((System.Web.UI.HtmlControls.HtmlInputCheckBox)r.FindControl(cnt)).Value);
bool isChecked = ((System.Web.UI.HtmlControls.HtmlInputCheckBox)r.FindControl(cnt)).Checked;
//now you have id and checked/unchecked. use your query to save it to database
}
}
Manipulate this according to you requirement.
foreach (GridViewRow gvrow in final.Rows)
{
CheckBox chk = (CheckBox)gvrow.FindControl("CheckBox1");
if (chk != null & chk.Checked)
{
//Perform action 1
}
else
{
// perform action-2
}

Hiding row on dropdown selected index change

I am using an asp.net formsview in my asp.net page and updatepanel . It has two templates Insertemplate and EditTemplate. Indie both templates there is a dropdownlist with id ddlCountry. I have a dropdownlist with all countries. I am showing states dropdown, if country is US and want to hide the row where states dropdown is shown If country is Non US. I am using following code but it is not working:
protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
{
Control c = (Control)sender;
Control nc = c.NamingContainer;
if (nc.ID == "fvBillTo" && rblShipSelect.SelectedValue == "billing")
{
setShippingAndTaxesDisplay();
DropDownList ddlCountry = c as DropDownList;
if (ddlCountry.SelectedItem != null && ddlCountry.SelectedItem.Value == "001")
{
HtmlGenericControl trState = nc.FindControl("trState") as HtmlGenericControl;
trState.Visible = true;
}
else
{
HtmlGenericControl trState = nc.FindControl("trState") as HtmlGenericControl;
trState.Visible = false; // code stops here
}
}
}
The code is throwing an exception at that point because it doesn't have a reference to the table row.
Make sure you have the row referenced as a server side control
<tr id="trState" runat="server" >
And cast it as a table row instead of a HtmlGenericControl
System.Web.UI.HtmlControls.HtmlTableRow trState = nc.FindControl("trState") as System.Web.UI.HtmlControls.HtmlTableRow;

How to insert 2 controls, one into each different column in a gridview

I place 10 or 15 webcontrols into 2 columns int a gridview dynamically.
So say if i decide to add 15 controls , then i want 15 of them to appear at each row of the two columns..
I want paging to be enabled as well.
How do i achieve that?
May be you can try the following code to add the controls.
The following code adds a textbox to the 2nd Cell in every row including the footer row.
protected void gvTest_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox tbox = new TextBox();
tbox = "the text you wasnt to add or bind with data field";
e.Row.Cells[1].Controls.Add(tbox);
}
if (e.Row.RowType == DataControlRowType.Footer)
{
TextBox tbox = new TextBox();
tbox = "the text you wasnt to add or bind with data field";
e.Row.Cells[1].Controls.Add(tbox);
}
}

With the ASP.NET GridView control, how can I disable controls or individual cells in a GridViewRow after PageIndexChanged?

I have a data bound GridView control, in which I am able to disable individual cells based on the User role. This only works on the first page.
private void LimitAccessToGridFields()
{
if (User.IsInRole("Processing")) return;
foreach (GridViewRow gridViewRow in gvScrubbed.Rows)
{
var checkBox = ((CheckBox) gridViewRow.FindControl("cbScrubbed"));
checkBox.Enabled = false;
// ButtonField does not have an ID to FindControl with
// Must use hard-coded Cell index
gridViewRow.Cells[1].Enabled = false;
}
}
I call this method on Page_Load, where it works. I've tried it in the PageIndexChaging and PageIndexChanged event handlers, where it doesn't work. While debugging, it appears to successfully set Enabled to false in both of the controls in the row. My goal is to disable these fields, depending on user role, after changing the page. How should this be accomplished?
You do not need to iterate through any controls to disable or hide/visible them.
Every cell in a GridView control is actually a HTML table reference, when rendered (look at the code in your page using FireFly or Inspector).
So why not iterate through all the cells, and any controls found within each cell, just disable them? Or you can simply loop through each row of your GridView and disable or hide it directly, which will affect everything inside the row.
Hiding using a Table Cell reference example:
foreach (GridViewRow gRow in myGridView.Rows)
{
if (gRow.RowType == DataControlRowType.DataRow)
{
TableCellCollection tbcCol = (TableCellCollection)gRow.Cells;
foreach (TableCell tblCell in tbcCol)
tblCell.Enabled = false;
}
}
So that will disable everything table cell by table cell.
OR.. why not just disable the entire row?
foreach (GridViewRow gRow in myGridView.Rows)
{
if (gRow.RowType == DataControlRowType.DataRow)
gRow.Enable = false;
}
If you need to pin-point or filter particular control types (CheckBox, TextBox, Label, etc) and only affect those controls then simply test for them!
foreach (GridViewRow gRow in myGridView.Rows)
{
if (gRow.RowType == DataControlRowType.DataRow)
{
TableCellCollection tbcCol = (TableCellCollection)gRow.Cells;
foreach (TableCell tblCell in tbcCol)
if (((TextBox)tblCell) != null)
((TextBox)tblCell).Enable = false;
}
}
I found that this must be done in the RowDataBound event handler.
if (e.Row.RowType == DataControlRowType.DataRow)
{
// details elided ...
// Limits the access to grid fields.
if (!User.IsInRole("PROCESSING"))
{
cbstuff.Enabled = false; // a checkbox
e.Row.Cells[1].Enabled = false; //a link button
}
}

Resources