ASP.NET DropDownList SelectedIndexChanged with UpdatePanel AsyncPostBackTrigger - c#

My DDL doesn't work with SelectedIndexChanged, or rather it only worked for the first time. Second time onward it doesn't trigger the drpItemType_SelectedIndexChanged method anymore.
ASPX
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:DropDownList ID="drpItemType" runat="server" CssClass="drpDown" Width="370px" OnSelectedIndexChanged="drpItemType_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Text="Computer (Desktop/Laptop)" Value="PC"></asp:ListItem>
<asp:ListItem Text="Others" Value="Others"></asp:ListItem>
</asp:DropDownList>
<asp:Label ID="lblID1" runat="server" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="drpItemType" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
Code Behind
protected void drpItemType_SelectedIndexChanged(object sender, EventArgs e)
{
if (drpItemType.SelectedValue == "PC")
{
lblID1.Text = "PC";
}
else if (drpItemType.SelectedValue == "Others")
{
lblID1.Text = "Others";
}
}

Actually your code works fine. I copied it and pasted it into a new project and it runs fine every time. Try it yourself. I think you have something else on your page which is causing a JavaScript error, and stopping subsequent postbacks from working. Hope this helps.

Related

SelectedIndexChanged only fires after button click

SelectedIndexChanged only works after form submit.If i select the data from drpdown list that it selects the index and gets data from database to texboxes.But it doesnot do that instead if i click the submit button it shows the data
I tried using postback but it didn't work.Is there any alternative to solve this problem.
Html
<div class="col-md-4">
<asp:Label ID="Label10" runat="server" Text="Related Word"></asp:Label>
<asp:UpdatePanel ID="update" runat="server">
<ContentTemplate>
<asp:TextBox ID="txtRelatedWord" runat="server" class="form-control" AutoPostBack="true" OnTextChanged="txtRelatedWord_TextChanged" placeholder="Word" ></asp:TextBox>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="txtRelatedWord" EventName="TextChanged" />
</Triggers>
</asp:UpdatePanel>
</div>
<div class="col-md-4" style="margin-top:37px">
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:DropDownList ID="cmbRelatedWord" CssClass="form-control" AutoPostBack="true" OnSelectedIndexChanged="cmbRelatedWord_SelectedIndexChanged" runat="server" >
</asp:DropDownList>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="cmbRelatedWord" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
</div>
Code Behind
private static void InsertRequest(ref RequestModel model, string connection)
{
using(var con = new SqlConnection(connection))
{
using(var cmd = new SqlCommand("Insert into infromationRequests (Gender, Age, IsMedicine, IsTransplant, Operations, ResponseJson) values (#Gender, #Age, #IsMedicine, #IsTransplant, #Operations, NULL); SELECT SCOPE_IDENTITY();", con))
{
cmd.Parameters.AddWithValue("#Gender", model.Gender);
cmd.Parameters.AddWithValue("#Age", model.Age);
cmd.Parameters.AddWithValue("#IsMedicine", model.Medicine);
cmd.Parameters.AddWithValue("#IsTransplant", model.Transplant);
cmd.Parameters.AddWithValue("#Operations", model.Operations);
con.Open();
model.Id = Convert.ToInt32(cmd.ExecuteScalar());
con.Close();
}
}
}
Well, I managed to do a simple test which worked fine :
Back :
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Console.WriteLine("Load triggered");
}
protected void ddlist_SelectedIndexChanged(object sender, EventArgs e)
{
Console.WriteLine("event triggered");
tb.Text = (sender as DropDownList).SelectedValue;
}
}
And front :
<asp:ScriptManager runat="server" ID="smanager"></asp:ScriptManager>
<asp:UpdatePanel runat="server" ID="upanel">
<ContentTemplate>
<asp:DropDownList ID="ddlist" runat="server" OnSelectedIndexChanged="ddlist_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Text="1" Value="1"></asp:ListItem>
<asp:ListItem Text="2" Value="2"></asp:ListItem>
</asp:DropDownList>
<asp:TextBox runat="server" ID="tb" Text="Default"></asp:TextBox>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlist" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
According to this I can give you those clues :
Did you try to put both component in the same Update panel ?
Are you sure your event isn't triggered ? Or mayeb you just didn't used any Postback check in your page ? If you don't check if your page is a postback on page load, then your component contents will be reloaded before the even is triggered.

