How to get full ID from asp.net control for <label> - c#

I'm trying to get apply the ID from an asp.net control for a <label>'s for property for accessibility. For some reason I can't get the full ID that .NET creates.
HTML on ASPX Page
<td>
<label id="lblSelReport" runat="server" for="selReport">Select Report Type</label>
<asp:DropDownList ID="selReport" runat="server"></asp:DropDownList>
</td>
What is rendered is:
<td>
<label id="AxMstr_ExMstr_bodyPlaceHolder_bodyPlaceHolder_lblSelReport" for="selReport">Select Report Type</label>
<select name="AxMstr$ExMstr$bodyPlaceHolder$bodyPlaceHolder$selReport" id="AxMstr_ExMstr_bodyPlaceHolder_bodyPlaceHolder_selReport">
...
</select>
</td>
I would think the for property of <label> would be "AxMstr_ExMstr_bodyPlaceHolder_bodyPlaceHolder_selReport" since I'm giving it the ID of the DropDownList control. Clearly I'm doing something wrong but I don't know what.
Any thoughts? Thanks!

Server controls all have a 'ClientID' property, which resolves to whatever messed up value .Net gives that control at runtime. Something like this:
<asp:Label ID="lblName" runat="server" />
<%= lblName.ClientID %>

Related

Programmatically put labels in div or label

I would like to set the value of my label (or div) as an element and not text.
Example, this code creates a list of checkboxes as followed in this post How to use Checkbox inside Select Option :
<asp:Label runat="server" id="checkboxesList">
<label for="one">
<input type="checkbox" id="one" />First checkbox</label>
<label for="two">
<input type="checkbox" id="two" />Second checkbox</label>
<label for="three">
<input type="checkbox" id="three" />Third checkbox</label>
</asp:Label>
I need to create my List in CodeBehind and not directly in ASPX I tried :
checkboxesList.Text = "<label for=\"one\"> < input type = \"checkbox\" id = \"one\" /> First checkbox </ label > <label for= \"two\" > < input type = \"checkbox\" id = \"two\" /> Second checkbox </ label >";
Doing so it only prints as a string and do not create the differents labels.
How to implement it from the code behind having just :
<asp:Label runat="server" id="checkboxesList">
</asp:Label>
It's not necessary to use server-side controls to generate all HTML elements. It is possible to use HTML directly for container elements. Use server-side controls only for elements that should be accessible from server code.
<div id="checkboxes">
<label>
<asp:CheckBox ID="one" runat="server" Text="First checkbox" />
</label>
<label>
<asp:CheckBox ID="two" runat="server" Text="Second checkbox" />
</label>
<label>
<asp:CheckBox ID="three" runat="server" Text="Third checkbox" />
</label>
</div>
The snippet allows you to get/set values and text of checkboxes on server-side. Also you will be able to add server-side click event handler for each of the checkbox.
Note, the for attribute is used to bound the <label> with <input> that is not placed inside the <label>. Moreover the attribute is not supported by IE. So, in your case it is possible to place checkboxes into labels instead of using of for attribute.
Also you need to know the HTML id attribute in general case is not equal to ASP ID value. Therefore, if you want to link controls using for attribute, then set id attribute value through ClientID property.
Thank you all for the answers.
I've finally decided to use a CheckBoxList :
<asp:Label runat="server" id="LabelList">
<asp:CheckBoxList runat="server" ClientIDMode="Predictable" DataValueField="Value" DataTextField="Name" ID="specificInfoListControl" Width="150">
</asp:CheckBoxList>
</asp:Label>
And I retrieve the values with :
foreach (ListItem item in specificInfoListControl.Items)
{
if (item.Selected)
{
}
}

How to read user input from .aspx and store it in SQL Server database in .aspx.cs

