Search grid view created dynamically inside a asp panel using id - asp.net

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

Related

Find textbox value in 2 nested gridviews

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
}
}
}
}
}
}

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
}

Binding multiple Dropdowns and textfields

In my project I have 2 dropdowns 1 listbox and 1 textbox. I Have already bound the 2 dropdowns together "PostalDropDown" and "CityDropDown" from the database and it works fine, Then i bind the listbox also to the previous dropdowns and it works fine also!
My question here i still have the last Textbox which i want it to display the name which is also bound to the results of dropdowns.
I cannot figur it out because the textbox does not have the SelectValue property, so I cannot assign it like i did with my dropdowns or listbox like i did:
if (!IsPostBack)
{
IEnumerable<Tuple<string, string, string, string>> data = GetData();
DropDownListPostal.DataSource = data.Select(tuple => tuple.Item1).Distinct().ToList();
DropDownListPostal.DataBind();
DropDownListCity.DataValueField = "Item1";
DropDownListCity.DataTextField = "Item2";
DropDownListCity.DataSource = data;
DropDownListCity.DataBind();
ListBox1.DataValueField = "item1";
ListBox1.DataTextField = "Item4";
ListBox1.DataSource = data;
ListBox1.DataBind();
}
}
and then i view the result on this on the selectedindexchanged on first dropdown:
protected void DropDownListPostal_SelectedIndexChanged1(object sender, EventArgs e)
{
//DropDownListPostal.ClearSelection();
ListBox1.ClearSelection();
DropDownListCity.ClearSelection();
var postal = DropDownListPostal.SelectedValue;
var listItem = DropDownListCity.Items.FindByValue(postal);
var street = ListBox1.Items.FindByValue(postal);
listItem.Selected = true;
street.Selected = true;
Can anyone show me how to add the rest of the database results on a textbox?
I can put the whole code for the page if you all want.
Cheers
If you really want to show all of the items in a Textbox which is quite strange you can display them comma seperated like this
TextBoxId.Text = String.Join(",", data.Select(op => op.Name));
Where Name is the property that contains the property you want to display in textbox
Edit
If you want to display the selected items of Listbox then loop on its item and check if the item is selected then display it in your textbox.
foreach (ListItem item in ListBox1.Items)
{
if (item.Selected)
{
txtval.Text += " " + item.Text +"/"+ dropdowncity.SelectedItem.Text +"/"+ dropdownpostal.SelectedItem.Text;
}
}

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;

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