asp.net: How to find repeater div in backend? - asp.net

I use repeater to display records fetching from database, especially I use div tag to display html content stored in datatable. But I don't know how to find the div in backend. In Repeater_ItemDataBound event I can use
Control divControl=e.Item.FindControl("divControl");
to get it but it does not have innerHtml property to set html content. Does anyone know how to get it as a div control?

HtmlGenericControl defines the methods, properties, and events for all HTML server control elements not represented by a specific .NET Framework class.
So if its a server control you can simply use:
HtmlGenericControl divControl = e.Item.FindControl("divControl") as HtmlGenericControl;

To find the DIV in the code behind, you'll need to add the runat="server" tag to the DIV.
If you're going to do that, you might as well just use a Panel, as it outputs a DIV anyway.
After doing the above, you should be able to find it like this:
Panel panel = (Panel)Repeater1.Items[0].FindControl("Panel1");

Related

ASP NET data pager on top and bottom

I have a Listview + Datapager and I wonder if there's some fancy solution to "duplicate" it and show the same datapager on top and bottom of the datagrid without using 2 different datapagers.
Thanx a lot
One of the dirty way could be replicate pager html using javascript. For example, using jquery:
$(document).ready(function() {
$('#topPager').append($('#bottomPager').html());
});
Where you have two divs - one empty placeholder div with id=topPager and another div with id=bottomPager containing pager control. Note that in case if events being added to pager elements such as links/anchors then you may use jquery clone method to clone pager element with events
I've come across to that page searching for a similar solution for Gridview control. If you prefer using Gridview, there is a built-in solution .net framework. You can set PagerSettings.Position property to TopAndBottom to get pager control on top of and at the bottom of gridview control.
You can see the MSDN documentation page for PagerSettings.Position here.
I've managed it using this approach:
http://www.codeproject.com/KB/custom-controls/mirror.aspx
I created an ascx user control that replicates any other control exactly as it is.
in .NET4 you can put multiple pager element tags in the listview LayoutTemplate element

Accessing asp.net web server controls from jquery

In my C# asp.net 3.5 web application I am having controls like div tag inside that one textbox and one check box and the page is having a submit button. So in this submit button click I want to make the controls inside the div tag visible.. I am calling a JQuery function to do this. All the statements are getting executed but the control is not visible..
Following is the code in my JQuery function
$("input[name$='QuestionAndAnswerEditorDiv']").show();
$("input[name$='answerLabel1']").show()
$("input[name$='wmd-AnswerTextBox']").show()
My div tag and its controls in the user control page are like the following
<div id="QuestionAndAnswerEditorDiv" runat="server">
<div id="wmd-button-bar" class="wmd-panel wmd-button-row"></div>
<textarea name="QuestionandAnswerTextArea" runat="server" id="AnswerTextBox" onkeyup="prettyPrint();" class="wmd-input editor" cols="92" rows="15"/><div class="wmd-preview text-preview preview" style="-ms-word-wrap: break-word;"></div>
As I noticed these controls are make visible=false in another page so they are not coming in the page source.. So Let me know how to work these controls now
all web server controls in asp.net < 4 are not rendered using their given name.
to use the rendered name use Control.ClientID
Setting QuestionAndAnswerEditorDiv.Visible = false; will mean that it doesn't get rendered to the page. In your code behind do the following:
QuestionAndAnswerEditorDiv.Attributes.Add("style", "display:none");
QuestionandAnswerTextArea.Attributes.Add("style", "display:none");
the JQuery show() function uses the display property and will set it to "block", which will make it visible.
This may not work because div is not going to render as input element and label is rendered as span element in asp.net so check for the type of controls
for div
$("div[name$='QuestionAndAnswerEditorDiv']").show();
for label
$("span[name$='answerLabel1']").show()
for textbox
$("input[name$='wmd-AnswerTextBox']").show()
Edit :
so as i suggested in my answer make use of proper jquery selctor so that this will work for you
Edit 1
So rather than making control visible= false set there sytles to disply:none so that controls avaialbe on you page and you can play with them using jquery/javascript

JQuery to add item to Asp.Net PlaceHolder control

It looks to me that the ASP.Net PlaceHolder control doesn't emit any HTML, and therefore can't be used by clientside javascript or JQuery to add items to the PlaceHolder. Does anyone know of a way around this limitation, or of an ASP.Net control that can be used to dynamically add items by client side code?
That is very true. Anyway, JavaScript cannot add controls to the PlaceHolder, only insert elements in its place.
You can use a client side <div></div>, or a Panel - they will all have the effect you're looking for.

Asp.Net How to control, does an element exist in document?

I want to control does an element exist in document with its ID in Asp.Net project when page's first or postback loading.
Thansk for your helps already now.
It sounds like your hiding a section of your page on postback, i'm assuming via the controls Visible property. The problem with this approach is that the control is never rendered when Visible="False", this is probably why your javascript code throws an error.
You can use the css property Display and set its value to None, this will allow the element to render but not display. I'm not sure what your using for a container, so i'm using a panel in my example (which renders as a div).
<asp:Panel ID="pnlContainer" runat="server">
</asp:Panel>
Then instead of toggling the Visible property, you can hide the panel using the CSS display property.
pnlContainer.Style.Add("display", "none");

Template control in ASP.NET hiding content from page

I'm writing my own Modal Popup as a template control in ASP.NET. I got two template containers - one for the heading of the window and one for the actual control i want to display in the modal window (let's call it the form control). The form controls can contain server controls like buttons, textbox'es etc.
It works well, except when i want to access the form control in my web page. My codebehind won't recognize the content in the template control - just like it won't in, say a Repeater. So i figured a Panel control works just like what i need, except the Panel control only has "one container" and i'd really like to be able to set both a header and the content (form control).
I figured i could overwrite the Panel control to add my own html but that would limit my header to be something encodeable in an attribute. So is my best bet really to expose the Heading as just a property, instead of a template and thus being limited in what i can write for heading?
Just as you can with a repeater, try using the ParentControl.FindControl(...) method to get the control you desire.
I ended up overriding the Panel control, accepting that i can only "pass" one set of controls as its children. Also i had to make the heading a property i set as an attribute on the modalpopup control.
Even though i couldn't solve it the way i wanted to, i think the solution is good enough.

Resources