Can not find ASP.Net Table with FindControl method - c#

How can I find an ASP.Net table control with FindControl method?
if (Convert.ToInt32(Session["Persons"]) == 1)
{
HtmlTable tt = (HtmlTable)panel1.FindControl("singleTbl");
tt.Visible = true;
}
<asp:UpdatePanel ID="panel1" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btn_serch" EventName="Click" />
</Triggers>
<ContentTemplate>
<div id="div_result" runat="server" style="display:none" class="divsearchresult">
<table width="750">
<asp:DataList ID="dtlRoomsPrice" Visible="false" orizontalAlign="center" runat="server"
ShowFooter="False" ShowHeader="False" Width="700px" OnItemDataBound="dtlRoomsDetails_ItemDataBound">
<ItemTemplate>
<table border="0" id="singleTbl" width="90" align="left" runat="server" style="visibility:hidden;">
</table>
</ItemTemplate>
</table>
</div>
</asp:UpdatePanel>

There are some problem with your code ,
First you can not cast an ASP.Net control to HTML control or vise versa
so your table is HTML control you need cast it to HtmlControl like:
HtmlControl tt = (HtmlControl)FindControl("singleTbl");
tt.Visible = true;
second you have designed a table HTML tag which have not any tr and td so change it like :
<table border="0" id="singleTbl" width="90" align="left" runat="server" style= "display:none;">
<tr>
<td></td>
</tr>
</table>
It is proffered to use display instead of visibility because display:none; hides an element, and it will not take up any space so you can try like:
HtmlControl tt = (HtmlControl)FindControl("singleTbl");
tt.Style.Add("display", "block");
//tt.Attributes["visibility"] = "visible";
//tt.Attributes["display"] = "block";
//tt.Visable = true;
or you should do it in ItemDataBound event of DataList as following:
HtmlControl tt = (HtmlControl)e.Item.FindControl("singleTbl");
tt.Style.Add("display", "block");

The method "FindControl" searches the first level controls under the container . Maybe your table is enjected dynamically after a postback or it 's under an other control like Panel or UpdatePanel.
First of all your table is a HTML table so singleTbl is a HtmlTable not Table.
If you want it a Table Class you must use <asp:Table> tag.
Second point your tag <contenttemplate> is not closed.
The last point, i mentioned that the FindControl sheaches only in the direct children of the control.
you must add runat server and an Id to your first div and table
then in your code:
var div= Panel1.FindControl("divID") as HtmlGenericControl;
var firstTable=div.FindControl("firstTableID") as HtmlTable;
var dataList=firstTable.findControl("dtlRoomsPrice") as DataList;
var tbl=dataList.FindControl("singleTbl") as HtmlTable;

Related

Disable asp imagebutton

I am trying to get the image button in this table to get hidden via the code behind in C# (so that I can hide it after an action).
<asp:UpdatePanel runat="server" ID="upEmpListContainer" UpdateMode="Conditional"
OnPreRender="upEmpListContainer_PreRender" OnInit="upEmpListContainer_Oninit">
<ContentTemplate>
<asp:ListView ID="lvEmpList" OnItemDataBound="lvEmpList_ItemDataBound" OnDataBound="lvEmpList_DataBound"
OnLayoutCreated="lvEmpList_LayoutCreated" runat="server" OnPreRender="lvEmpList_PreRender">
<LayoutTemplate>
<table class="formData_tb" cellspacing="0" style="margin: 0px; width: 100%;">
<tr>
<th colspan="9" class="currentManagerLabel" style="text-align: center">
<span>Currently displaying
<asp:Label ID="lblManager" runat="server" />
employees </span>
<asp:ImageButton ID="DrillUp" AlternateText="Move up in reporting heirarchy" runat="server"
CommandName="DrillDown" ImageUrl="~/images/icoDoubleArrowUp.gif" OnCommand="butDrillDown_Click" />
</th>
</tr>
</table>
</LayoutTemplate>
</asp:ListView>
</ContentTemplate>
</asp:UpdatePanel>
I just can't seem to access it from its ID. I've tried DrillUp.Enabled = false; but Visual Studio is saying that it can't resolve symbol 'DrillUp'.
Your imagebutton is inside layout template, you cant access it directly.
Try this
ImageButton b = (ImageButton)lvEmpList.FindControl("DrillUp");
b.Visible = false;
In your code behind method lvEmpList_ItemDataBound, you need to use the following:
((e.Item.FindControl("DrillUp")) as ImageButton).Visible = false;
Also, you should add a check that this is done only for the DataItem items like this:
if (e.Item.ItemType == ListViewItemType.DataItem)
{
((e.Item.FindControl("DrillUp")) as ImageButton).Visible = false;
}
Try to:
1-remove that imagebutton from aspx
1-clean the solution
2- Write manually again that asp:imagebutton...
It will work fine

