Page.Request in OnLoad of a Server Control - c#

In the OnLoad of a server control I would like to do the following:
string username = Page.Request["UsernameTextBox"];
The actual controls are then created in CreateChildControls (so I will only need to do this on a postback).
The problem is that I cannot use the IDs in this way, because the control in this case must be referenced by name.If I hard code it i can solve the problem by doing this:
string username = Page.Request["ctl02$ctl00$ctl02$ctl00$ctl02$ctl03$ctl03$ctl02$ctl01"];
Of course this would be a bad solution considering that the control hierarchy could change. So my question is this: Is there a way for me to get this control name in OnLoad. If I could find the method that calculates the name from the NamingContainer or something i would be fine.
(Let's not discuss this wierd design in this login control/page - consider it as the constant of my question).

You can set the ClientIDMode property of your username textbox to static.
http://msdn.microsoft.com/en-us/library/system.web.ui.clientidmode.aspx

Related

Pass Query String to User Control from Parent Page

i have create a user control search.ascx with query string values and i m accessing this page from Parent page(Rcomm.aspx) and now this Page is open from another page(view.apsx) on button click using hyperlink navigateurl. my question is that how can i pass my user control query string from view.aspx
A user control has access to the page's URL the same as any normal ASP.NET page does; thus, in order to retrieve the QueryString from the user control, the normal syntax will work:
Request.QueryString('key')
However, I consider this poor style, because I think of user controls as a modular item you can embed in ANY page: thus tying it directly to a particular querystring value seems to go against that. However you may have valid reasons for this.
I'm not sure I understand the second part of your question:
how i make hyperlink navigate URL with query string for open my Rcomm.aspx
Could you please clarify?
ADDITIONAL: OK it seems to me like you have a link in your user control, and want to use a querystring value from the user control's parent page (Rcomm.aspx) to use in the link to a third page. Correct me if I am wrong.
In this case, you would read the querystring from the user control, then append to another URL using NavigateURL:
URL: Rcomm.aspx?field=value
(in code behind:)
Dim qs as string
qs = Request.QueryString('field')
MyHyperlink.NavigateURL = "newpage.aspx?field2=" & qs
Or something similar. Yes?

ASP.NET Scope of user control for javascript functions

So I have a user control that exists multiple times on a page. From the back end I can call userControl1.someFunction(); and specify which user control I want to call someFunction() for. But if I have a java-script function on the front-end of the user control I can't call it for individual user controls. All I have to do is call javaFunction(), but this doesn't specify which user control I want to call. So this is what I would like to be able to do, clientsideUserControl1.javaFunction(); Is this possible to do? What I have been doing is generating the function name dynamically IE: clientsideUserControl1_javaFunction(), but I feel like there has to be a better way to do this.
usually you can have one function and have it perform it's work on the whole page or you can change it to take a parameter ( a reference to the usercontrol you're interested in )
That way you don't need to have multiple copies of the same javascript function.
So instead of
function CLIENTID_javascriptFunction{
}
You'd have on function at the global level :
function javascriptFunction(id){
}
and call it with the id of the dom object you're interested in. (use ClientID to get the DOM id of the control)
Turns out that in this case it would be better to use a server control instead of a user control. Server controls seem to be a little more complicated to make but they do protect the scope of the javascript functions.
Here is a link that discusses the differences.
http://www.hotscripts.com/forums/asp-net/31174-difference-between-user-control-custom-server-controls.html
one possible solution is this:
function <%= ClientID %>javaFunction()
{
//code here
}
you will have a function declaration for each user control with the client ID of the control plus function name

Referencing an ASP control within a LoginView

