asp.net regex validator conundrum - asp.net

input '12', click button, a is false, b is true... note that EnableClientScript is set to false.
<asp:TextBox ID="TextBox1" runat="server"
Width="20px" MaxLength="3"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1"
runat="server" EnableClientScript="false" ControlToValidate="TextBox1"
ErrorMessage="RegularExpressionValidator"
ValidationExpression="([1-9]|[1-9][0-9]|[1-9][0-9][0-9])"
ForeColor="Red"><sup>invalid</sup></asp:RegularExpressionValidator>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
protected void Button1_Click(object sender, EventArgs e)
{
Page.Validate();
bool a = RegularExpressionValidator1.IsValid;
bool b = System.Text.RegularExpressions.Regex.IsMatch(TextBox1.Text,
RegularExpressionValidator1.ValidationExpression);
}

Replace ValidationExpression with:
ValidationExpression="^([1-9]|[1-9][0-9]|[1-9][0-9][0-9])$"

Related

Dropdown item select change event

I have dropdownlist1 containing items 2wheeler, 3wheeler.
If I select 2 wheeler dropdownlist2 should be open with items gear and non gear.
if I select gear I want to open textbox to write bike model name.
else if I select non gear I want to open just label.
Your design page:
<asp:DropDownList ID="ddl1" runat="server" OnSelectedIndexChanged="ShowDDL2" />
<asp:DropDownList ID="ddl2" runat="server" OnSelectedIndexChanged="ShowControls" Visible="false" />
<asp:TextBox ID="TextBox1" runat="server" Visible="false" />
<asp:TextBox ID="TextBox2" runat="server" Visible="false" />
<asp:Label ID="Label1" runat="server" Visible="false" />
<asp:Label ID="Label2" runat="server" Visible="false" />
Your code behind:
protected void ShowDDL2(object sender, EventArgs e){
ddl2.Visible = true;
}
protected void ShowControls(object sender, EventArgs e){
TextBox1.Visible = TextBox2.Visible = Label1.Visible = Label2.Visible = true;
}
Is this what you need? But I don't see a logic in this...

AutoPost capture what caused it.

I have the following code in my .aspx file:
<asp:TextBox ID="txtSearch" runat="server" Width="278px"></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" Text="Search" AutoPostBack="true" />
When I click on the btnSearch button it does an AutoPostBack.
What my goal is that if btnSearch was clicked, I would then capture the value of txtSearch or else I would not
How do I code such that if btnSearch was clicked on AutoPost I can flag it.
First, a Button does not have an AutoPostBack property. It posts back always.
Second, you can simply handle it's Click event and read the txtSearch.Text property:
<asp:TextBox ID="txtSearch" runat="server" Width="278px"></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="btnSearch_Click" />
codebehind:
protected void btnSearch_Click(Object sender, EventArgs e)
{
string search = txtSearch.Text;
// ...
}

Can not get textbox value inside datalist

I am trying to get the value of textbox "Qty" inside a datalist. It does not work. What is wrong? I have both the CartItem label and the datelist inside the ajax updatepanel. Thanks for any help. Here is my code:
<asp:Label ID="CartItems" runat="server" Text="CartItem"></asp:Label>
<br />
<asp:DataList ID="DataList1" runat="server" CellPadding="10"
DataKeyField="product_id" DataSourceID="SqlDataSource1" RepeatColumns="2">
<ItemTemplate>
<asp:Label ID="product_id" runat="server"
Text='<%# Eval("product_id") %>' /><br/>
<asp:Label ID="product_name" runat="server"
Text='<%# Eval("product_name") %>' />
<br />
Qty
<br/>
<asp:TextBox ID="Qty" runat="server"></asp:TextBox>
<asp:Button ID="ButtonAddToCart" runat="server" Text="Add to Cart"
onClick="ButtonAddToCart_Click"/>
<br />
</ItemTemplate>
</asp:DataList>
and here is the button click event. The CartItem has null value:
protected void ButtonAddToCart_Click(object sender, EventArgs e)
{
CartItem.Text = DataList1.FindControl("Qty").ToString();
}
TextBox Qty = (TextBox)DataList1.FindControl("Qty");
if(Qty != null)
{
CartItem.Text =Qty.Text;
}
You can use NamingContainer or Parent to access sibling controls
protected void ButtonAddToCart_Click(object sender, EventArgs e)
{
var button = sender as Button;
var textbox = button.NamingContainer.FindControl("Qty") as TextBox;
CartItem.Text = textbox.Text;
}
I prefer you to use item_command event of the datalist
do some think like this.
<asp:Label ID="CartItems" runat="server" Text="CartItem"></asp:Label>
<br />
<asp:DataList ID="DataList1" runat="server" CellPadding="10" DataKeyField="product_id" DataSourceID="SqlDataSource1" RepeatColumns="2">
<ItemTemplate>
<asp:Label ID="product_id" runat="server"
Text='<%# Eval("product_id") %>' /><br/>
<asp:Label ID="product_name" runat="server"
Text='<%# Eval("product_name") %>' />
<br />
Qty
<br/>
<asp:TextBox ID="Qty" runat="server"></asp:TextBox>
<asp:Button ID="ButtonAddToCart" runat="server" Text="Add to Cart" CommandName="addtocart2" OnCommand="DataList1_ItemCommand"
/>
<br />
</ItemTemplate>
</asp:DataList>
Here is the item_command event that works.
public void DataList1_ItemCommand(object source, System.Web.UI.WebControls.CommandEventArgs e){
TextBox qtytxtbox = DataList1.FindControl("Qty") as TextBox;
}
You can do like this
protected void ButtonAddToCart_Click(object sender, EventArgs e)
{
Button ButtonAddToCart= (Button)sender;
DataListItem item = (DataListItem)ButtonAddToCart.NamingContainer;
var textbox = (TextBox)item.FindControl("Qty");
}
try this one
TextBox txtquantity = (TextBox)(e.Item.FindControl("Qty"));
simply use this......You will get the value in txtquantity

