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
Related
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)
{
}
}
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);
I have a asp.net page which contains checkbox controls like below:
<input name="REQUEST_DA_TYPE$0" disabled="disabled" id="REQUEST_DA_TYPE_0" type="checkbox" checked="checked"/>
<label for="REQUEST_DA_TYPE_0">
Text - Accept
<input name="REQUEST_DA_TYPE$1" disabled="disabled" id="REQUEST_DA_TYPE_1" type="checkbox" checked="checked"/>
<label for="REQUEST_DA_TYPE_1">
Text - Refuse
Now I need to dynamically change both label's text in C# page.
Help me to solve this.
Why don't you use server controls instead of native html elements directly? That's one of the main features of ASP.NET, isn't it? So, instead of doing this...
<input name="REQUEST_DA_TYPE$0" disabled="disabled" id="REQUEST_DA_TYPE_0" type="checkbox" checked="checked"/>
<label for="REQUEST_DA_TYPE_0">
<input name="REQUEST_DA_TYPE$1" disabled="disabled" id="REQUEST_DA_TYPE_1" type="checkbox" checked="checked"/>
<label for="REQUEST_DA_TYPE_1">
Why don't you do this...?
<asp:CheckBox ID="REQUEST_DA_TYPE_0" runat="server" Checked="True" Enabled="false" Text="Text - Accept"/>
<asp:CheckBox ID="REQUEST_DA_TYPE_1" runat="server" Checked="True" Enabled="false" Text="Text - Refuse"/>
Then, from code behind you can change whatever you want. For example, the text...
REQUEST_DA_TYPE_0.Text = "I've been changed";
enable it...
REQUEST_DA_TYPE_0.Enabled = false;
the font color...
REQUEST_DA_TYPE_0.ForeColor = System.Drawing.Color.Blue;
etc, etc.
My .aspx looks like the following:
<div id="valueIntroduction" type="text" class="labelarea" runat="server">
<input name="$labeluniversityid" type="text" id="labeluniversityid" style="color:#7D110C;border:0;background-color:#C0D5F2;width:100%" />
</div>
.cs File looks like:
if (results.Read())
{
labeluniversityid.value = results["TEXT_CONTENT"].ToString();
}
Exactly what am trying do is am getting the data from the database and displaying it in the valueIntroduction div. That is workiing fine. Now i added a text box with readonly mode. So that in my page if I press EDIT button, the value could be edited.
Use a TexBox component:
<asp:TextBox ID="labeluniversityid" runat="server" CssClass="yourcssclass"></asp:TextBox>
As for the styling:
.yourcssclass
{
color:#7D110C;
border:0;
background-color:#C0D5F2;
width:100%
}
Then, in your code behind you can easily use it like this:
labeluniversityid.Text = results["TEXT_CONTENT"].ToString();
Keep in mind that ASP.NET Controls are translated into common HTML tags, so you can wrap it and style it as you would with any other normal input of type text.
Also: type="text" is not valid for div
Try putting runat="server" attribute in <input id="labeluniversityid"> tag.
Or use a <asp:TextBox> control as areks suggests.
You need to add -
runat="server"
to your input field
Or, even better, use an
<asp:textBox ..>
I am using asp.net C#
I am also using the jQuery UI Calendar control.
calendar control
It appears the calendar control wants to work with an input control with an ID of "datepicker".
<input type="text" id="datepicker" />
I want to use value of the input control in my code behind but seeing how it is not an asp.net control I am not sure how I can reference it in the code behind.
Does anyone know how to get the value of the input control in the code behind?
use
<input type="text" id="datepicker" name="datepicker" />
and in the code behind:
Request.Form["datepicker"]
In fact, Form property of Request is populated with form values.
Here you have to mention your form name which contains input tag
<input name="txtemail" id="txtemail" runat="server" type="text" class="cssspa" form="form"/>
in C#
string email = txtemail.Value.ToString();
now u can get data from textbox
If you want to use basic form pulling, you need to add a name attribute:
<input type="text" id="datepicker" name="datepicker" />
if(Page.IsPostBack)
{
string value = Request.Form["datepicker"];
}
Alternatively, you can mark is as an HtmlInputControl
<input type="text" id="datepicker" runat="server" />
System.Web.UI.HtmlControls.HtmlInputControl datepickerControl = this.FindControl("datepicker") as System.Web.UI.HtmlControls.HtmlInputControl;
if(datepickerControl != null)
string value = datepickerControl.Value;
You can access the value via the Request object like onof wrote, or you can turn every html tag into a server control by adding the attribute runat:
<input type="text" id="datepicker" runat="server" ClientIdMode="static" />
The attribute ClientIdMode="static" ensures that the tags ID is not changed by the ASP.NET runtime.
Then you can access the input by the autogenerated member datePicker.
Since you explicitly say code-behind, I assume you probably want to stay with the WebForms/Controls model? With clientidmode you can do that while also having a determinite id value for the calendar.
<asp:TextBox id="datepicker" ClientIDMode="Static" runat="server" />
Then you can interact directly with the control from the code-behind as normal.
But, the calendar control does not require a specific ID. You can initialize it with whatever ID you want
<asp:TextBox id="DateTextBox" runat="server" />
<script>
$(function() {
$( "#<%=DateTextBox.ClientID%>" ).datepicker();
});
</script>