I'm new to programming in asp.net, I'm still learning and right now I'm building a login form.
This is my aspx (HTML file):
<div class="login">
<input type="text" placeholder="User" name="user" id="user" runat="server"><br>
<input type="password" placeholder="Password" name="password" id="password" runat="server"><br>
<button type="submit" class="btn-sucess" id="btn" runat="server" onserverclick="btn_Click">Login</button>
</div>
I need to create a button click event in aspx.cs to store the data in SQL Server, in my registration form I did this:
cmd.Parameters.AddWithValue("user", user.Text);
cmd.Parameters.AddWithValue("password", password.Text);
The difference was that the TAG in the .aspx file for registration was like this:
<td class="auto-style8">
<asp:TextBox ID="nome" runat="server" Width="159px"></asp:TextBox>
</td>
How can I do the same that I did in the registration form without using these asp tags?
You are trying to get rid of ASP.NET tag controls which effectively are syntactic sugar around HTML equivalents. You could re-write you ASP Textbox tag as below:
<input type="text" ID="nome" runat="server" style="width:159px"></input>
The runat="server" attribute gives you the flexibility to access this control at code behind (aspx.cs) and if you do not intend to do that, it can be removed as well.
from code behind access the value of text control like this -
string userName = user.Value;
you can directly access value of controls by using name of that control
In your case
<td class="auto-style8">
<asp:TextBox ID="nome" runat="server" Width="159px"></asp:TextBox>
</td>
you can access this value in .cs file like
cmd.Parameters.AddWithValue("user", nome.Text);

Error setting Control Value within asp:ListView LayoutTemplate

So I'm really not sure what I'm doing wrong here, but my code is clearly not working. I have my list view doing everything that I need, except that when I use the ListView.FindControl() method, and then set a property on that control, it give me an Object reference not set to an instance of an object. error. Here is my code:
ASPX
<p class="rates-title"><span>
<asp:Literal ID="currentDate" runat="server"></asp:Literal></span><br>
Todays Rates</p>
<span class="rates-arrow sprite"></span>
<asp:ListView ID="RatesList" ItemPlaceholderID="RSSPlaceholder" runat="server">
<LayoutTemplate>
<ul>
<asp:PlaceHolder ID="RSSPlaceholder" runat="server"></asp:PlaceHolder>
<li>
<asp:HyperLink ID="AllRatesLink" CssClass="all-rates" runat="server">
View All Rates<span></span>
</asp:HyperLink>
</li>
</ul>
</LayoutTemplate>
<ItemTemplate>
<li>
<div class="rate-text">
<p><%# Eval("Name") %></p>
<div class="rate">
<p><%# Eval("InitialRate") %>%</p>
</div>
</div>
<%# Eval("Apr") %>
<div class="todays-rates-rollover">
<p><%# Eval("ContentTruncated") %></p>
</div>
</li>
</ItemTemplate>
</asp:ListView>
Code Behind
currentDate.Text = DateTime.Now.ToString("MM.dd.yy");
HyperLink allRatesLink = (HyperLink)RatesList.FindControl("AllRatesLink");
allRatesLink.Text = "hello";
So the weird thing is that currentDate works just fine, the data that I use (elsewhere in my codebehind) works just fine for iterating through the list, but as soon as I set any property on the allRatesLink control, it gives the object reference not set error. Any ideas/help on this one?
allRatesLink is null because it is not being found which leads to the Object reference error when you try to set a property of null.
You need the FindControl to be called after the LayoutTemplate is created such as in the OnLayoutCreated handler or after DataBind()

How do I create new controls inserted into newly generated HTML on runtime?

