reading form value of a control (TextBox) in ASP.Net - c#

I would like to read the Form value of a control (e.g TextBox), i.e 'Request.Form["[Control_Name_Here]"]. The problem with using say TextBox.Text is because if you explicity set it yourself in the Page_Load, there is no way you can get back the 'original value' submitted in the form.
As you know, Asp.Net generates a unique ID/Name for the control. The Request.Form is based on the name attribute of a control. Each webcontrol has a ClientID property, however this does not match the name. The name seems to be almost like the ClientID, having $ instead of _. Is there a way to easily get the value from the form, without resorting to having to replace the _ to a $?
And this should also cater for other naming-conventions, because as from Asp.Net you can also choose to have a control's id statically-generated, rather than dynamically.

It sounds to me like you're not looking for the .ClientID property, but the .UniqueID property of the Control.
See: MSDN
Edit: Also, is there a reason you're always setting the .Text property within the page load? Instead of for example check the Page.IsPostBack property instead and only set the .Text if it's false?

I personally use jquery to find the control and read and set the value
GetFormValue = function (idName) {
var srchP = '[id='+idName+']';
var ctrl = $(srchP);
if (ctrl != null)
return ctrl.val();
return null;
}
so assume th id name in server part is txMytext
and it will be .....$txMytext in client
and by calling GetFormValue('txMytext') in client side you cna get the value of control
dont foget to use jquery library

Related

How to keep controls' url within a module in dnn?

I create a module in DNN 7, which have 10 controls. These controls call through each other with different parameters.
i need to keep the navigation URL of my controls in order to use in "Back" button in each control.
I used this code : Response.Redirect(DotNetNuke.Common.Globals.NavigateURL());
but it returns to the very first control of my module.
How can i have a simple breadcrumb like object within my module which keeps previous control with all its parameters?
You can store the last control and all the parameters in a Session variable:
Session["LastControlKey"] = "Edit";
...and get the values again using:
string lastControl = (string)Session["LastControlKey"];
More examples for the session variable on MSDN: http://msdn.microsoft.com/en-us/library/6ad7zeeb.ASPX
Edit: To get the control's URL, you can use NavigateURL with the control's key:
DotNetNuke.Common.Globals.NavigateURL(this.TabId, lastControl)

Changing input value using javascript

I've been looking for a reason why this doesn't work, but I can't find one. In my asp.net application I create a a bunch of hidden inputs in c# and then try to modify them in javascript before I call them back to the server.
My c# code:
hidden3 = new HtmlInputHidden();
hidden3.ID = "total";
hidden3.Value = index.ToString();
this.Controls.Add(hidden3);
my javascript code:
mod = document.getElementById("total");
mod.value = newVal;
I can call the value back fine but it doesn't change. I have also added alerts for the original value and then the value after changing values and they both show up fine. However the code is never changed so when I pull the values
To get the value back I am using this;
HtmlInputHidden hiddenControl = (HtmlInputHidden)FindControl("total");
Have you verified that the resulting input tag as the ID of "total"? By default, in Webforms, the actual client-side ID is prefixed with the parent's Id (and a delimiting character); this helps to ensure that IDs are unique. One way to get the real client-side Id is to pull the value from the ClientID property of the control, but you should only look at that value once it has been put in a Controls collection.
These controls are dynamically created and they have to be created in each postback. However, these should be built before Page_Load preferably in Page_Init event handler. If these are created in Page_Load, the view state has already been processed and the control can't be set from the posted value.

