I have a webform with a checkbox in it. I need to do two things differently based on an environment setting.
Add a class
Add the Text attribute so a label gets created
<% if setting == true) { %>
<asp:CheckBox ID="optionCheckbox" class="option-checkbox radio-checkbox" runat="server" Text="Label Text"/>
<% } else { %>
<asp:CheckBox ID="optionCheckbox" class="option-checkbox" runat="server"/>
<% } %>
The problem with this is the page won't render because the ids are the same, even though only one could ever get rendered. There is a lot of other processing with javascript and such so I don't want different ids for each scenario.
I was able to fix this by keeping only the "base" checkbox code below and then overriding the PreRender event to add the class and set the Text attribute there.
<asp:CheckBox ID="optionCheckbox" class="option-checkbox" runat="server"/>
Code Behind:
CheckBox optionCheckbox = this.optionCheckbox as CheckBox;
if (optionCheckbox != null)
{
optionCheckbox.Text = "Label Text";
optionCheckbox.Attributes.Add("class", "option-checkbox radio-checkbox");
}
I'd still like to know if there is a way to do this in the markup file though.
Sorry about this syntax , I don't use ASPNet webPage.
You can implement it logically..
Firstly , define class and text variable and set value for business..
Finaly set checkbox class and Text value by variable
var class = "option-checkbox";
var text = "";
if(setting == true)
{
class = "option-checkbox radio-checkbox";
text = "Label Text";
}
<asp:CheckBox ID="optionCheckbox" class="setClassProperty" runat="server" Text="SetTextProperty"/>
Related
I want to load 2 user controls(same but with different text property).
My user control consists of a label with a Function defined for text in ascx.cs
i am loading the control at run time using a panel ..here i want both the user control label to have different texts.
My .ascx file
<asp:Label ID="uclbl" runat="server" />
My .ascx.cs file
public string textforlabel
{
get { return uclbl.Text; }
set { uclbl.Text = value; }
}
My .aspx file
<asp:Panel ID="panelMain" runat="server" >
</asp:Panel>
* i have registered the the control
My .aspx.cs file
Control _dummyUserControl = (Control)Page.LoadControl("~/UserControl/User1.ascx");
_dummyUserControl. ; //can not find the textforlabel here
panelMain.Controls.Add(_dummyUserControl);
because you are making incorrect casting, you should cast to your user control type :
User1 _dummyUserControl = (User1)Page.LoadControl("~/UserControl/User1.ascx");
_dummyUserControl.MyProperty = "SDfsdf" ; //here you can find all your custom properties
panelMain.Controls.Add(_dummyUserControl);
You have to type cast:
User1 _dummyUserControl = (User1)Page.LoadControl("~/UserControl/User1.ascx");
This is the ascx control in .aspx
<Menu:MNU ID="PMPHeaderMenu" runat="server" HiLiter="<%=h%>"></Menu:MNU>
in aspx.cs I have
public int h = 1;
....
....
h = 5;
in ascx.cs I have the HiLiter Property
public string HiLiter { get; set; }
When I debug I get the value as <%=h%> for HiLiter when I expect it to be 5.
How will I pass the server side set value to the user control?
You can't use <%=%> for controls with runat="server" for setting properties, <%=%> is similar like Response.Write.
<%# %> (Data-binding expressions) can be used to fill control properties, but the control should be inside a data-binding container like GridView, DetailsView, FormView and Repeater.
In your case you should set the value of the property from the code behind (aspx.cs) page like following.
PMPHeaderMenu.HiLiter = h;
this.DataBind();
I have defined a variable in C# as the item selected in a drop down.
string parametername = ddlCarrier.SelectedItem.Text;
I now want to pass this variable in my URL to the next page. How do I do this in the href tag?
<asp:LinkButton href="Table.aspx?parameter=<%parametername%>" ID="btnSubmit" runat="server">Click Here</asp:LinkButton>
Purely Server-Side Approach
Instead of a LinkButton, you might want to consider using a HyperLink or <a> tag as you aren't going to be doing anything with your code-behind:
<asp:HyperLink ID="btnSubmit" runat="server" NavigateUrl="Table.aspx" Text="Navigate"></asp:HyperLink>
Then you can use the NavigateUrl property, which you might want to consider setting within your code-behind :
// This will set up your Navigation URL as expected
btnSubmit.NavigateUrl = String.Format("Table.aspx?parameter={0}",ddlCarrier.SelectedItem.Text);
If you use this approach, you may want to explicitly set that a PostBack occurs when your DropDownList changes so that this value will consistently be correct :
<asp:DropDownList ID="dllCarrier" runat="server" AutoPostBack="True" ...>
Client-Side Approach
However, if you are expecting to be able to change this to reflect the current value of your Carrier DropDownList without a PostBack, then you'll likely need to resort to Javascript to populate the value prior to actually navigating :
<!-- Set your base URL within the method and append the selected value when clicked -->
<asp:Button ID="Example" runat="server" OnClientClick="ClientSideNavigate('Table.aspx'); return false;" Text="Navigate"></asp:Button>
<script>
function ClientSideNavigate(url) {
// Get the selected element
var e = document.getElementById('<%= ddlCarrier.ClientID %>');
// Navigate
window.location.href = url + '?parameter=' + e.options[e.selectedIndex].value;
}
</script>
Or you could just avoid ASP.NET Controls altogether and just use an <button> tag :
<button onclick="ClientSideNavigate('Table.aspx'); return false;">Navigate</button>
<script>
function ClientSideNavigate(url) {
// Get the selected element
var e = document.getElementById('<%= ddlCarrier.ClientID %>');
// Navigate
window.location.href = url + '?parameter=' + e.options[e.selectedIndex].value;
}
</script>
You need to handle TextChanged or SelectedIndexChanged event for ddlCarrier and properly set href property of btnSubmit to include ddlCarrier.Text.
I have a hidden field like this:
<asp:HiddenField ID="showHideFlag" runat="server" />
I am assigning some value to this hidden field in java script as follows:
function controlSearchBar() {
if ($("#MainContent_ProjectListControl_searchBar").is(":hidden")) {
$("#MainContent_ProjectListControl_showHideFlag")[0].value = "showing";
} else {
$("#MainContent_ProjectListControl_showHideFlag")[0].value = "hiding";
}
}
I am trying to read this hidden field in ascx.cs page as follows:
string hdnValue = this.showHideFlag.Value;
But this hdnValue is not getting the value of that hidden field.
Can someone help on this?
Hidden as type="hidden"
$("#MainContent_ProjectListControl_searchBar").attr('type') == 'hidden'
Hidden as display: none
$("#MainContent_ProjectListControl_searchBar").is(":hidden")
Gets the control ID for HTML markup that is generated by ASP.NET.
<asp:Label ID="SelectedSport" runat="server" ClientIDMode="Static" ClientID="showHideFlag">
javascript
$("#showHideFlag").text("found");
You are saying that you can get the value in javascript So I think the the problem is with the hidden field. try to set value by Client id as follows-
var hd = document.getElementById('<%= showHideFlag.ClientID%>');
hd.value = "hi";
And my another question is in which event you are accessing value? because If you are setting value in javascript and accessing in Page Load event then it will not work because first of all Page load event gets fired and then Javascript function executes.
I know this question is asked before for assigning text to textbox, but they didn't have answers or the given answers didn't work for me. I have a static function for translations and I'm trying to use that to assign a placeholder to a Textbox. How can I do it in aspx page?
My code is:
<asp:TextBox ID="search" runat="server"
placeholder='<%# islem.DilGetir(7) %>'>
</asp:TextBox>
This one returns this sourcecode:
<input name="ctl00$search" type="text" id="ctl00_search">
You should set this attribute from the page code behind:
search.Attributes["placeholder"] = islem.DilGetir(7)
you can use ajax controll toolkit text box watermark I find it to be mnost usefull in aspx apps
http://www.asp.net/AjaxLibrary/AjaxControlToolkitSampleSite/TextBoxWatermark/TextBoxWatermark.aspx
<asp:TextBoxWatermarkExtender ID="TextBoxWatermarkExtender1" runat="server" TargetControlID ="search" WatermarkText="textthe my name" WatermarkCssClass="watermarked">
</asp:TextBoxWatermarkExtender>
on the back end
TextBoxWatermarkExtender1.WatermarkTex=islem.DilGetir(7);
I experienced the same problem. The exact solution: Take all placeholder's text to hiddenfield component(separated with , ) and with JS, take the inside array hiddenfield text and assign to input text's placeholder attr with jQuery or JS.
this code for multiselectbox choosen js and normal selectbox.
public void SelectFill(DataTable dtResult, HtmlSelect htmlSelect, string placeHolder, string textColumn, string valueColumn, string value)
{
htmlSelect.DataSource = dtResult;
htmlSelect.DataTextField = textColumn;
htmlSelect.DataValueField = valueColumn;
htmlSelect.DataBind();
bool isMultiple = false;
foreach (var item in htmlSelect.Attributes.Keys)
{
if (item.ToString() == "multiple")
{
isMultiple = true;
}
}
if (isMultiple)
{
htmlSelect.Attributes["data-placeholder"] = placeHolder;
}
else
{
ListItem placeHolderItem = new ListItem(placeHolder, "-1");//create placeholder option for non multible selectbox
placeHolderItem.Attributes.Add("disabled", "disabled");
htmlSelect.Items.Insert(0, placeHolderItem);
htmlSelect.Items[0].Selected = true;
}
}