Radio Button Check Changed even not firing - asp.net

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!

Related

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

Asp.Net Update Panel Required Field Validator Issue

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"));
}
}

Updating Label inside UpdatePanel

i have a label and button inside update panel, when i try to get the value from the label on the button click, i get the value from label, but when i try to set the value to the label, it does not happens, i checked for JavaScript error, but there wasn't any, does anyone have any guess what could be the reason. i am using dotnetnuke and here is my code
<asp:UpdatePanel ID="updSection6" runat="server"><ContentTemplate>
<asp:Label ID="lbl" runat="server" />
<asp:ImageButton ImageUrl="/images/edit.gif" ID="btnEditSectionStory6" runat="server" OnClick="Clicked" />
</ContentTemplate></asp:UpdatePanel>
and here's the code
protected void Clicked(object sender, EventArgs e)
{
lbl.Text="Welcome";
}
You need to add the following code
<Triggers>
<asp:PostBackTrigger ControlID="btnEditSectionStory6" />
</Triggers>
Just before your closing </asp:UpdatePanel>
So your code should look like:
<asp:UpdatePanel ID="updSection6" runat="server">
<ContentTemplate>
<asp:Label ID="lbl" runat="server" />
<asp:ImageButton ImageUrl="/images/edit.gif" ID="btnEditSectionStory6" runat="server" OnClick="Clicked" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="btnEditSectionStory6" />
</Triggers>
</asp:UpdatePanel>
ASP PostBackTrigger
Specifies a control and event that will cause a full page update (a
full page refresh). This tag can be used to force a full refresh when
a control would otherwise trigger partial rendering.
You can read more about UpdatePanel's and Triggers here.
C# (using the ImageClickEventArgs)
protected void Clicked(object sender, ImageClickEventArgs e)
{
lbl.Text = "Welcome";
}

UpdatePanel Does Not Update with Preloaded Data

I have an update panel with some textboxes and a button inside. When nothing is preloded into the textbox and I enter the data and click on the button the update panel works fine, but when data is preloaded onto the textbox onload, the update panel would not update my data and the server does not grab the data that was entered onto the textbox. Below is a sample code. Please help.
<asp:UpdatePanel runat="server" id="UpdatePanel1" UpdateMode="Conditional">
<ContentTemplate>
<asp:TextBox runat="server" Columns="30" MaxLength="50" ID="tbxPhone" onblur="PhoneBlur(this)"></asp:TextBox>
<asp:Button ID="findOrderBtn" runat="server" OnClientClick="test()" OnClick="btnSearch_Click" Text="Search..." />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="findOrderBtn" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
Below is an example of the code behind:
protected void btnSearch_Click(object sender, EventArgs e)
{
string phone = tbxPhone.Text;
}
So if tbxPhone Textbox is originally "555-555-5555" on page_load and I change it to "222-222-2222" and click on the Search... button, the data is returned is "555-555-5555" instead of "222-222-2222"
Here is the sample for you try this:
1.Add a script manager on your form
2.Add a update panel and do this way
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:TextBox runat="server" Text="222-222-222" ID="txtValue"></asp:TextBox>
<asp:Button ID="btnsubmit" runat="server" Text="Button" />
<asp:Label ID="lblValue" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</div>
On button_Click event:
Protected Sub btnsubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnsubmit.Click
lblValue.Text = txtValue.Text
End Sub
I have an Page_Load that calls a function to pre-load data into my textbox. Once I hit the search button to update my panel, I realized that it goes through the Page_Load again hence why the data I've entered into the textbox was over written with original pre-loaded data.
Added the !IsPostBack to my Page_Load and fixed my problem.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Code Here
}
}

OnTextChanged event is not firing

I have a TextBox inside my .Aspx page:
<ajax:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:TextBox ID="txtCity" AutoPostBack="true" OnTextChanged="txtCity_TextChanged"
Width="90%" runat="server" ></asp:TextBox>
</ContentTemplate>
</ajax:UpdatePanel>
Code behind:
protected void txtCity_TextChanged(object sender, EventArgs e)
{
lblMessage.Text = "you have typed:" + txtCity.Text;
}
And for lblMessage [on the same .Aspx page]:
<ajax:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="lblMessage" runat="server" Text="" ></asp:Label>
</ContentTemplate>
</ajax:UpdatePanel>
But when I am typing in the TextBox. lblMessage is not updating.
How to rectify this?
It sounds like you're thinking that the OnTextChange event is fired while you are typing in the text box. This is not true. OnTextChange is a server-side event and only fires when the page (or panel) is posted back. Typing into a text box on a page does not post the page back and so this event will only fire once you submit the form.
What you would actually want to do in this case, is to use some JavaScript with the onkeypress JavaScript event to update the label text as things are typed into the TextBox. JavaScript is run on the client and doesn't require you to post back the page in order for it to run.

Resources