Reading value from a dynamically added attribute on UserControl - c#

I am trying to read the value from an attribute I have added to a UserControl. Here is how I am implementing this.
The user control is added to my page as follows.
<uc1:myUserControl runat="server" id="myUserControl" />
From that pages code behind file. I have added the following attribute to this control to enable some configuration of how the user control will behave when loaded onto this page.
myUserControl.Attributes.Add("configSize", "Small");
The issue I then have, is making use of that value ('Small') from the UserControls code-behind page. I can iterate through the AttributeCollection, but I can only seem to be able to access the key ('configSize') and so the only thing I can then output is that key name. I am actually after it's value. This then lets me set some sizing config on the user control.
This is how I am at least getting 'something' to show, in this case just the key name. I have no idea how I can fetch that keys value.
IEnumerator keys = this.Attributes.Keys.GetEnumerator();
int i = 1;
while (keys.MoveNext())
{
test.InnerHtml = test.InnerHtml + "<br />" + (String)keys.Current;
i++;
}
I am outputting the text to a span control that I called 'test'. As mentioned above, this only outputs the keys name, and not it's value that I had set for it. I can't see to find any way to get it's value.
Any help would be greatly appreciated!!

Related

Dynamically change value of hidden field

For an existing site, I have to pass values in hidden fields from a form that's loaded by different pages (eg. City1.aspx, City2.aspx, City3.aspx, etc.), but they are loaded inside an iframe. I also have to dynamically change the value of at least one of those hidden field (let's call it "source") based on the city page loading it. I am familiar with PHP and JavaScript/JQuery, but I have no idea how to do this in C#.
I've found tutorials on retrieving the file name (sans extension) via JavaScript. I think I can still get the city even if the form is in an iframe, but I'd like to keep to the site's conventions and use C# if possible.
Code snippets or links to possible solutions would be much appreciated.
if you want modify the value of your input in c# associated to your aspx (Code behind), you must to add attributes runat=server to your input.
use this code in your aspx
<input id="test" type="hidden" runat="server"/>
and in your c#
test.Value = 123; //your value is 123 for example
Disclaimer, I don't know JQuery, so there could be easier ways to do this. I also haven't tested any code...
If you know the exact ID then you can do something like this from the parent page (in a javascript block):
var frame = document.getElementById('myIFrame');
var ctrl = frame.document.getElementById('myControl');
ctrl.value = "New Value";
If you don't know the exact ID's of the controls in the CityX.aspx pages, then you will either need a way for those ID's to be discovered, or you will need to go through all controls within the iframe looking for the correct one. (I say this because if the controls in the iframe pages are held in any sort of ASP.NET structure they will not be called txtMyCtrl (for instance) but possibly something like ct00_txtMyCtrl.)
If you don't know the EXACT control name (because of the ASP.NET structure I mentioned before), you could do something like:
var frame = document.getElementById('myIFrame');
var ctrls = frame.document.getElementByTagName("INPUT");
for(var i=0;i<ctrls.length;i++){
if(ctrls[i].getAttribute("type")=="hidden" && ctrls[i].id.indexOf("_myControl") != -1){
ctrls[i].value = "New Value";
break;
}
}
Or if you have the ability to update the CityX.aspx pages, then you could have the following in the CityX.aspx page:
function getCtrls(){
return [document.getElementById("<%=hiddenCtrl.ClientID%>"),
document.getElementById("<%=anotherHiddenCtrl.ClientID%>")];
}
... and then in your parent page, something like:
var frame = document.getElementById('myIFrame');
var ctrls = frame.document.getCtrls();
for(var i=0;i<ctrls.length;i++){
ctrls[i].value = "New Value";
}
They're just ideas on a general theme

User Controls required an ID in postback?

I add some div into a panel on server side, when the page is generated, and I add a ID for each one :
HtmlGenericControl divContainerInside = new HtmlGenericControl("div");
divContainerInside.ID = "inside_" + m_oIDCategoria + "_" + numero;
than, on postback (after re-creating them), I cycle them :
foreach (HtmlGenericControl divInside in myPanel.Controls.OfType<HtmlGenericControl>())
{
Response.Write(divInside.ID);
}
all is ok! But, if I remove that divContainerInside.ID when I generate it, I get a NullException cycling them. Why?
I guess you get NullException when you try to read the ID, which you haven't set.
If you change your code like this, you'll get the value:
foreach (HtmlGenericControl divInside in myPanel.Controls.OfType<HtmlGenericControl>())
{
Response.Write(divInside.ClientID);
}
PS: I don't know if you have got this line of code:
myPanel.Controls.Add(divContainerInside);
If you want to find out more about web controls you can read this article and this.
You cant add a control to the page using response.Write you need to add it to the Pages or another controls control collection like below:
Page.Controls.Add(divInside);

DotNetNuke 5 open aspx in new window

