How do I access Page controls from within my custom server control? - asp.net

I am building a modal box as a custom server control and I want to have a property on the modal box TargetControlID that specifies the element that will show the modal when clicked. I've set the property up in the modal box and in the code behind I use the following code block (which I've tried in several different places
If (_targetControlId <> "") Then
Dim targetControl As WebControl = Me.Page.FindControl(_targetControlId)
targetControl.Attributes.Add("onclick", "test1();")
targetControl.Attributes.Add("onclick", "test2();")
End If
What happens is that targetControl always winds up to be NULL, and causes the page to crash when I tried to add attributes to it. I've double checked the spelling of the targetControlId and I am specifying a control that is runat="server". What is the proper way for a server control to access other controls on its containing page?
Thanks,
Mike

First of all, I should point out that the behavior you're looking for already exists in the ModalPopupExtender that comes with the free, open-source AjaxControlToolkit. I'd recommend you just use that. If you're still sure you want to write your own, then I'd recommend at least taking a look at their code to see how they go about it. ExtenderControlBase.FindControlHelper is a good place to start.

Related

Application wide Modalpopupextender

I'm trying to build a modalpopupextender, along with a panel and content, and need to make it application-wide. I am thinking about creating it on the Masterpage, so it's accessible on all pages, but I need the content inside the panel (anything that I may need to add there) to be visible and editable from outsite the masterpage.
For now, I'm working on this, but haven't figured out how to make it accessible to other pages and classes, and so would like to have some help on it.
Basically, what I want is to work more on the idea in a near future in order to make something consistent to be used on any web application, and to be fully customizable. What I'm having trouble is with "basics", like making it accessible to the application, allow customization of some controls inside the panel from both server and client sides, and will improve everything from there.
I have tried creating a user control for it, but didn't seem to work. I'm not an expert on asp.net (few years of experience), and even less on ajax, so any help is appreciated.
Please let me know if anybody have any questions.
EDIT:
I have now succeeded somehow creating the moodal within a user control and it's almost done.
At this moment, there are 2 issues I couldn't fix:
The damn flickering that happens on Firefox 3.5 (Corporate version, can't touch this). Ocasionally during page load (Somewhere near Page_Init or Page_PreInit events, not sure), the modals I have blink quickly on the screen, only when a postback happens. I have already done some workaround, like setting style display:none, but the issue remains. Need some help on the matter.
I need to have a modal that have 2 behaviors, like windows popups. One is information, likee only showing the message with some buttons, and the other is question. For questions, I'll need to use the ConfirmButtonExtender, and so would need to tell this confirm extender and the modal that an external button (Means a button that isn't within the user control, and by that means it's outside the same UpdatePanel as the confirm extender and modal extender) will be their TargetControlID. For now, I couldn't solve this, so I thought about creating a button inside the UC and UpdatePanel that will always be the TargetControlID. When the popup is informational, it will work as a dummy hidden button (information messages are called on server-side through methods), and when it's a question, it will receive the response. The method to be executed by the button will be set through a delegate, and therefore any method may be run when it's clicked and the Yes button on the modal is pressed (It's not ready yet, and I'm not sure it will work, or even if it's a good idea).
Any thoughts on this second option is appreciated.
It's easy for elements on the masterpage to be visible and editable from outsite the masterpage.
In this example the masterpage has a label that you want to read/write from other pages
<asp:Label ID="lblSubTitle" runat="server" Text="sub title"></asp:Label>
In the codefile for the masterpage, create a property for the subtitle:
public partial class MainMasterPage : System.Web.UI.MasterPage
{
public string SubTitle
{
get
{
return lblSubTitle.Text;
}
set
{
lblSubTitle.Text = value;
}
}
}
Then any page that uses this masterpage as its MasterPageFile can find the subtitle property and read or write it.
// get a reference to the masterpage
MainMasterPage master = (MainMasterPage)Master;
//set it to the value you want.
master.SubTitle = "Custom sub title";
I have solved the issue and created a user control containing the modalpopup for showing customized messages.
Many aspects are public, so it allows high customization, and the modal and it's customized buttons work like a charm. I still have a problem regarding the flickering, but it's for another question.

making control in user control visible=false on mouse over

More
I have this datalist in a user control i want when i keep mouse over "More", it should be invisible. it is working on .aspx page not on user control. How to do this. This control is placed on master page.
Please Help.
Probably it will be an issue with getElementById. In a naming container you cannot get the element by simply giving its id. You have to use ClientID to get the generated id of the element at runtime.
Something like
document.getElementById ( "<%= DataList2.ClientID %>");
See Control.ClientID Property
and
Control ID Naming in Content Pages
I would agree that it's probably an issue with trying to get the id of the element since the element's id changes at runtime when you put it inside a user control. You can run your code then do a view source in the browser and see exactly what the id is generating to at runtime.
Have you tried debugging the javascript mover() and mout() function? I am guessing you are looking for elements with the wrong id's since the id's are probably different inside a user control.

loading value to asp.net control using javascript

iam into problem of reading the value of the control which i alterd using javascript
the sequence goes like this
i got the text box control by using its id
cleared the value of the text box
make the control disabled.
when i tried to retrive the value of the textbox in aspx.cs
iam still getting the old value of the text box which i actualy cleared in the javascript
kindly suggest me to over come this issue
Thanks
Disabled input controls don't get their values posted back so ASP.Net doesn't know you changed the value. You need to enable the control.
With ASP.NET Web Forms, IDs for elements get assigned automatically by the Page on the server side. It is tricky, but possible, to manipulate element values using javascript. One way to avoid this would be to use ASP.NET MVC where the HTML would be rendered as is and javascript or jquery can be easily used to "play" with the HTML elements. We need more information to help you with the details of your request.

I've built a ascx control and I would like to be able to keep adding them using Javascript instead of having to do a full call back

I've built a ascx control and I would like to be able to keep adding new instances of it using JavaScript instead of having to do a AJAX callback. Is this possible? I am basically building a web form for a query control and should clause X be filled in, I want to generate a control for the next clause below. I would like to learn how to do this without doing a callback.
Thanks
ASCX are server side user controls and, to my knowledge, can only be loaded by a server event. This can be accomplished through a full page postback or using UpdatePanels and ASP.net AJAX.
If you don't want to use these options and stick with a full JavaScript solution, you're looking at probably doing DOM manipulation and dynamically adding straight HTML.
If the ASCX controls don't change their appearance and all you're doing is showing and hiding them, one last alternative could be to load all of them into DIV tags that have their display style set to none. Then when the user clicks on a checkbox or whatever, you can use JavaScript to show that DIV tag containing the next control. This is how many JavaScript tab setups work.

Are hidden fields on child window inaccessible from parent window

I have asp.net form that contains fields. When I access this window, my javascript functions can access the fields via the DOM with the getElementById() method and when I postpack to the server I am receiving the updates made by the client.
However, when I launch the form as a child window using Telerik's RadWindow control, the javascript can not access the hidden fields on the child form. Instead I get null.
My questions are:
Are hidden fields on a child window
not accessible when the window is
launched from a parent asp.net form?
Has anyone attempted this with Telerik controls and run into issues?
EDIT
Craig pointed out that the id may be different. Two additional questions then:
Can you ensure that the id you assign at the server is actually used?
Is using getElementByName() a better mechanism to access DOM elements?
To get the ID of your asp.net control do something like this:
<%= theControl.ClientID %>
getElementByName is not as commonly used as getElementById. The ID attribute is supposed to be unique for each element on the page whereas the name attribute can be duplicated.
It is quite possible that the element's ID is not what you think it is. Check the rendered page and see if the ID is there. I am guessing that the page is given a different ID since it is rendered inside another control.
If that is the case, you can have the form render some script that returns the element ID by accessing the controls client ID.
David, I'm sending you this answer because I saw the same issue in my code, and the only REAL solution I found was that I had to support the "OnClick" function in two places... In my case, I was using PetersDatePackage, but it was on a Telerik RAD Strip.
In my case, the control was on a .ascx page, and the JS code was as follows:
function OnIncidentDateChange(ctrl, dtDate, bErr)
{
var weekday = new Array(7);
weekday[0] = "Sunday";
weekday[1] = "Monday";
weekday[2] = "Tuesday";
weekday[3] = "Wednesday";
weekday[4] = "Thursday";
weekday[5] = "Friday";
weekday[6] = "Saturday";
<%=LabelDayOfWeek.ClientID %>.innerText = weekday[dtDate.getDay()];
}
But, this itself was not enough. I had to add THIS code to my parent page. The page that holds the controls for the Telerik strip.
// Dummy function?
function OnIncidentDateChange()
{
}
Once I did that, it worked...
I'm not certain why, to tell you the truth, and it makes no sense to me, and may just be a issue with the PDP package...
I use getElementsByName for checkboxes within the same group.
As for the control's ID, TonyB has the right idea, but make sure you refer to the ClientID property in the PreRender event handler, because if you do it too early in the page life cycle, it will not be available yet).
Is it possible the that javascript is trying to get a reference to the hidden field before the RadWindow has loaded it? I believe I've run into this before and had to use setTimeout to get around the problem.

Resources