ASP.NET Page Load - Cannot find control in edit item template - asp.net

Why I cannot find textbox control in edit item template in gridview?
I set Session["index"] on Edit button click
if (Session["index"] != null)
{ TextBox txt =
GridView1.Rows[Convert.ToInt32(Session["index"])].FindControl("txtEmail") as TextBox;
txt.Text = "AAAAA";
}
GridView: http://pastebin.com/CwAqs2J3

I think you should put it first into the object then determine if the control is a textbox something like this:
Textbox txt = new Textbox();
if(Session["index"] != null) {
int sessionIndex = Convert.ToInt32(Session["index"]);
object thiscontrol = GridView1.rows[sessionIndex].FindControl("txtPhone");
if(thiscontrol is typeof(Textbox))
{
txt = thiscontrol;
txt.Text = "AAAAA";
}
}
I hope this helps.

Related

asp.net visibility of button depended on DataTable row value

I would like to add button to ListView only for some of rows. I have something like "online chat" and I want to add delete option only for one user. It is complicated because I would like to have "delete" button only in rows which are these user's messages. Messages are saved in DataBase.
.aspx
<td><asp:Button ID="Button1" runat="server" Visible = <%# Eval("BUTTON") %>
.aspx.cs
DataColumn newColumn = new DataColumn("BUTTON", typeof(bool));
dataTable.Columns.Add(newColumn);
foreach (DataRow row in dataTable.Rows)
{
if (row["USER_NAME"].ToString() == "Franek")
row["BUTTON"] = true;
else
{
row["BUTTON"] = false;
}
}
sqlDataAdapter.Fill(dataTable);
MessagesView.DataSource = dataTable;
MessagesView.DataBind();
Communicat of error is (image)
Thank you and have a nice day
Try adding the column BUTTON after sqlDataAdapter.Fill(dataTable);.
sqlDataAdapter.Fill(dataTable); would fill the Data Table from Database and it may be resetting the values of "BUTTON" column.
sqlDataAdapter.Fill(dataTable);
DataColumn newColumn = new DataColumn("BUTTON", typeof(bool));
dataTable.Columns.Add(newColumn);
foreach (DataRow row in dataTable.Rows)
{
if (row["USER_NAME"].ToString() == "Franek")
row["BUTTON"] = true;
else
{
row["BUTTON"] = false;
}
}
MessagesView.DataSource = dataTable;
MessagesView.DataBind();

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

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

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;

Resources