Iframe autoresizing height from codebehind in C#

My html page is
<iframe runat="server" id="iframe1" width="100%" height="100%" scrolling="no" frameborder="0"></iframe>
.cs content in my pageload event
iframe1.Attributes["src"] = "http://default.com/";
//iframe1.Attributes["height"] = "100%";
//iframe1.Attributes["width"] = "100%";
iframe1.Attributes.Add("style","width:100%;height:100%;");
But its not working
i want to display whole page content but my height of iframe is not taking the height of http://default.com/
I don't know how to autoresize iframe on .cs page but It's another option like put your iframe in datalist control like...
<asp:DataList ID="dtlhtml" runat="server" Width="100%">
<ItemTemplate>
<table cellpadding="0" cellspacing="0" width="100%">
<tr>
<td>
<iframe src='<%#Eval("html") %>' width="713" id="iframe1"
frameborder="0" onLoad="autoResize 'iframe1');">
</iframe>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
Put javascript code as...
<script language="JavaScript">
function autoResize(id)
{
var newheight;
var newwidth;
if (document.getElementById(id))
{
newheight = document.getElementById(id).contentWindow.document.body.scrollHeight;
newwidth = document.getElementById(id).contentWindow.document.body.scrollWidth;
}
document.getElementById(id).height = (newheight) + "px";
document.getElementById(id).width = (newwidth) + "px";
}
</script>
And put on .cs page.
DataTable dt1 = new DataTable();
dt1.Columns.Add("html");
DataRow dr = dt1.NewRow();
dr["html"] = "";//Any dynamic url path
dt1.Rows.Add(dr);
dtlhtml.DataSource = dt1;
dtlhtml.DataBind();
NOTE:
This will not work in local host ..please try it on online.
I assume you don't want 'scrolling', so why not disable it?
<iframe src="/default.asp" width="100%" height="100%" scrolling="no"></iframe>
or try
iframe1.Attributes.Add("scrolling","no");
Edit: Try
PlaceHolder1.Controls.Add(new LiteralControl("<iframe src='mypage.aspx' width='100%' height='100%' scrolling='no'></iframe>"));
or
iframe1.Attributes["src"] = "http://www.asp.net";
Since you are using runat="server" so you can access the attributes like height and width from code behind.
Try
Updated Answer
iFrame1.Attributes.Add("height","100%");
iFrame1.Attributes.Add("width","100%");
set scrolling ="no" inside tag as suggested by Paul

How to access a legend tag from code

