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;
Related
I have taken a datalist in my asp.net project which contains some picture and picture name. Now I want to show each picture in another page after. It means if any user will click on the picture then picture will open with it's details in another page.So I want to know that how I find a control in datalist.
In following picture of datalist I want to click on picture name like Jai Ho and It will open in another page.
You can use the OnClientClick property of Link Button.
In <asp:LinkButton> tag add following code
OnClientClick="javascript:window.open('your_url');"
try this
protected void up-movie-name_Click(object sender, EventArgs e)
{
Label4.Text = (Datalist1.SelectedItem.FindControl("Label1").ToString());
}
or
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{
LinkButton linkButton = e.Item.FindControl("LinkButton1") as LinkButton;
string commandText = linkButton.Text;
}
}
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";
}
}
}
}
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);
I am using a databound repeater component with some click-sensitive panel inside.
<ItemTemplate>
<asp:Panel ID="PanelContent" runat="server">
<asp:Panel ID="PanelMenuTitle" runat="server"
ondblclick="EditMenu(<%# Eval("ID") %>)">
As you can see I want to pass the ID of the current data item to a javascript function called EditMenu().
However, this code breaks due to "The server tag is not well formed.". I also tried everything I can think of: Using <%= instead of <%#, Bind() instead of Eval(), ' instead of " without success.
Use single quotes around the ondblclick function. That should fix it. See below.
ondblclick='EditMenu(<%# Eval("ID") %>)'
My second suggestion would be to use another control, like a ListView or a DataList, so that you can assign data keys to hold the ID. Then you can assign the ondblclick event in the ItemDataBound event like this.
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
Panel pnlCtrl = (Panel)e.Item.FindControl("Panel1");
if (pnlCtrl != null)
{
pnlCtrl.Attributes["ondblclick"] = String.Format("EditMenu({0})", ListView1.DataKeys[((ListViewDataItem)e.Item).DisplayIndex]["ID"]);
}
}
}
Alright, I got it working. Not the original approach, but in the end it's what I wanted:
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Panel pnlCtrl = (Panel)e.Item.FindControl("PanelMenuTitle");
if (pnlCtrl != null)
{
myMenu menu = (e.Item.DataItem as myMenu);
pnlCtrl.Attributes["ondblclick"] = String.Format("EditMenu('{0}')", menu.ID);
}
}
I have filled the asp.net datagrid in codebehind file. while binding i have used the datagrid onItemDataBound event to add onmouseover and onmouseout event that looks lik this..
protected void dataGridSavedQueris_OnItemDataBound(Object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
e.Item.Attributes.Add("onmouseover", "dgRowHighlightOnWhenMouseOver(this,'" + e.Item.Cells[0].Text.ToString() + "');");
e.Item.Attributes.Add("onmouseout", "dgRowHighlightOffWhenMouseOut (this,'" + e.Item.Cells[0].Text.ToString() + "');");
}
}
now, it renders properly. i mean the dgrowHighlightOnWhenMouseOver and dgRowHighlightOffWhenMouseOut is assigned every row of the datagrid expect the header and footer.
in onmouseover and onmouseout function, I show the div with values of associating id.
what happening is whenever i move cursor one cell to another cell on the same row, it fires.
but, i need to fire the both events whenever I mouseover on the row not for cell..
how to do that?
thanks
r.e
You might want to attach the mouseover/out on RowCreated rather than on ItemDataBound.
Something like this:
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "dgRowHighlightOnWhenMouseOver(this,'" + e.Row.Cells[0].Text.ToString() + "');");
e.Row.Attributes.Add("onmouseout", "dgRowHighlightOffWhenMouseOut(this,'" + e.Row.Cells[0].Text.ToString() + "');");
}
}
PS: I've not tried it out myself yet, so I'm not 100% sure :P