Another issue I have in DNN5:
I'm currently creating a module that shows a GridView that has a "Edit" column.
When user clicks on "Edit" column, it should open an edit form in a new window.
This edit form is an ASPX-page inside my module folder and it expects a ModuleId parameter in order to access the module Settings; that part works fine and I'm able to retrieve the Module settings.
However, I still have the following issues:
How can I localize my Labels? I have tried DNN's label control, but no success. I also tried asp:Label with "meta:resourceKey", but it looks like it isn't able to access the local resource file.
It's very annoying to use Aspx-pages in my module since it will operate outside DotNetNuke's context. Does anybody knows an approach that allows me to use PortalModuleBase?
I have tought about displaying a DotNetNuke page in the new window, just by referencing the Control to load. However when I do that, it will show me the full page (so with navigation bar, footer, and so on) and I actually just want to show the control.
Besides, I'm only able to open my Aspx-page by referencing to /DesktopModules/MyModule/Page.aspx instead of DNN's NavigateUrl or so.
Thanks for your replies.
DNN will hide all other modules on the page whenever a control (or ctl=mycontrol) is specified for the page. So,
You should change your code from an ASPX page to an ASCX control.
Add the ascx control to the Module Controls section of your module's Module Definition.
Use DNN's NavigateURL function to generate the link. You'll want to use one of the options where you specify the Control Key (i.e. NavigateURL("edit", "SkinSrc=[G]" + Globals.QueryStringEncode( DotNetNuke.UI.Skins.SkinInfo.RootSkin + "/" + Globals.glbHostSkinFolder + "/" + "No Skin" ))
In the above sample, "edit" is the control key you specified for the control.
Why not load the edit interface in another ASCX file rather than an ASPX page? Check out http://dnnsimplearticle.codeplex.com for some examples in C#. It's a basic article module, but does a lot of useful things from a DNN perspective.
Mate for localization Aspx-pages operating outside DotNetNuke's context i suggest you to do it programatically. It will give you more control and you can debug it if some problem arises.
Like EfficionDave suggest use Control Key (i.e. NavigateURL("edit", "SkinSrc=[G]" + Globals.QueryStringEncode( DotNetNuke.UI.Skins.SkinInfo.RootSkin + "/" + Globals.glbHostSkinFolder + "/" + "No Skin" )) method
/Adnan Zameer
http://www.adnanzameer.com

navigation menu doesn't redirect to the given value

the problem i encounted concerns redirecting in the navigation menu. I'd like to dynamicly create a navmenu. Depending on what role the user has we get to see the required navigation menu items.
At the moment i use:
if (found)
{
if (admin == true)
{
NavigationMenu.Items.Add(new MenuItem("Agenda", "/AdminPages/Agenda.aspx"));
NavigationMenu.Items.Add(new MenuItem("Add Product", "/AdminPages/ProductToevoegen.aspx"));
}
else if (user == true)
{}
This code I have placed in my Site.master.cs, but I also have a control in my login.aspx.cs code which does a Response.Redirect("~/AdminPages/Agenda.aspx"); to a certain page depending on admin or user once logged in. Now the problem I have is that when I log in, a part works, so it controls the role and adds the required navigation menu items. But when I click for example on the Add Product link it doesn't redirect me to the page. It keeps redirecting me to :
http://localhost:52853/AdminPages/Agenda.aspx
In the url bar it actually shows the url followed with a # when clicked and than redirect to the Agenda page.
Any ideas on how to fix this problem? I tried finding a way to put the navigation links in the login.aspx.cs code aswell but couldn't find the correct way to refer to the NavigationMenu, don't know if that could be off any help. Thank you in advance.
Look at the parameters for creating a new MenuItem.
When only passing 2 parameters, you are filling the text and value parameters. What you want to pass is a navigateUrl parameter. This is only available when passing an imageURL parameter (which can be empty).
Something like the following should fix your problem.
NavigationMenu.Items.Add(new MenuItem("Agenda", "", "", "/AdminPages/Agenda.aspx"));
Hope this helps.
I would suggest using a standard siteMap object and RoleProvider. And read a little bit about security trimming (you can specify on your sitemap nodes which roles can access which sites).
Hm might not be the correct way but i fixed it making another menu in the site.master.aspx file called MenuAdmin for example. Than in the code behind i've set the
NavigationMenu.visible = false;
and
MenuAdmin.visible = true;
Seems to work fine, does what it needs to do for now ;)

Removing default text from text box

I got this Text box with default value as "First Name" ..Now when I click inside this text box to enter name , this value "First Name" keeps on displaying. What I want is to the text box to clear as soon as I click inside it. What property do I need to set in mt textbox tag ?
[Edit]
ok anything from Telerik that I can use to do that ?
There is not out of the box functionality in TextBox that will accomplish this, but the ASP.Net Ajax Toolkit has a Watermark Extender that will do everything you want.
I have used both, but now personally use a jQuery Watermark Plugin
Either will work just fine, choose based on your needs.
According to the Telerik docs you just have to set the EmptyMessage property on their TextBox control. Demo Page Here
In the code behind, on Page Load you can add the following code to achieve this
TextBox1.Attributes.Add("onClick", "javascript:if(this.value=='First Name'){this.value='';}");
You can use the method suggested by #Josh. If you do not want to use Ajax Toolkit controls or JQuery you could write it on your own using Javascript. Write a function which gets called when the foucs is received by the textbox control. I thik the function is called onfocus or just focus in Javascript.
Hi I just wrote this small function which will achieve your desired result
function clearInputBox(x,prefil){
if(x.value == prefil){
x.value = '';
}
}
Your input box looks like this
<input type='text' value='First Name' onfocus="clearInputBox(this,'First Name')" />
May be this will help you
Taking Shobans advice one step farther, you could add something like this to your Page subclass
protected override void OnInitComplete(EventArgs e)
{
string jsString = "javascript:if(this.value=='" + TextBox1.Text + "'){this.value='';}";
TextBox1.Attributes.Add("onFocus", jsString);
base.OnInitComplete(e);
}
What this will do is, it will always consider that default string is the one this controll contains at esign time (the initial one in your .aspx file), so you wont have to manually change it in codebehind every time you change your .aspx. Remember, that OnIinitComplete fires before any viewstate or postback data has been applied, but after the controlls on your page have been set to their default values.
P.S. As anishMarokey pointed, use onFocus vs onClick, since fields can gain focus without clicks via Tab key.

Categories