I have the following data-bound repeater code :
<%--categories--%>
<asp:Repeater ID="CategoryRepeater" runat="server" OnItemDataBound="ItemBound">
<ItemTemplate>
<div class="groupbox">
<fieldset>
<legend><%# Container.DataItem %></legend>
<table>
<asp:Repeater ID="ItemRepeater" runat="server">
<ItemTemplate>
<tr>
<td>
<asp:CheckBox id="chkItem" runat="server" Text='<%# Eval("Text")%>' />
<asp:HiddenField id="pgNos" runat="server" Value='<%# Eval("PGNos")%>' />
<asp:Button ID="btnXRefs" Text="x-refs" runat="server" CssClass="xRefButton" OnClick="btnSelectXRefs_Click" />
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
</fieldset>
</div>
</ItemTemplate>
</asp:Repeater>
There is a repeater inside a repeater. How do I access the text inside the legend (<legend><%# Container.DataItem %></legend>) from code?
I tried :
foreach (RepeaterItem cr in CategoryRepeater.Items)
{
string heading = (string) cr.DataItem; // returns null
}
Container.DataItem is a runtime alias for the DataItem for this specific item in the bound list. For a Repeater which displays 10 rows of data, this is one row from the datasource...Basically, it's a specific row and at runtime you can get the Property Values from this row
I saw your above Mark Up...Seems like you are missing to mention the property of Data-Bound type Class like below.
<%# ((Your Class Name)Container.DataItem).Class Property Name %>
There is a repeater inside a repeater. How do I access the text inside
the legend (<%# Container.DataItem %>) from code?
As told by phemt.latd, you can change the Legend tag into server side control like below.
<legend id="lg" runat="server">
<%# ((Your Class Name)Container.DataItem).Class Property Name %>
</legend>
Now, in the Item-Bound Data Event, Find the Legend Control.
protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
HtmlGenericControl ctl = (HtmlGenericControl)e.Item.FindControl("lg");
ctl.InnerText //This is what will give you the result.
}
}
The legend tag that you use is not visible on server side. Is a client control and not a server one.
Try with this:
<legend id="myLegend" runat="server"><%# Container.DataItem %></legend>
Then in codebehind:
protected void ItemBound(Object sender, RepeaterItemEventArgs e)
{
if (e.Item.DataItem == null) return;
HtmlGenerics body = (HtmlGenerics)e.Item.FindControl("myLegend");
body.InnerText = "Foo";
}

Target accordion pane content template after databind so that I can load data on demand?