change Visibility of a control in ASP.NET code behind (c#)

How do you set the visibility of a (fileupload) control from ASP.net code (I need to hide a fileupload control in a webuser control from server site, otherwise hasFIle is always false).
Also setting the "Visible" property to false does not work (as is confuses the AJAX panel so the fileupload forgets that it has a file).
theFileUpload.Visible = false => does not work
so I want to try to set the CSS style visibility to hidden or display to none.
The main problem is I want to do it from the server side (I know how I could do it on client).
Is there a safe way to overwrite
theFileUpload.Attributes["styles"]
in case I modify other CSS styles in there,
also throwing a whole CSS class at it (by moidifying the CSSClass property) seems like overkill.
thanks in advance
Axel
By using theFileUpload.Visible = false; you just tells to asp.net to not render theFileUpload on the page.
You may use
theFileUpload.Attributes.CssStyle[HtmlTextWriterStyle.Visibility] = "hidden";
That allows you to set only a specific css property.

ASP.NET: aggregating validators in a user control

I am developing a web application where I would like to perform a set of validations on a certain field (an account name in the specific case).
I need to check that the value is not empty, matches a certain pattern and is not already used.
I tried to create a UserControl that aggregates a RequiredFieldValidator, a RegexValidator and a CustomValidator, then I created a ControlToValidate property like this:
public partial class AccountNameValidator : System.Web.UI.UserControl {
public string ControlToValidate {
get { return ViewState["ControlToValidate"] as string; }
set {
ViewState["ControlToValidate"] = value;
AccountNameRequiredFieldValidator.ControlToValidate = value;
AccountNameRegexValidator.ControlToValidate = value;
AccountNameUniqueValidator.ControlToValidate = value;
}
}
}
However, if I insert the control on a page and set ControlToValidate to some control ID, when the page loads I get an error that says Unable to find control id 'AccountName' referenced by the 'ControlToValidate' property of 'AccountNameRequiredFieldValidator', which makes me think that the controls inside my UserControl cannot resolve correctly the controls in the parent page.
So, I have two questions:
1) Is it possible to have validator controls inside a UserControl validate a control in the parent page?
2) Is it correct and good practice to "aggregate" multiple validator controls in a UserControl? If not, what is the standard way to proceed?
Addressing your second question first- I don't think it's a good idea to "aggregate" validators together this way unless you include the controls you're validating in the user control. Too much work for not enough payoff.
You could fix this problem by exposing properties on your AggregatedValidator to set the names of the controls to validate, and pass in the ClientID of those controls you want validated.
I believe ASP.NET expects the ControlToValidate ID to be in the same naming container. You can probably override the validation method and use Parent.FindControl.
EDIT: This might be a good place to use a CompositeControl rather than a UserControl. They are designed for just this kind of aggregation. But you might have a similar NamingContainer issue.

ASP.NET: generate property value when adding control to page

Context: ASP.NET 3.5 / C#
Hi,
I created a user control
public partial class MyControl : UserControl
{
// EDIT: example first used "UniqueId" as property name, which was wrong.
public Guid MyId { get; set; }
// ...
}
and this example usage
<uc1:MyControl
ID="myControl"
MyId="443CBF34-F75F-11DD-BE2F-68C555D89123"
runat="server" />
Steps:
Add this control to a web form (aspx)
Expected result:
the HTML for the user control is added, a unique value (corresponding to Guid.NewGuid()) for MyId is set in the ASPX HTML at design-time as the MyId attribute value.
Actual result:
the HTML for the user control is added, a unique value for MyId is not set in the HTML at design time for the MyId Attribute value.
If this is not possible:
Workaround 1: Would it be possible to achieve this using a server control? How?
Workaround 2: is it possible to achieve this using a UserControl design-mode task?
Clarification:
persisting the property value is not an issue, since it never changes for a control intance and is automatically set by ASP.NET through the control declaration in the aspx page.
the MyId attribute does not need to be rendered at runtime.
Gr B!
Custom Design-time Control Features in Visual Studio .NET
You have a couple problems here, but first I will answer your questions about the workarounds.
No you are already using a server control.
No design-mode is to just make the lives of the developer easy, it doesn't effect anything else
You have two problems here. There is already a property called UniqueID I don't know if you were trying to overload that, but the question wasn't clear. The second problem is that your UniqueID essentially not getting stored anywhere. Try the following code:
public Guid UniqueId {
get { return (Guid)ViewState["MyUserControlUniqueId"]; }
set { ViewState["MyUserControlUniqueId"] = value; }
}
That will store the GUID in the ViewState so that you can retrieve it on post backs.
Update: Given your comment you need to override/use this method to add attributes to the rendered content.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.webcontrol.addattributestorender.aspx
If I understand your question correctly, you are exposing a property on your user control called MyId. This allows you to set the property wherever you put that control.
What you also want, is for the rendered HTML to also include this attribute and value.
If that's the case, the property MyId is not passed through to the HTML, it's only because the user control has MyId as a property that it's visible in the designer.
In your user control you will have defined the markup that gets rendered.
So for example if you have:
<asp:Panel runat="Server" Id="myControlDiv">Some other content</asp:Panel>
You can then in your controls prerender event (or wherever else you choose) put
myControlDiv.Attributes.Add("MyId", SomeGuid.ToString())
Which will then get output in the HTML as
<div id="generatedID" MyID="443CBF34-F75F-11DD-BE2F-68C555D89123">Some other content</div>
So you just want a unique ID generated that you only ever use in design time?
Why not override Object.GetHasCode();
And then exposure this as a property?

Categories

Resources