Generating controls dynamically using placeholder - asp.net

How can I create divs dynamically using placeholder in c#? I have declared placeholder inside of a repeater.
Is it possible to create controls dynamically in placeholder?

here you go:
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
PlaceHolder pl = e.Item.FindControl("PlaceHolder1") as PlaceHolder;
if (pl != null)
{
HtmlGenericControl div1 = new HtmlGenericControl("div");
div1.InnerHtml = "Hello";
pl.Controls.Add(div1);
}
}
}

To dynamically add a div to a PlaceHolder control within a Repeater control, you need to use the ItemDataBound event of the Repeater.
Within the method for ItemDataBound you will need to find the PlaceHolder control. Checking first if the ItemType is ListItemType.Item or ListItemType.AlternatingItem first.
PlaceHolder ph = (PlaceHolder) e.Item.FindControl("PlaceHolderControl1");
Then you can add the div as MUG4N has suggested.
HtmlGenericControl div1 = new HtmlGenericControl("div");
div1.InnerHtml = "Hello";
div1.Attributes.Add("class", "classname");
div1.Attributes.Add("style", "top: 10px; left: 20px;");
ph.Controls.Add(div1);
To add another div, just repeat from div1 = new HtmlGenericControl("div"); down to ph.Controls.Add(div1);
Alternatively, you can add a <asp:Panel> control as this renders as a <div> on the page but is more ".net"
Panel panel1 = new Panel();
Then add a <asp:Literal> control to this.
Literal literal1 = new Literal() { Text = "Hello" };
panel1.Controls.Add(literal1);
ph.Controls.Add(panel1);

Related

find parent repeater from child repeater's dropdownlist selectedindexchange

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

Find Anchor Tag from Listview InsertItemTemplate to Codebehind ItemData bound

I want to set anchor tag visible false/True and want to set value to it ,in Code Behind ListView_ItemDataBound. Anchor tag is inside Listview Insert ItemTemplate .
you can use linkbutton instead of anchor tag...
if (e.Item.ItemType == ListViewItemType.DataItem)
{
LinkButton linkButton = e.item.FindControl("LinkButton1") as LinkButton;
if (linkButton != null)
linkButton.Visible = false;
}
or use this code
HtmlGenericControl anchor = e.item.FindControl("LinkButton1") as HtmlGenericControl;
anchor.Attributes.Add("href", "page.htm");
anchor.InnerText = "TabX";
anchor. Visible = false;

How do I find an AjaxToolkit TabContainer in the code behind?

I have a tabcontainer in a repeater. When the page builds I want to set the active tab. How do I find it in the code behind?
This doesn't work:
AjaxControlToolkit.TabContainer tc = FindControl("projTabContainer");
because I'm trying to cast a control to a tabcontainer.
If the TabContainer is in an ItemTemplate of a Repeater, you have to set the ActiveTab in ItemDataBound. That's also the only place where you can find it in this way since the RepeaterItem is it's NamingContainer.
void R1_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
var tc = (AjaxControlToolkit.TabContainer)e.Item.FindControl("projTabContainer");
tc.ActiveTab = ...
}
}
Try the following.
AjaxControlToolkit.TabContainer tc = e.Item.FindControl("projTabContainer") as AjaxControlToolkit.TabContainer;

How do I change the class for a specific <li> inside a ASP.NET listview ?

The idea is for a menu. I need to highlight the menu a specific color depending on which top node is clicked.. So if I just needed to highlight it with ONE color I could say
<li <%# (Container.DataItem as FigLeafMenuItem).ItemSelected == "True" ? #" class=""MainMenuSelectedBlue""" : #" class=""""" %>>
But I need to change the color depending on some peice of server side info.. The closest I have gotten is below.. How do I access that specific LI so I can add a class to it?
protected void lvMainOuter_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
ListViewDataItem lvdi = (ListViewDataItem)e.Item;
FigLeafMenuItem flmi = (FigLeafMenuItem)lvdi.DataItem;
if (flmi.Name == "About Us")
{
HtmlGenericControl hgc = (HtmlGenericControl)e.Item.FindControl("xxx");
hgc.Attributes.Add("class", "MainMenuSelectedBlue");
}
}
}
You cannot access html tag from code behind without runat="server". However, you can render entire li tag using literal control.
<asp:Literal id="MenuItemLiteral" runat="server" />
if (flmi.Name == "About Us")
{
var literal = (Literal)e.Item.FindControl("MenuItemLiteral");
literal.Text = string.format("<li class=\"{0}\">{1}</li>", DATA1, DATA2);
}

How do I change the image on a <asp:buttonField type="Image" /> in code behind?

I have a <asp:GridView > with a <asp:ButtonField ButtonType="Image"> as one of the columns.
Here's the problem: I have to dynamically change the image of this ButtonField during the gridView_RowDataBound(...) event based on the data found in that particular gridview row.
The real question is, is how to access that particular ButtonField inside the gridView_RowDataBound(...) event so I can change its image in C# code?
I can't use
Image imgCtrl = (Image)args.Row.FindControl("ctrlID");
because the <asp:ButtonField> won't allow an ID to be set (get a parser error when I try to run the webPage). And I can't use
args.Row.Cells[0].Controls[0];
because the zeroth index of the .Controls[0] doesn't exist (I get a boundry overflow error).
There's got to be a simple, slick, easy way to do this!
Quick Example :
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataRowView drv = (DataRowView)e.Row.DataItem;
if (e.Row.RowType == DataControlRowType.DataRow)
{
TableCell tableCell = e.Row.Cells[3]; // Column 3 in the grid have the Image Button
foreach (var control in tableCell.Controls)
{
if (control.GetType() == typeof(System.Web.UI.WebControls.ImageButton)) ;
{
ImageButton iButton = control as ImageButton;
iButton.ImageUrl = "/Logo.jpg";
}
}
}
}

Resources