Asp.Net Update Panel Required Field Validator Issue - asp.net

I have an update panel, and inside the update panel there is a dropdown and a required field validator which is associated with the dropdown and the autopostback of dropdown set to true.
now whenever user change the option the autopost back occure in updatepanel.. what i need is that if user pick the empty item from the dropdown it should not postback to the server and validator should fire.... but in my case the validators fire perfecty and also there is a postback and after the postback the required field validaor state is true.
what i need is to stop the postback if validator fails.
i have search for this problem alot but doe's not find any thing helpful.

1- In DropDownList:
CausesValidation="True"
2- In Page_Load use string.Empty to value of first item:
DropDownList1.Items.Add(new ListItem("Select...", string.Empty));
ASPX:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
CausesValidation="True"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="DropDownList1" Display="Dynamic" ErrorMessage="Empty"></asp:RequiredFieldValidator>
<br />
<asp:Label ID="Label1" runat="server" EnableViewState="False"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
Code behind:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Label1.Text = DropDownList1.SelectedItem.Text;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList1.Items.Add(new ListItem("Select...", string.Empty));
DropDownList1.Items.Add(new ListItem("mehdi", "1"));
DropDownList1.Items.Add(new ListItem("ali", "2"));
}
}

Related

Radio Button Check Changed even not firing

I've 2 radio buttons in a user control and control is registered to the page. When i click on the radio button, the event(CheckChanged) is not firing.
<asp:View ID="viewfirst" runat="server">
<asp:UpdatePanel ID="updatepanel1" runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:RadioButton ID="radio1" Text="Yes" Enabled="true" runat="server" />
<asp:RadioButton ID="radio2" Text="No" Enabled="true" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
</asp:View>
Below is code in behind file of the control.
Protected Sub radio1_CheckedChanged(sender As Object, e As EventArgs) Handles radio1.CheckedChanged
//
//
End Sub
It seems everything looks good, but something is wrong. Can you please let me know.
Set autopostback=true for checkbox - This will trigger the event
UpdateMode=conditional and ChildrenAsTriggers=false for the UpdatePanel - so when you trigger checkbox, it won't trigger full postback
You have to set the event handler of each radio button to OnCheckChanged and put them in the same RadioGroup (GroupName) so they do not fire together. Remember to set AutoPostBack = true for each radio button. Set only one radioButton as checked = true. I can see your code has both checked. Something like this...
The aspx page:
<asp:RadioButton id="radioButton1" runat="server" GroupName="btnGrp" Text="Button 1" AutoPostBack="true" Checked="true" OnCheckedChanged="radioButton1_CheckedChanged"></asp:RadioButton>
<asp:RadioButton id="radioButton2" runat="server" GroupName="btnGrp" Text="Button 2" AutoPostBack="true" OnCheckedChanged="radioButton2_CheckedChanged"></asp:RadioButton>
The code-behind (aspx.cs) page:
protected void radioButton1_CheckedChanged(object sender, EventArgs e)
{
// Your code here
}
protected void radioButton2_CheckedChanged(object sender, EventArgs e)
{
// Your code here
}
Hope this helps!

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;
// ...
}

Incomprehension in asp.net

I want when I click button my textbox1 must change, but textbox3 not
Why does not it work?
protected void Page_Load(object sender, EventArgs e)
{
TextBox3.Text = DateTime.Now.ToLongTimeString();
}
protected void Button1_Click(object sender, EventArgs e)
{
TextBox1.Text = DateTime.Now.ToLongTimeString();
}
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button"/>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</ContentTemplate>
<br />
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</asp:UpdatePanel>
When I Click Button1 my TextBox3 changes
Why?
Because when you click the button you submit a request to the server, which results in a round-trip, which results in Page_Load being executed again. You can avoid this by detecting whether the request is part of the postback cycle using the IsPostBack property:
if (IsPostBack) {
}
Or, as is in most cases, doing stuff when it's not a postback:
if (!IsPostBack) {
TextBox3.Text = DateTime.Now.ToLongTimeString();
}

WeForm texbox control. action related to ontextchanged event not happening

Web.UI.Controls.TextBox
in my aspx page i have used-
<asp:TextBox ID="TextBox1" runat="server" OnTextChanged="tmaintextchanged" ></asp:TextBox>
in my code behind :
<asp:Label ID="show" runat="server" ></asp:Label >
protected void tmaintextchanged(object sender, EventArgs e)
{
show.Text = "working";
}
when I am executing this ,Why is the text of label "show" not changing.Kindly help. Am i missing any configuration to the Text box.
You need to set autopostback on TextBox control:
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="true" OnTextChanged="tmaintextchanged" ></asp:TextBox>
Update
see this post:
How do I make a Textbox Postback on KeyUp?

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