How do I update only a panel on check changed?

<asp:UpdatePanel id="up1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
<ContentTemplate>
<asp:CheckBox ID="chkParent" runat="server" autopostback="true" OnCheckedChanged="chkParent_OnCheckedChanged" />
<asp:DataList ID="ChildList" runat="server" OnItemDataBound="ChildList_OnItemDataBound">
</ContentTemplate>
</asp:UpdatePanel>
protected void chkParent_OnCheckedChanged(object sender, EventArgs e)
{
this.ChildList.DataSource = this.ChildValues;
this.ChildList.DataBind();
}
On check changed of parent, child gets updated but the entire page also loads. Any way to avoid it?

Radio button with default checked value do not fire oncheckedchanged event in asp.net

I have two radio buttons and i have made one default checked.
Both button has oncheckedchanged functions.On page load the radioSource is checked and dropdownlist listSource is enabled.
Below is my code.
<asp:RadioButton ID="radioCampaign" runat="server" AutoPostBack="True" GroupName="inquiryRadio" oncheckedchanged="radioCampaign_CheckedChanged" />
Campaign
<asp:UpdatePanel ID="UpdatePanel3" runat="server">
<ContentTemplate>
<asp:DropDownList ID="lstCampaign" runat="server" Width="206px" Height="27px" AutoPostBack="True" Enabled="False" ></asp:DropDownList>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="radioCampaign" EventName="checkedchanged" />
</Triggers>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="radioSource" EventName="checkedchanged" />
</Triggers>
</asp:UpdatePanel>
<asp:RadioButton ID="radioSource" runat="server"
GroupName="inquiryRadio" oncheckedchanged="radioSource_CheckedChanged" Checked="true" AutoPostBack="True" OnSelectedIndexChanged="radioSource_CheckedChanged" />
Source:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="lstSource" runat="server" Width="206px" Height="27px" onselectedindexchanged="lstSource_SelectedIndexChanged" Enabled="False" AutoPostBack="True" >
<asp:ListItem value='0' Selected="True">--Select--</asp:ListItem>
<asp:ListItem >Email</asp:ListItem>
<asp:ListItem >Telemarketing</asp:ListItem>
<asp:ListItem >Banner Exchange</asp:ListItem>
<asp:ListItem >Agent</asp:ListItem>
<asp:ListItem >Advertisement</asp:ListItem>
<asp:ListItem >Website</asp:ListItem>
<asp:ListItem >Others</asp:ListItem>
<asp:ListItem >Event Listing</asp:ListItem>
<asp:ListItem >Enq On Call</asp:ListItem>
<asp:ListItem >Online Chat Inq.</asp:ListItem>
</asp:DropDownList>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="radioCampaign" EventName="checkedchanged" />
</Triggers>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="radioSource" EventName="checkedchanged" />
</Triggers>
</asp:UpdatePanel>
code behind:
if (!IsPostBack)
{
lstSource.Enabled = true;
}
protected void radioCampaign_CheckedChanged(object sender, EventArgs e)
{
try
{
lstCampaign.Enabled = true;
lstSource.Enabled = false;
lstAgent.Enabled = false;
radioCampaign.Checked = true;
radioSource.Checked = false;
}
catch (Exception Ex)
{
}
}
protected void radioSource_CheckedChanged(object sender, EventArgs e)
{
try
{
lstCampaign.Enabled = false;
lstSource.Enabled = true;
}
catch (Exception Ex)
{
}
}
Now when the page is loaded the source radiobutton is checked and dropdownlist for source is enabled. When i click/check campaign radio button listsource is disabled and list campaign is enabled as per the code behind.
But when i click/check source radio button again manually the radioSource_CheckedChanged event is not fired and as a result the list source remains disabled.
I tested by removing the default checked of radio_soucre than the code was working fine. But i need the default checked functionality in my application. What is suppose to be done? please help.

ASP UpdatePanel doesn't work when used in ContentPage

