I want to find the Parent Repeater, which contains Child Repeater and Child Repeater contains dropdownlist. On SelectedIndexChange of the Drowndownlist I want to find out the Parent Repeater. After finding the parent repeater, I want to find the hiddenfield value inside Parent Repeater. i.e.
Parent Repeater Contains HiddenField and Child Repeater
Child Repeater contains Dropdownlist on this dropdown selected index change event I want to find HiddenField value which is in Parent Repeater.
My Code:
DropDownList myGeneralButton = (DropDownList)sender;
Repeater item = (Repeater)myGeneralButton.Parent.Parent;
for (int i = 0; i < item.Items.Count; ++i)
{
HiddenField hdn= item.Items[i].FindControl("Hdhotelname") as HiddenField;
string h = hdn.Value;
}
In this hidden field I am getting all the values, but I want a value of that particular index where I am selecting selecting the dropdown.
Thanks
You have to search through the DropDownList's NamingContainer. The flow should be like this:
(DropDownList)sender
--> NamingContainer(Child RepeaterItem)
--> NamingContainer(Child Repeater)
--> NamingContainer(Parent RepeaterItem)
--> FindControl"Hdhotelname" (Hdhotelname)
and your code should be like this:
protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
var ddl = (DropDownList)sender;
var rptChild = ddl.NamingContainer.NamingContainer;//Child Repeater
if (rptChild != null)
{
var rptParentItem = rptChild.NamingContainer;//Parent RepeaterItem
var hdnfld = rptParentItem.FindControl("Hdhotelname") as HiddenField;
if (hdnfld != null)
{
//Do your tasks
}
}
}
Hope it helps!
<%= (Repeater)ChildRepeater.NamingContainer =>
or use it directly in your code-behind without the <%= and =>
Related
I am using a TabContainer control which contains a usercontrol which is dynamically rendered depending upon selected tab. Inside usercontrol I have a repeater which contains a checkbox,label and hidden field. I am updating/changing the values of these fields inside itemdatabound event.
Everything is working fine when page loads first time but after that if I rebind the repeater then only label values are changing and checkbox/hiddenfield containing some old values.
I am not using any updatepanel at all. Please suggest what I am doing wrong.Below is my code snippet inside itemdatabound.
protected void rptrIngrList_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
try
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
SimpleIngredientObject currentItem = e.Item.DataItem as SimpleIngredientObject;
HiddenField hiddenIngrId = (HiddenField)e.Item.FindControl("hdnIngrId");
hiddenIngrId.Value = currentItem.IngredientId.ToString();
HiddenField hdnAltIngrId = (HiddenField)e.Item.FindControl("hdnAltIngrId");
hdnAltIngrId.Value = currentItem.IngrAltId.ToString();
HtmlGenericControl span = (HtmlGenericControl)e.Item.FindControl("abcd");
if (currentItem.UserIngrAvailability == 0 || currentItem.UserIngrAvailability == null)
span.InnerText = GetAvailabilityChar(currentItem.IngrAvailabilityId);
else
span.InnerText = GetAvailabilityChar(currentItem.UserIngrAvailability);
Label lblIngrName = (Label)e.Item.FindControl("LabelIngredientName");
lblIngrName.Text = currentItem.IngredientName;
CheckBox chkIngrUse = (CheckBox)e.Item.FindControl("chkIngrUsage");
// chkIngrUse.Checked = (currentItem.IngrMarked == null) ? false : (currentItem.IngrMarked == true) ? true : false;
chkIngrUse.Checked = Convert.ToBoolean(System.Web.UI.DataBinder.Eval(e.Item.DataItem, "IngrMarked"));
}
}
Could you please share the code where you try to save the data? It seems you are rebinding old values before you save the new form data.
Repeater will only fire ItemDataBound when Databind() has happened - so you have to call databind() on the repeater. So your code will not fire if you have not called Databind() manually. After the datasource is available, it will go through each item and call itemdatabound event. That is why you can see the DataItem value ( which is the object from the datasource).
I had the same problem with an asp:HiddenField inside an asp:repeater.
The issue was : On PostBack, the values inside my asp:Repeater where refreshed with DataBind() in the Page_Load() method instead of the ButtonSearch_click() event method. That's why I was still getting old values in asp:HiddenField and asp:CheckBox of my HTML table.
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;
}
}
I have a Repeater control that outputs various forum posts on the page.
Within each repeater row, I have a LinkButton and 4 TextBoxes that contain values.
When I click on one of the LinkButtons, in my event handler code, I want to get the values that are in each of the 4 TextBoxes that correspond with that one particular repeater item/row.
I can repeat through each item within the repeater, but I am only interested in the values that exist in the 4 textboxes that sat alongside the LinkButton that was clicked/that triggered the event. I'm not interested in any of the Textbox values that belong to the other rows/items within the repeater.
What's the best way to do this?
You can use with ItemCommand event and e.Item.FindControl
Link : http://msdn.microsoft.com/fr-fr/library/system.web.ui.webcontrols.repeater.itemcommand.aspx
protected void Repeater_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if(e.CommandName == "YourCommand" ) //Adjust your CommandName
{
var textbox1 = (TextBox)e.Item.FindControl("YourIdTextBox1"); //Adjust your Id of TextBox in row
var textbox2 = (TextBox)e.Item.FindControl("YourIdTextBox2");
var textbox3 = (TextBox)e.Item.FindControl("YourIdTextBox3");
var textbox4 = (TextBox)e.Item.FindControl("YourIdTextBox4");
....
}
}
I have a case of nested repeaters where a child repeater is nested in the ItemTemplate of a parent repeater. The DataSource of the parent is a Dictionary<String, List<XYZ>>.
In the ItemDataBound of the parent Repeater I am using the full code:
protected void rptParent_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
if (e.Item.DataItem is KeyValuePair<String, List<Object>>)
{
pair = (KeyValuePair<String, List<XYZ>>)e.Item.DataItem;
}
Repeater childRepeater = e.Item.FindControl("rptChild") as Repeater;
//bind the child repeater.
childRepeater.ItemDataBound += new RepeaterItemEventHandler(childRepeater_ItemDataBound);
childRepeater.DataSource = pair.Value;
childRepeater.DataBind();
}
}
protected void childRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer)
{
//Access the Parent row's Key value
}
}
There are 2 questions:
Can I use a hidden field while binding the parent and set it to the Key value and then retrieve the hidden field value in the child?
Will the order of the events firing be as follows:
a. Parent_ItemDataBound for Row 1 of Dictionary<Key, List<XYZ>>
i. Child_ItemDataBound for each child row of Row 1 of parent repeater
ii. Child_ItemDataBound for Footer of child repeater
b. Parent_ItemDataBound for Row 2 of Dictionary<Key, List<XYZ>>
i. Child_ItemDataBound for each child row of Row 2 of parent repeater
ii. Child_ItemDataBound for Footer of child repeater
and so on. In other words, will the Parent_ItemDataBound be followed by Child_ItemDataBound events for each child row - with the cycle repeating?
Here are the answers to your question:
Yes, you can access parent repeater item control
var hfID = e.Item.NamingContainer.NamingContainer.FindControl("hfID") as HiddenField;
the order is correct.
Hope that helps
The row has a LinkButton that when clicked needs to highlight the row.
Code so far:
protected void linkbutton1_Click(object sender, EventArgs e)
{
LinkButton l = (LinkButton)sender;
GridViewRow g = (GridViewRow)l.Parent; // what is the correct way to do this?
//g.Style etc etc
}
first of all set the "CommandName" property of LinkButton to "select",
then in the selectedIndexChanging event of gridview write below code:
for (int i = 0; i < GridView1.Rows.Count;i++ )
GridView1.Rows[i].BackColor = System.Drawing.Color.White;
GridView1.Rows[e.NewSelectedIndex].BackColor = System.Drawing.Color.Cornsilk;
Make use of the RowCommand event of the GridView instead of the Click event of the LinkButton.
Then you can have a CommandName on the LinkButton such as "HighlightRow" and do something like the following:
Select Case e.CommandName
Case "HighlightRow"
e.item.row.attributes("class") = "highlight"
End Select
Sorry its in VB.NET and not C#
1.) Set Command Name Property to "Select"
2.) Change style either on code behind as shown by #Raymond or give set Cssclass attribute for
SelectedRowStyle of gridview to CssClass="selecterowstyle"
.selectedRowstyle
{
background-color:#EAEAEA;
}