custom validator is not working in asp.net

<asp:TextBox ID="TextBox3" runat="server" Width="127px"></asp:TextBox>
<asp:CustomValidator ID="CustomValidator1" runat="server"
ControlToValidate="TextBox3"
ErrorMessage="Must Be 8 characters"
onservervalidate="CustomValidator1_ServerValidate"
ValidateEmptyText="True"
ValidationGroup="g1">*</asp:CustomValidator>
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="LogIn"
ValidationGroup="g1" Width="83px" />
protected void CustomValidator1_ServerValidate(object source,
ServerValidateEventArgs args)
{
if (args.Value.Length == 8)
{
args.IsValid = true;
}
else
args.IsValid = false;
}
I want textbox to take 8 characters.But the event is not getting fired up.I haved tried by taking Required Field Validator as well as ValidateEmptyText to true but either of them is not working
Please Help.
Try adding a RequiredFieldValidator for the TextBox too, and it should trigger the custom validator server validate event.
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="TextBox3"
ErrorMessage="Required">
</asp:RequiredFieldValidator>

Working with dropdown list & Validation in asp.net

I have got a dropdown list populated with products from a product table. I have a button named ADD Product, so when the user wants to add a new product instead of selecting product from the drop down list, one should click on add new then panel 1 should show which includes a textbox. Panel1 visibility is set to false by default and it should be visible when the user clicks on the button. Also I want a validation on dropdown list and textbox(if panel is visible). Below is the code which is not working :
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
<%--<asp:TextBox ID="txtMake" runat="server" CssClass="txtStyle"></asp:TextBox>--%>
<asp:Button ID="btnAdd" runat="server" />
<asp:Panel ID="panel1" Visible="false" runat="server"><asp:TextBox ID="txtAddnew"></asp:TextBox></asp:Panel>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Please Select a Product" ValidationGroup="insert" ControlToValidate="DropDownList1" ForeColor="Red"></asp:RequiredFieldValidator><br />
codebehind:
protected void btnAdd_Click(object sender, EventArgs e)
{
panel1.Visible = true;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
MultiView1.ActiveViewIndex = 0;
DropDownList1.DataSource = caravans.GetProductNames();
DropDownList1.DataBind();
}
}
You can use this concept for validation on dropdownlist. I am given you an example please check this...
<asp:DropDownList runat="server" ID="ddl">
<asp:ListItem Value="-1" Text="Select"></asp:ListItem>
<asp:ListItem Value="1" Text="One"></asp:ListItem>
<asp:ListItem Value="2" Text="Two"></asp:ListItem>
</asp:DropDownList>
<asp:RegularExpressionValidator ID="reg1" runat="server" ControlToValidate="ddl"
Display="Dynamic" ErrorMessage="Select Proper" SetFocusOnError="true" Text="Select Proper"
ValidationGroup="check" ValidationExpression="^\d{0,5}$" />
<asp:Button ID="btn" runat="server" ValidationGroup="check" Text="Submit" />

Resources