I have 2 inputs:
<label for="timeStart">Start Time:</label>
<input id="timeStart" value="10:00" type="text" runat="server" clientidmode="static" />
<label for="timeEnd">End Time:</label>
<input id="timeEnd" value="16:00" type="text" runat="server" clientidmode="static" />
and comparer validator inline with submit button
<input class="btn" id="save" value="CREATE Object" type="submit" runat="server"
ValidationGroup="save"/>
<asp:CompareValidator id="CompareTimes"
runat="server"
ControlToCompare="timeStart"
ControlToValidate="timeEnd"
ForeColor="Red" Display="Dynamic"
ErrorMessage="The End Time must be later than the Start Time."
Type="Date"
Operator="GreaterThan"
ValidationGroup="save"/>
in all case it gave me error message, what I do wrong?
Your inputs are client side controls. If you would like to use <asp:CompareValidator.../> you have to use server side label controls such: <asp:Lable runat="server".../>.
If you would like to use these (client side) input labels you have to compare them by JavaScript on the client side.
Related
I'm new to KnockOut and I just wanted to bind selected radio button value to td. For my knowledge I didn't do any mistake but my following code doesn't work anymore. Please help me to solve this minor mistake.
Other code parts are working fine in the same page, So nothing wrong other than this code part.
<td data-bind="text: Sex"></td>
<td>
<asp:RadioButton ID="RadioButtonMale" value="Male" GroupName="RadioGroup1" data-bind="checked: Sex" runat="server" Text="Male"/>
<asp:RadioButton ID="RadioButtonFemale" value="Female" GroupName="RadioGroup1" data-bind="checked: Sex" runat="server" Text="Female"/>
</td>
<script type="text/javascript">
var ViewModel = {
Sex : ko.observable("Male"),
};
ko.applyBindings(ViewModel);
</script>
The html that asp .net is generating is incorrect. It needs to have data-bind="checked: Sex" on the input instead of the span. Instead of using <asp:RadioButton do the following:
<input type="radio" ID="RadioButtonMale" value="Male" Name="RadioGroup1" data-bind="checked: Sex" runat="server"><label for="RadioButtonMale">Male</label>
<input type="radio" ID="RadioButtonFemale" value="Female" Name="RadioGroup1" data-bind="checked: Sex" runat="server" Text="Male"><label for="RadioButtonFemale">Female</label>
Bootstrap login form is below:
<form class="form-vertical login-form" runat="server" action="~/Default.aspx">
<label class="control-label visible-ie8 visible-ie9">User Name</label>
<input class="m-wrap placeholder-no-fix" type="text" placeholder="UserName" name="username"/>
<label class="control-label visible-ie8 visible-ie9">Password</label>
<input class="m-wrap placeholder-no-fix" type="password" placeholder="Password" name="password"/>
<button type="submit" class="btn green pull-right" aria-pressed="undefined">
Log In <i class="m-icon-swapright m-icon-white"></i>
</button>
</form>
When the button is clicked, I want to create the connection to the database. So, I need to have sth like this:
protected void (ButtonName)_Click(object sender, EventArgs e)
{
string connStr = "Initial Catalog=LoginCheck; Data Source=MYCOMPUTER; User id=sa; password=000000;";
SqlConnection conn = new SqlConnection(connStr);
conn.Open();
...
}
But it doesn't work like ASP.NET. If I double-click on the button when I am designing, it's not taking me to code behind. Please put me in the right direction!
In ASP.Net, you want to use Server control if you want to post back.
Most of the time, <form> tag is located inside Master Page, so you cannot style it easily.
Here is an example -
<form id="form1" runat="server">
<div class="form-vertical login-form">
<asp:Label runat="server" ID="UsernameLabel"
AssociatedControlID="UserNameTextBox"
CssClass="control-label visible-ie8 visible-ie9">User Name
</asp:Label>
<asp:TextBox runat="server" ID="UserNameTextBox"
CssClass="m-wrap placeholder-no-fix" />
<asp:Label runat="server" ID="PasswordLabel"
AssociatedControlID="PasswordTextBox"
CssClass="control-label visible-ie8 visible-ie9">Password
</asp:Label>
<asp:TextBox runat="server" ID="PasswordTextBox"
CssClass="m-wrap placeholder-no-fix" />
<asp:LinkButton runat="server" ID="SubmitLinkButton"
CssClass="btn btn-default pull-right"
OnClick="SubmitLinkButton_Click">
Log In <i class="m-icon-swapright m-icon-white"></i>
</asp:LinkButton>
</div>
</form>
But it doesn't work like ASP.NET
Your code (aka "code-behind") looks like it expects ASP.Net server controls e.g. <asp:Button runat="server" id="foo"... so it can do a Postback which is the the ASP.NET "web forms" way.
Having said that, you can try
assigning a bootstrap css class to an ASP.net server control to make it look like a bootstrap button (styling)
keep your existing HTML above handle the normal HTTP POST and not deal with server controls (and deal with request.form)
It's your choice based on what works for you. Either way the core concept is based on standard HTTP POST (html form post, asp.net web forms "postback").
Hth...
Super newbie question here. I'm trying to teach myself Web Forms, so I'm experimenting with the runat="server" attribute. So here's my code:
<p>
Number of items:
<input type="checkbox" name="custom_name_qty" value="25" runat="server" />25
<input type="checkbox" name="custom_name_qty" value="50" runat="server" />50
<input type="checkbox" name="custom_name_qty" value="75" runat="server" />75
<input type="checkbox" name="custom_name_qty" value="100" runat="server" />100
<input type="checkbox" name="custom_name_qty" value="Other" runat="server" />Other
</p>
And here's what gets generated:
<p>
Number of items:
<input name="ctl01" type="checkbox" value="25" />25
<input name="ctl02" type="checkbox" value="50" />50
<input name="ctl03" type="checkbox" value="75" />75
<input name="ctl04" type="checkbox" value="100" />100
<input name="ctl05" type="checkbox" value="Other" />Other
So, it took my group of checkboxes that had the same name, and gave them separate names. Why is this? Even when I give them separate names (add an integer to the end of each one), they still get renamed.
What am I doing wrong here? How can I get the checkboxes to output the same name? How can I even get the checkboxes to keep the names that I give them? I'd rather work with a name I give than "ctl01".
You are not doing anything wrong, that's the default behaviour of asp.net. That is otherwise achieved with <asp:CheckListBox /> control: CheckListBox
<asp:CheckBoxList id="checkboxlist1"
AutoPostBack="True"
CellPadding="5"
CellSpacing="5"
RepeatColumns="2"
RepeatDirection="Vertical"
RepeatLayout="Flow"
TextAlign="Right"
OnSelectedIndexChanged="Check_Clicked"
runat="server">
<asp:ListItem>Item 1</asp:ListItem>
<asp:ListItem>Item 2</asp:ListItem>
<asp:ListItem>Item 3</asp:ListItem>
<asp:ListItem>Item 4</asp:ListItem>
<asp:ListItem>Item 5</asp:ListItem>
<asp:ListItem>Item 6</asp:ListItem>
</asp:CheckBoxList>
That shows how to add items manually. You can also populate the check list from database
System.Data.DataTable dtOptions = GetValuesFromDataBase();
checkboxlist1.DataSource = dtOptions;
checkboxlist1.DataBind();
Update
In Asp.Net 4 and above, you are given more control on how control ID are generated (e.g. AutoID, Static, Predictable, Inherit)
I have a form with 2 Text Boxes (along with other controls). Both of them have TextChanged Event. I'm using Tab Key to go from one Control to another.
After entering some value in the first TextBox and then pressing Tab, the focus is not going to Second TextBox.
I'm using TextBox2.Focus();
But this is not working.
Your Code is not useful for me. My TextBox does have OnTextChanged Event. I implemented TabIndex, but this is not working after PostBack.
Why to do server side code when you can handle it with plain html property tabindex
Plain Html Code
<span>2</span><input type='text' tabindex="1" /><br/>
<span>1</span><input type='text' tabindex="3" /><br/>
<span>3</span><input type='text' tabindex="4" /><br/>
<span>4</span><input type='text' tabindex="2" /><br/>
ASP.NET Code
<asp:TextBox ID="txtField1" runat="server" tabindex="1" />
<asp:TextBox ID="txtField2" runat="server" tabindex="2" />
For working demo see here
I have the following html:
<html>
<body>
<form runat="server">
Name: <input type="text" name="name" />
<br />
<input type="submit" name="submit" />
</form>
</body>
</html>
How do I retrieve the value in the "name" textbox posted back to the webserver to manipulate in ASP.NET WebForms?
(I know about the ASP.NET built-in controls and the possibilities with them, but I am looking for a "clean" solution without the use of built-in ASP.NET controls)
If you can't, or don't want to use asp.net textboxes, then you can retrieve the name of a regular html textbox like this:
string nameTextPosted = Request.Form["name"];
Just note that textboxes created in this manner will not automatically persist their values across postbacks like asp.net textboxes will.
Simplest solution would be to turn it into a server-side component and access it by it's name. e.g.
<asp:TextBox Id="Name" runat="server"></asp:TextBox>
...
string name = Name.Text;
Unless you have other reasons not to use a component, you'd only be making things much more difficult on your part for no justification.
ASP.net includes Html server controls for backward compatibility for just someone like you fond of html. make your html tags server controls by adding the runat="server" and id properties and you are able to access them inside your server side code with their id.
<form runat="server">
Name: <input type="text" name="name" id="name" runat="server" />
<br />
<input type="submit" name="submit" id="name1" runat="server" />
</form>
Now after this you can control their behavior:
name.Value="Hellow World !"
You have to add id and runat="server" in each control. like this :
<input type="text" name="name" id="name" runat="server" />
Its better to use asp:TextBox like this :
<asp:TextBox ID="name" runat="server"></asp:TextBox>