Another total newb question, I'm afraid: I have a LoginView with some HyperLinks inside it, but when I try to reference the HyperLink in the code behind it tells me that it doesn't exist in the "current context".
eg. hypLink1.NavigateUrl = "some/link/on/my/site.aspx"
I've figured out that it's only because it's in the LoginView that it can't find it... so what can I do to tell it to look inside the LoginView?
I thought it might be something intuitive like:
LoginView1.hypLink1.NavigateUrl = "some/link/on/my/site.aspx"
But to no avail.
Thanks for any help with this (most likely) really obvious problem!
I'm guessing that you're trying to reference the hyperlink from outside the loginview control?
At that point, you could try a FindControls operation on the LoginView:
HyperLink hypLink1 = (HyperLink)LoginView1.FindControls("hypLink1");
UPDATE
Ok, so I was confused as to what you were asking. The LoginView control only allows FindControls, and so you have to use the code snippet up above in order to reference controls internal to it.
Since the LoginView control uses templates, different controls will exist under different circumstances. As such, the code cannot ensure any given control inside the template will be alive at compile time.
So you'll have to FindControls every time you want to get a child control :'(

Having to call .ClientID to get .ID to populate for a web control

I'm making a forms framework for a project, and have found that when trying to programmatically set the AssociatedControlID property of the asp:Label to its associated TextBox, I have to call ClientID to get ID to populate (not be null).
While debugging, this use of the intermediate window shows the issue:
_inputTextBox.ID
null
_inputTextBox.ClientID
"ctl00_MainContent_ctl01"
_inputTextBox.ID
"ctl01"
(Value is present 2nd time ID is called)
I'm setting _inputLabel.AssociatedControlID = _inputTextBox.ID in an overridden CreateChildControls(), and have tried in RenderContents() of my WebControl.
I've tried loading ClientID into a unused temp variable first, and it works, eg:
var x = _inputTextBox.ClientID;
_inputLabel.AssociatedControlID = _inputTextBox.ID;
Gives: (correct)
<label for="ctl00_MainContent_ctl01">Name of customer</label>
<input type="text" id="ctl00_MainContent_ctl01" name="ctl00$MainContent$ctl01"/>
Instead of: (incorrect)
<span>Name of customer</span>
<input type="text" id="ctl00_MainContent_ctl01" name="ctl00$MainContent$ctl01"/>
My question is - Why do I have to perform this hack? Is there a better way for ID to be reliably populated?
Thanks.
The best thing to do is set the id on the control yourself. If you don't one will automatically be generated when the controls protected EnsureID method is called.
This method is automatically called at various times including, but not limited to, when the getter for ClientID is accessed.
You should also be aware that if you are dynamically manipulating controls then it is possible that the dynamically generated ids could vary based on the order that you add the controls to the page.
Also, even if you aren't dynamically creating the controls the dynamically generated id can vary if you force the id generation (by accessing ClientID) in a different order.
The ID of dynamically created controls is null unless explicitly defined until the RenderControl method is called.

Failing to add controls to a page dynamically

I'm adding a User Control for each record pulled up in a data reader, here's the basic loop:
while (dr.Read())
{
ImageSelect imgSel = new ImageSelect(dr["Name"].ToString());
myPanel.Controls.Add(imgSel);
}
The problem is that there are no controls added to the page, I check the html output and there is my panel, with nothing in it.
I even stepped through the code in the debugger, and verified that myPanel.Controls gets a control added on each loop, with the count being 6, no errors, but then they dont show up on the page.
I've run the above code in the Page_Init and Page_Load events, both with the same result.
EDIT:
Ok so I've switched to using LoadControl("....ascx") to get my control instance, which is now working. But originally I was also passing in data via the controls constructor.. Is this still possible or do I just need to set them via get/sets?
EDIT 2:
Thanks to Freddy for pointing out that the LoadControl has an overload where you CAN pass in constructor params, see accepted answer.
EDIT 3:
After trying this method both with and without the constructor. I have found its better to just use setters for any properties I want the control to have versus trying to use the passed in object array for my constructor.
Update: As Steve pointed out, the overload of LoadControl that uses the type won't take into account the controls in the ascx. This is also mentioned in this answer: Dynamically Loading a UserControl with LoadControl Method (Type, object[]).
As I mentioned before, the get/set are more in line with the asp.net model, so I recommend using that with the LoadControl variation that receives the user control path. That said, the Steve's version is an interesting alternative: http://www.grumpydev.com/2009/01/05/passing-parameters-using-loadcontrol/.
My take is the LoadControl with type is meant to be used with web custom controls instead.
If it is an user control you should use LoadControl(usercontrolpath) to get the instance of the user control.
You can use a constructor by doing:
var name = dr["Name"].ToString();
var imgSel = LoadControl(typeof(ImageSelect), new object[]{ name });
myPanel.Controls.Add(imgSel);
Notice that depending on the project model you are using, you need to add a Reference to the aspx to use it with the typeof variation:
<%# Reference Control="~/somepath/myusercontrol.ascx" %>
Ps. I usually use the set/get for controls as I find them more in line with the asp.net model
To add UserControls you must call the LoadControl method passing in the path to the .ascx file. You can not create them by just instantiating the object the .ascx file inherits from.
A UserControl consists of both the markup and the class in the code behind. The markup contains a link to the class behind, but the class behind does not know where the markup lives and therefore can not be created on it's own.

Categories