My code works fine when used in a normal webform. but when I use it in a webform using masterpages, It doesn't work.
page header : ~/Manager/BaseManager.master
and some nested master pages : Base.master > Pages.master > BaseManager.master
ASP
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger EventName="Click"
ControlID="btnUpdateEditPage" />
</Triggers>
<ContentTemplate>
<asp:Label ID="lblTest" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
<asp:Button ID="btnUpdateEditPage" CssClass="btnUpdateEditPage" runat="server"
Text="Button" OnClick="btnUpdateEditPage_Click" />
C#
protected void btnUpdateEditPage_Click(object sender, EventArgs e)
{
lblTest.Text += "**";
}
Do the following please:
1- Add UpdatePanel1.Update(); like the following:
protected void btnUpdateEditPage_Click(object sender, EventArgs e)
{
lblTest.Text += "**";
UpdatePanel1.Update();
//Your UpdatePanel should be UpdateMode="Conditional" as what you have now..
}
2- Put the button inside the update Panel
3- Remove the Trigger to not fire a post back, so your code has to be like:
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="lblTest" runat="server" Text="Label"></asp:Label>
<asp:Button ID="btnUpdateEditPage" CssClass="btnUpdateEditPage" runat="server"
Text="Button" OnClick="btnUpdateEditPage_Click" />
</ContentTemplate>
</asp:UpdatePanel>
Place the button inside UpdatePanel.
SOLVED.
I change
<form id="form1" runat="server" action="post">
to
<form id="form1" runat="server">
and my problem solved.
But why?!

ImageButton is not being enabled when using AsyncPostBackTrigger in ajax update panel in asp.net

i am using asp.net with c#. i have two image buttons : open and delete.
By default they are disabled i.e. imgOpen.Enabled = false; imgDelete.Enabled = false;.
I have a GridView which shows search results from table. GridView contains a radio button which when selected, should enable Open and delete image button.
if I won't use ajax update panel, then every time i select a radio button my page reloads and it's quite disturbing and not friendly.
<asp:TemplateField>
<ItemTemplate>
<asp:UpdatePanel ID="updateRadioButton" runat="server">
<ContentTemplate>
<asp:RadioButton ID="rdoBtnFileOption" runat="server" OnCheckedChanged="rdoBtnFileOption_CheckedChanged" AutoPostBack="true" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="rdoBtnFileOption" EventName="CheckedChanged" />
</Triggers>
</asp:UpdatePanel>
</ItemTemplate>
</asp:TemplateField>
protected void rdoBtnFileOption_CheckedChanged(object sender, EventArgs e)
{
imgOpen.Enabled = true;
imgDelete.Enabled = true;
RadioButton curretnRdo = sender as RadioButton;
GridViewRow currentRow = (GridViewRow)curretnRdo.NamingContainer;
int index = currentRow.RowIndex;
try
{
foreach (GridViewRow grv in grdSearchResults.Rows)
{
if (grv.RowType == DataControlRowType.DataRow && grv.RowIndex != index)
{
RadioButton rdo = new RadioButton();
rdo = (RadioButton)(grv.FindControl("rdoBtnFileOption"));
rdo.Checked = false;
}
}
}
catch (Exception ex)
{
form.MessageBox.Show(ex.Message, "Error", form.MessageBoxButtons.OK, form.MessageBoxIcon.Error);
}
}
These two line are not working for me.
imgOpen.Enabled = true;
imgDelete.Enabled = true;
Please Suggest any approach.
I hope i am quite clear.
Try the following
Wrap your imgDelete with another UpdatePanel and set UpdateMode=Conditional and set Trigger
<asp:UpdatePanel ID="updateRadioButton" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:Image ID="imgOpen" runat="server"/>
<asp:Image ID="imgDelete" runat="server"/>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="rdoBtnFileOption"
EventName="CheckedChanged" />
</Triggers>
</asp:UpdatePanel>
or set UpdateMode="Always" and No Triggers
For using AsyncPostBack, the contents should be in Update panel also.
you can use another Asp:UpdatePanel with UpdateMode="Always" having your images in it.
<asp:UpdatePanel ID="UP1" runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:ImageButton ID="imgOpen" runat="server" ImageUrl="" Visible="false" AlternateText="this is Open Image" />
<asp:ImageButton ID="imgDelete" runat="server" ImageUrl="" Visible="false" AlternateText="this is Delete Image" />
</ContentTemplate>
</asp:UpdatePanel>
May this Help You.

Categories