How can I add a custom attribute to a HiddenValueField in ASP.NET? Specifically, I need a class="gmapPoint" attribute for a dynamically generated HiddenValue control. This is necessary for a JavaScript on that page
var hiddenField = new HiddenField();
hfield.Value = "myValue";
hfield.... Attributes["class"]
-- how can I do a similar thing?
Use HtmlInputHidden control instead of HiddenField. It allows programmatic access to the HTML <input type=hidden> element on the server and has .Attributes property:
var hiddenField = new HtmlInputHidden();
hiddenField.Value = "myValue";
hiddenField.Attributes["class"] = "a-class-for-a-hidden-field";
Related
In my asp.net application , I have created HTMLAnchor class and HTMLImage class dynamically.
So that they bind the data from sqlserver at runtime. The code as below,
for (int i = 0; i < _dataset.Tables[0].Rows.Count; i++)
{
//Allows programmatic access to the HTML <a> element on the server.
HtmlAnchor URL = new HtmlAnchor();
URL.HRef = ConfigurationManager.AppSettings["MakerURL"].ToString();
URL.Attributes.Add("col-md-2 select_make", "url");
URL.Target = "_self";
URL.InnerText = "";
//Allows programmatic access to the HTML <img> element on the server.
HtmlImage htmlImage = new HtmlImage();
htmlImage.Src = _dataset.Tables[0].Rows[i][0].ToString();
//add htmlimage control to anchor control
URL.Controls.Add(htmlImage);
//add anchor control to div control
makeList.Controls.Add(URL);
}
So far so good!. Everything works fine. Now my requirement is when i click on image it redirect to another page where i need to show the image which i selected from home page.
How do i proceed with my above code.
Regards
Sangeetha
You may append the Id or name of image as query string in href of anchor.
Add the following to the PageLoad of CodeBehind of target page:
HtmlImage htmlImage = new HtmlImage();
htmlImage.Src = _dataset.Tables[0].Rows[i][0].ToString();
//add htmlimage control to anchor control
Page.Controls.Add(htmlImage);
or
AnyOtherControlInYourPage.Controls.Add(htmlImage);
I am new on Asp.net. I am using Javascript calender It use Input tag.if I use runat="server" so that I can get value on cs page then it does not display calender. How can I get calender value from input tag to .cs page
Simplest way:
Have a hidden field on your control.
Set its in javascript by using :
$('#<%=hdDate.ClientID %>').val("date value here");
and then you can access this hidden field in cs file.
create a hidden field as in your aspx page which should be runat="server".
then
On button click call javascript function say jsOnclikfun() and write this function as
<script type="text/javascript">
function jsOnclikfun() {
var dateInputtext = document.getElementById("Id Of input textbox which you are trying to make runat=server");
var hdDateValue = document.getElementById('<%= hiddenField_Id.ClientID %>');
hdDateValue.value = dateInputtext.value;
return true;
}
</script>
Now on server side where ever you need data get it from that hidden field as hiddenfieldID.Value.
Hope this helps.
I have a custom user control that I made called OrderForm.ascx. I also have an .aspx file that utilizes the OrderForm control.
I want to access a control on the .aspx file from the OrderForm control. Is there a way to do this?
You could use the FindControl method in the user control like this:
Label label = Page.FindControl("Label1") as Label;
if (label != null)
string labelText = label.Text;
As a note on the above, depending on where the Label is in the page, you may need to use recursion to find the Label.
You could also create a property on the page that returns the text of the Label:
public string LabelText
{
get { return Label1.Text; }
}
To access the property from the user control, here are two options:
Option #1
string labelText = ((PageName)Page).LabelText;
Option #2
string labelText = Page.GetType().GetProperty("LabelText").GetValue(Page, null).ToString();
If you have two user controls, ControlA and ControlB, and they are both registered on the same page you can easily access one from the other. Simply create a public property that you want to have access to in ControlB such as:
Public ReadOnly Property ControlB_DDL() As DropDownList
Get
Return Me.ddlItems
End Get
End Property
And then you can reference that property in ControlA after finding that control:
ControlB ctrlB = (ControlB)Page.FindControl("cB");
DropDownList ddl = ctrlB.ControlB_DDL;
Refer here for more info: http://www.dotnetcurry.com/ShowArticle.aspx?ID=155
To access the controls of .ascx in .aspx.
HiddenField selectedEmailsId = performanceReportCtrl.FindControl("CONTROLID") as HiddenField;
And to access controls of aspx in ascx.
HiddenField selectedEmailsId = Page.FindControl("CONTROLID") as HiddenField;
What is the server side control used to add an H4 to the markup from the code behind?
var h4 = new HtmlGenericControl("h4");
h4.InnerHtml = "Heading Text";
parentControl.Controls.Add(h4);
I recommend creating an HtmlGenericControl in your code-behind. The benefit of these over Literals is that they are proper HtmlControls, with the ability for you to programmatically set and modify properties such as InnerHtml, CssClass, Style etc.
HtmlGenericControl myH4 = new HtmlGenericControl("h4")
{
ID = "myH4",
InnerHtml = "Your Heading Here"
});
yourContainerControl.Controls.Add(myH4);
You could use an asp:Literal control - this just writes out the exact text that you set it to.
E.g:
Dim myLiteral as Literal = new Literal()
myLiteral.Text = "<h4>My Heading</h4>"
Then add your Literal to the page.
There is nothing like a <asp:H4 /> control. However, you can add any HTML element to a page via HtmlGenericControl type in your code behind.
For example, to create it:
HtmlGenericControl headerControl = new HtmlGenericControl(HtmlTextWriterTag.H4.ToString());
headerControl.ID = "myHeader";
headerControl.InnerHtml = "Hello World";
placeHolder.Controls.Add(headerControl);
To access it from code behind later:
HtmlGenericControl headerControl = FindControl("myHeader") as HtmlGenericControl;
I'm currently trying to add a FilteredTextBoxExtender in a ASP.NET composite control, but I can't get it to work.
I have the following code within the overridden CreateChildControls method:
var textbox = new TextBox { ID = string.Format("{0}_textbox", ID);
var filteredTextBoxExtender = new FilteredTextBoxExtender { ID = string.Format("{0}_filter", ID), TargetControlID = textbox.ID, FilterType = FilterTypes.Numbers };
Controls.Add(textbox);
Controls.Add(filteredTextBoxExtender);
Somehow, it looks like it is added correctly when I view the source of my test page, but it won't fire the filtering
Sys.Application.add_init(function() {
$create(AjaxControlToolkit.FilteredTextBoxBehavior, {"FilterType":2,"id":"ctl00_testTextBox_testTextBox_filter"}, null, null, $get("ctl00_testTextBox_testTextBox_textBox"));
});
...
<input id="ctl00_testTextBox" name="ctl00$testTextBox$testTextBox_textBox" type="text" value="" id="ctl00_testTextBox_testTextBox_textBox" />
Any idea what goes on here, and how to get it to fire the filtering?
edit:
I've narrowed it down to a matter of dynamically choosing which controls to render. The FilteredTextBoxExtender works if I explicitely render each child control, but not if I don't render one of the child controls in the composite control (a label control)
Try this:
The problem is the textbox's id. If you check the generated JavaScript, you can sense, the input id is different from assigned id to $get("ctl00_testTextBox_testTextBox_textBox"). So your code doesn't work. You should mark your composite control by INamingContainer marker inerface. Thus, the composite control's id combines with the added textbox id and generates an unique identity. It resolves the problem.
public class Foo : CompositeControl
{
protected override void CreateChildControls()
{
var textbox = new TextBox { ID = "textbox" }
var filteredTextBoxExtender = new FilteredTextBoxExtender { TargetControlID = textbox.ID, FilterType = FilterTypes.Numbers };
Controls.Add(textbox);
Controls.Add(filteredTextBoxExtender);
}
}