I am still rather new to ASP.NET, but I find myself stuck on a problem that shouldn't be too much of a problem..
Right now I have a page that holds this div:
<div id="EditSurveySetID" class="EditSurveySet" runat="server">
<div class="cell">
<div class="cell_title">Survey Set(s)</div>
<table id="surveySetTableData" runat="server" style="margin: 10px;">
<tbody>
<tr>
<td class="form_labelSurveySet" style="width: 330px;">
<input type="button" value="-"> Survey Set 1:
<input id="EditSurveySetTitle" runat="server" style="width: 200px;" value="Netherlands">
</td>
<td>
<asp:DropDownList ID="DDLSurveySetSurveys" runat="server">
</asp:DropDownList>
<asp:Button ID="addAdditionalDDLColumns" runat="server" Text="+" />
</td>
</tr>
</tbody>
</table>
</div>
</div>
Which looks like this:
I want a user to press the + button (addAdditionalDDLColumns). Upon pressing that button, I want a new table row to appear with the same controls in it, so that on runtime, it would look like this at that point:
<div id="EditSurveySetID" class="EditSurveySet" runat="server">
<div class="cell">
<div class="cell_title">Survey Set(s)</div>
<table id="surveySetTableData" runat="server" style="margin: 10px;">
<tbody>
<tr>
<td class="form_labelSurveySet" style="width: 330px;">
<input type="button" value="-"> Survey Set 1:
<input id="EditSurveySetTitle" runat="server" style="width: 200px;" value="Netherlands">
</td>
<td>
<asp:DropDownList ID="DDLSurveySetSurveys" runat="server">
</asp:DropDownList>
<asp:Button ID="addAdditionalDDLColumns" runat="server" Text="+" />
</td>
</tr>
<tr>
<td class="form_labelSurveySet" style="width: 330px;">
<input type="button" value="-"> Survey Set 2:
<input id="Text1" runat="server" style="width: 200px;" value="Netherlands">
</td>
<td>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" Text="+" />
</td>
</tr>
</tbody>
</table>
</div>
</div>
Or in image:
So the way I see, some new HTML code is generated on the + button click event, along with some controls (dropdownlist, another + button with the same functionality(?), possibly a textfield instead of input field).
Questions that come to mind right now:
How do I generate the HTML code that would create a new table row
How do I control where this generated HTML code is added (in this case, it should be under the existing
[Vague/Abstract question] Ultimately a user could possibly have 1 to infinite 'survey sets'. If a user were to have created 4 survey sets, he would eventually press a save button or something alike. Upon pressing this button I would save the 4 selectedvalues of the 4 dropdownlists belonging to the 4 survey sets. How would I call each individual dropdownlist in order to get the data? What I'm actually asking, I think, is whether it's possible to assign an ID programmatically upon the auto generated dropdownlist creation of my previous two questions.
How do I do this? Any advise and tips are very welcome!!
You could use and UpdatePanel and dynamically add new asp controls in the code-behind.
This could be expensive however, because it means your application would be going back to the server every time the user clicks the "Add" button, however I'm not sure how you'd achieve this strictly on the client-side. But there is nothing stopping you creating new controls on the fly on the server-side using asp.net.
If you want to surround the new controls with custom HTML, you could use a PlaceHolder component and replace it with raw text (your HTML) during the callback.
I would suggest you use a GridView.
It provides an option to add rows. It provides a Rows collections, so you can get the data bound to each row.
Here are some examples to get you started.
Dynamically creating, adding and maintaining controls would involve some effort. You would need a PlaceHolder and have to add controls to that. You would have to assign unique ids to each one of them, and use those to retrive the values. This must be done on each PostBack.
Try this when you don't feel you are rather new to asp.net.
protected void Button1_Click(object sender, EventArgs e)
{
HtmlTableRow tr = new HtmlTableRow();
HtmlTableCell td = new HtmlTableCell();
Button btn=new Button();
btn.Text="gdfgd"; /* here u can create ur contol and add it in cell*/
td.Controls.Add(btn); /*add controls to colum*/
tr.Cells.Add(td); /*add column to row*/
surveySetTableData.Rows.Add(tr); /*ad row to table*/
}
instead of using this i would also recommend u to use gridview like nunespaqscal bcz dataretrival become very easy....in gridview

If condition show error for radio button id

I have following input tags inside table in asp.net
<td class="style18">
<asp:CheckBox ID="chkContentTone" onclick="enableGroupBoxTone()"
runat="server" Text="Content Tone" CssClass="ChkBoxStyle"/>
</td>
<td class="styleCntTon">
<input type="radio" id="rdBtnPositive" name="q2" title="Posotivetitle" value="positive"/>
<label for="rdbtnPositive" class="RadioGroup">Positive</label>
<input type="radio" id="rdBtnNegative" name="q2" title="Negativetitle" value="negative"/>
<label for="rdBtnNegative" class="RadioGroup">Negative</label>
<input type="radio" id="rdBtnNeutral" name="q2" title="Neutraltitle" value="neutral"/>
<label for="rdBtnNeutral" class="RadioGroup">Neutral</label>
</td>
but when I put an if condition in, the radio button's the show an error.
Any ideas why this is occurring?
Try to use Asp.net controls:
<asp:RadioButton runat="server" ... />
The markup you posted does not include server side controls hence those controls cannot be accessed via server side code.
Looking at what you want seems like you would need an asp:RadioButton
once you add a asp:radiobutton you can access these controls in your server side code.
Change this:
<input type="radio" id="rdBtnPositive" name="q2" title="Posotivetitle" value="positive"/><label for="rdbtnPositive" class="RadioGroup">Positive</label>
To this:
<asp:RadioButton id="rdBtnPositive" Label="Positive" runat="server" />
Then you can do:
if(rdBtnPositive.Checked)
{
//code it...
}
You'll need to do that for all three radiobutton's, each should have a unique id and be a server side control, namely asp:RadioButton.
you should use asp:RadioButton to be able to access in code behind.
So basically replace all "input" tags with "asp:RadioButton" and remove non sensible properties like Type.
asp:RadioButton usage example

Categories