I have an accordion that has another accordion inside one of its panes. This inner accordion is created using a datasource so each of its panes are loaded from a list of objects. In this particular case, this datasource is also loaded on demand. Now, where I'm stuck is that I want to be able to load the pane headers only and then load the contents when the pane is clicked; similar to what I have in the outer pane. The reason I'm confused here, is because the lazy load happens when the pane is clicked, but since this happens AFTER the databind, I don't know how to reference the content of the pane that invokes the ItemCommand. Not sure if that makes sense. Here is the inner accordion:
<ajaxToolkit:Accordion runat="server" ID="accReviewers" OnItemDataBound="accOuterAccordion_ItemDataBound" ContentCssClass="ReviewerContent" RequireOpenedPane="False" SelectedIndex="-1" OnItemCommand="accReviewers_ItemCommand">
<HeaderTemplate>
<div>
<asp:LinkButton Text='<%#Eval("Header") %>' CssClass="InReviewHeader" runat="server"
CommandName="LoadReviewers" CommandArgument='<%#Eval("MocRequestId") %>'/>
</div>
</HeaderTemplate>
<ContentTemplate>
<div>
<asp:ListView runat="server" ID="lvReviewers" ItemPlaceholderID="phReviewer" OnItemDataBound="lvReviewers_ItemDataBound">
<LayoutTemplate>
<div>
<asp:HyperLink runat="server" ID="lnkGotoRequest" Text="View this request"/>
</div>
<asp:PlaceHolder runat="server" ID="phReviewer"/>
<div style="margin-top: 5px;">
<asp:Button runat="server" ID="btnResubmit" Text="Resubmit" CssClass="ResubmitInitial"/>
</div>
</LayoutTemplate>
<ItemTemplate>
<div class="ReviewerItem">
<%#Eval("Assignee.Name") %><br />
<img src="" alt="Reviewer" runat="server" ID="imgReviewer" width="75" style="border: 1px solid gray; border-radius: 6px;"/><br />
<asp:Label runat="server" ID="lblStatus" Text='<%#Eval("ReviewStatus") %>' />
<asp:HyperLink runat="server" ID="lnkRejectComment" CssClass="InitialRejectComment">(details)</asp:HyperLink>
</div>
</ItemTemplate>
</asp:ListView>
</div>
</ContentTemplate>
</ajaxToolkit:Accordion>
</Content>
</ajaxToolkit:AccordionPane>
As you can see, the accordion accReviewers is generated via a DataSource. The listview contained in the LayoutTemplate will not have its datasource bound until the LinkButton has been clicked, which will fire the item command. Also worth noting that this entire accordion is wrapped in an UpdatePanel.
This is the code behind I was starting to work with, but it doesn't appear to get the correct instance of the listview and while the list is not empty, it will not display anything:
protected void accReviewers_ItemCommand(object sender, CommandEventArgs e)
{
var mocId = int.Parse(e.CommandArgument.ToString());
var list = (sender as AjaxControlToolkit.Accordion).FindControl("lvReviewers") as ListView; //APPARENTLY WRONG
var reviewers = MocApi.GetReviews(mocId);
list.DataSource = reviewers;
list.DataBind();
}
So to recap, when the LinkButton within the HeaderTemplate is clicked, I need to somehow gain reference to the correct instance of the ListView so that I can bind its datasource. As always, any help or insight is appreciated. This is similar to a previous question of mine but is specific to gaining this reference after databind which seems a bit more complicated. TIA
UPDATE:
I found that I can bind the item datasource if I can somehow capture its index. I'm exploring trying to set that as a command argument during the databinding of the inner accordion.
I managed to solve this with some minor shenannigans:
Here is the markup:
<ItemTemplate>
<div class="ReviewerItem">
<%#Eval("Assignee.Name") %><br />
<div style="display: inline-block; position: relative;">
<img src="" alt="Reviewer" runat="server" ID="imgReviewer" width="75" style="border: 1px solid lightgray; border-radius: 6px; overflow: hidden;"/><br />
<div runat="server" ID="divYes" Visible="False">
<img src="../Images/Yes.png" alt="Approved" class="ApprovalIcon" />
</div>
<div runat="server" ID="divNo" Visible="False">
<img src="../Images/No.png" alt="Rejected" class="ApprovalIcon" id="imgNo" />
</div>
</div>
<asp:Label runat="server" ID="lblStatus" Text='<%#Eval("ReviewStatus") %>' />
<asp:HyperLink runat="server" ID="lnkRejectComment" CssClass="InitialRejectComment">(details)</asp:HyperLink>
<asp:Panel runat="server" ID="pnlDemoApproval" Visible="False" CssClass="DemoButtons">
<asp:Button runat="server" ID="btnApprove" Text="Approve" CommandArgument='<%#Eval("Assignee.Guid") + "|" + Eval("Ticketid") %>' CommandName="ApproveReview"/>
<asp:Button runat="server" ID="btnDeny" Text="Deny" CommandArgument='<%#Eval("Assignee.Guid") + "|" + Eval("Ticketid") %>' CommandName="DenyReview"/>
</asp:Panel>
<ajaxToolkit:BalloonPopupExtender runat="server" ID="balloon" BalloonPopupControlID="pnlPopup"
TargetControlID="lnkRejectComment" Position="TopRight" BalloonStyle="Cloud" BalloonSize="Medium" DisplayOnMouseOver="True"/>
<asp:Panel runat="server" ID="pnlPopup">Rejection Reason</asp:Panel>
</div>
</ItemTemplate>
On databind, I catch the item so that I can grab the index and set it to the CommandName for later use:
AjaxControlToolkit.AccordionItemEventArgs e)
{
if (e.ItemType != AjaxControlToolkit.AccordionItemType.Content) return;
var index = e.ItemIndex;
var button = e.AccordionItem.Parent.FindControl("lnkbHeader") as LinkButton;
if (button != null) button.CommandName = index.ToString();
}
Now that control contains the index, i can use that to target the correct pane and bind its datasource:
protected void accReviewers_ItemCommand(object sender, CommandEventArgs e)
{
//This seems stupid to put here, but for some reason the item command bypasses the listview catch and passes it to the accordion
if (e.CommandName == "ApproveReview")
{
var assigneeGuid = new Guid(e.CommandArgument.ToString().Split('|')[0]);
var ticketId = int.Parse(e.CommandArgument.ToString().Split('|')[1]);
var ticket = new MocApproval(ticketId);
DoDemoApproval(ticketId, assigneeGuid, true);
var approvalIndex = (sender as AjaxControlToolkit.Accordion).SelectedIndex;
var lv =
(sender as AjaxControlToolkit.Accordion).Panes[approvalIndex].FindControl("lvReviewers") as ListView;
lv.DataSource = MocApi.GetReviews(ticket.MocRequest);
lv.DataBind();
return;
}
if (e.CommandName == "DenyReview")
{
var assigneeGuid = new Guid(e.CommandArgument.ToString().Split('|')[0]);
var ticketId = int.Parse(e.CommandArgument.ToString().Split('|')[1]);
var ticket = new MocApproval(ticketId);
DoDemoApproval(ticketId, assigneeGuid, false);
var approvalIndex = (sender as AjaxControlToolkit.Accordion).SelectedIndex;
var lv =
(sender as AjaxControlToolkit.Accordion).Panes[approvalIndex].FindControl("lvReviewers") as ListView;
lv.DataSource = MocApi.GetReviews(ticket.MocRequest);
lv.DataBind();
return;
}
...

Once clicking the select button of a row, a gridview appears between that row and the rest of the gridview. Possible?

I'd like to have the user click the select button of, lets say, the 250th row of a 400 row gridview. When they click that, then another gridview that's 3x12 appears below that row, then the 150 other rows appear below that. Is this at all possible? I guess I could create a whole other div that'll have three gridviews that output depending on being <= and > the index of the selected row.
It starts as:
Gridview rows 1-400
Then after row 350 is selected is it:
Gridview rows 1-350
Gridview of row 350 info
Gridview rows 351-400.
It's definitely possible, but I would use a ListView or DataList as your parent container instead, because with a GridView, you'll have to put the child list in a column, which will look ugly. This should put you on the right path:
<asp:ListView ID="lstOuterList" runat="server" DataKeyNames="ID, OtherColumn">
<LayoutTemplate>
<table width="100%">
<asp:PlaceHolder runat="server" ID="itemPlaceHolder" />
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td><asp:LinkButton ID="LinkButton1" runat="server" Text="Expand" OnCommand="LinkButton1_Command" CommandArgument='<%#Container.DisplayItemIndex%>'></asp:LinkButton></td>
<td><%#Eval("Value")%></td>
<td><%#Eval("OtherValue")%></td>
<td><%#Eval("OtherOtherValue")%></td>
</tr>
<asp:PlaceHolder ID="plcInnerList" runat="server">
<asp:ListView ID="lstInnerList" runat="server" Width="100%">
<LayoutTemplate>
<tr>
<td colspan="4">
<div style="padding:20px;background-color:#fffeee;">
<table width="100%">
<asp:PlaceHolder runat="server" ID="itemPlaceHolder" />
</table>
</div>
</td>
</tr>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td><%#Eval("Value")%></td>
<td><%#Eval("OtherValue")%></td>
<td><%#Eval("OtherOtherValue")%></td>
</tr>
</ItemTemplate>
</asp:ListView>
</asp:PlaceHolder>
</ItemTemplate>
</asp:ListView>
And when the user clicks the LinkButton/Button in DataList1, do something like this:
protected void LinkButton1_Command(object sender, CommandEventArgs e)
{
//pass index of item in command argument
var itemIndex = Convert.ToInt32(e.CommandArgument);
//find the pnlChildView control
var innerPlaceHolder = lstOuterList.Items[itemIndex].FindControl("plcInnerList") as PlaceHolder;
if (innerPlaceHolder != null)
{
innerPlaceHolder.Visible = !innerPlaceHolder.Visible;
if (innerPlaceholder.Visible)
{
var innerList = innerPlaceHolder.FindControl("lstInnerList") as ListView;
if (innerList != null)
{
//the id to retrieve data for the inner list
int keyValue = (int)lstOuterList.DataKeys[itemIndex]["ID"];
//bind the list using DataList1 data key value
innerList.DataSource = new DataTable("DataSource"); //your datasource
innerList.DataBind();
}
}
}
}
one way is:
on the rowcommand of the main grid:
create c# code for the grid will be inside GridView grd = new GridView();
bind this instance like any other grid
add on the controls from the main grid current line, should be something like
e.Cells[0].Controls.Add(grd);
I don't have VS here right now but I guess you could get the idea, I use this approach all the time

Categories