SelectedIndexChanged only fires after button click - c#

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.

Related

SelectedIndexChanged event of dropDownList not firing in Update Panel

SelectedIndexChanged event of dropDownList not firing in Update Panel and also set AutoPostBack="true".
Below is my Design Code:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="ddl_TypeofCampaign" runat="server" CssClass="form-control" AutoPostBack="true" OnSelectedIndexChanged="ddl_TypeofCampaign_SelectedIndexChanged" AppendDataBoundItems="true" ViewStateMode="Enabled" EnableViewState="true" >
<asp:ListItem Text="Select" Value="0"></asp:ListItem>
<asp:ListItem Text="Email" Value="Email"></asp:ListItem>
<asp:ListItem Text="SMS" Value="SMS"></asp:ListItem>
<asp:ListItem Text="Voice SMS" Value="Voice SMS"></asp:ListItem>
</asp:DropDownList>
<asp:UpdateProgress ID="UpdateProgress1" runat="server" AssociatedUpdatePanelID="UpdatePanel1"
DisplayAfter="1">
<ProgressTemplate>
<div id="IMGDIV" runat="server" align="center" style="visibility: visible; vertical-align: middle; position: absolute; background-color: #fafbf6"
valign="middle">
<asp:Image ID="Image001" runat="server" ImageUrl="~/assets/img/ajax-loader.gif" />
</div>
</ProgressTemplate>
</asp:UpdateProgress>
</ContentTemplate>
</asp:UpdatePanel>
Code behind:
protected void ddl_TypeofCampaign_SelectedIndexChanged(object sender, EventArgs e)
{
Thread.Sleep(2000);
FillTemplates();
btn_Preview.Visible = false;
}
My page Load code.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillGrid();
FillTypeofSourcing();
FillCampaignNames();
FillTemplates();
}
}
Fill Template method where I am getting templates name base on selection.
protected void FillTemplates()
{
if (ddl_TypeofCampaign.SelectedItem.ToString() != "Select")
{
bo.Para1 = ddl_TypeofCampaign.SelectedItem.ToString();
bo.Para2 = "Stage1";//StageValue in TemplateMasterInfo Table
DataTable dt = bl.Get_Templates(bo);
ddl_TypeofTemplateName.DataSource = dt;
ddl_TypeofTemplateName.DataTextField = "TemplateName";
ddl_TypeofTemplateName.DataValueField = "TemplateId";
ddl_TypeofTemplateName.Items.Clear();
ddl_TypeofTemplateName.Items.Add(new ListItem("Select", "0"));
ddl_TypeofTemplateName.DataBind();
}
else
{
ddl_TypeofTemplateName.Items.Clear();
ddl_TypeofTemplateName.Items.Add(new ListItem("Select", "0"));
ddl_TypeofTemplateName.DataBind();
}
}
In Page Tag I mention viewStateEncryptionMode="Never" and I am using Visual Studio 2013. Below is my Page Tag details.
<%# Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="ViewCandidate.aspx.cs" Inherits="ViewCandidate" validateRequest="false" enableEventValidation="false" viewStateEncryptionMode="Never" %>
Try to add Trigger to your Update Panel like shown as below
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddl_TypeofCampaign"
EventName="SelectedIndexChanged" />
</Triggers>
UPDATE
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<Triggers> //this is missing in your code posted
<asp:AsyncPostBackTrigger ControlID="ddl_TypeofCampaign" EventName="SelectedIndexChanged" />
</Triggers>
<ContentTemplate>mydropcode </ContentTemplate> </asp:UpdatePanel>

ASP.NET DropDownList SelectedIndexChanged with UpdatePanel AsyncPostBackTrigger

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.

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.

Why no render partial with UpdatePanel?

I need partial render with ajax; I don't know what is wrong. ¿What is the problem?
My code:
ascx
<div id="temasatratar" onclick="__doPostBack('UpdatePanel1', '');"><h1>Temas a tratar</h1></div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" OnLoad="UpdatePanel1_Load">
<ContentTemplate>
<asp:Label runat="server" ID="Label1" />
</ContentTemplate>
</asp:UpdatePanel>
ascx.cs
protected void UpdatePanel1_Load(object sender, EventArgs e)
{
Random rnd = new Random();
int number = rnd.Next(0, 99999);
Label1.Text = "Best "+number;
}
Any suggest?
My application: Sharepoint - Visual web part / C# / Asp.Net / Visual Studio
I would use a fake-button that is invisible as trigger for the UpdatePanel:
<div id="temasatratar" onclick="__doPostBack('fakeButton', '');"><h1>Temas a tratar</h1></div>
<asp:LinkButton ID="fakeButton" style="display:none" runat="server" Text="foo" OnClick="fakeButton_Click" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" OnLoad="UpdatePanel1_Load">
<ContentTemplate>
<asp:Label runat="server" ID="Label1" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="fakeButton" />
</Triggers>
</asp:UpdatePanel>
Now this click-event is handled in an async postback when the user clicks on the div.
protected void fakeButton_Click(Object sender, EventArgs e)
{
// you are in an asynchronous postback now
}

ASP.NET: ModalPopupExtender prevents button click event from firing

Here is what I'm trying to do: Click a button on my page, which in turn makes (2) things happen:
Display a ModalPopup to prevent the user from pressing any buttons or changing values
Call my code behind method, hiding the ModalPopup when finished
Here is the ASP markup:
<asp:UpdatePanel ID="UpdatePanel2" runat="server" ChildrenAsTriggers="true"
UpdateMode="Always">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnSaveData" EventName="Click" />
</Triggers>
<ContentTemplate>
<asp:Panel ID="pnlHidden" runat="server" style="display: none;">
<div>
<h1>Saving...</h1>
</div>
</asp:Panel>
<cc1:ModalPopupExtender ID="modalPopup"
BackgroundCssClass="modalBackground" runat="server"
TargetControlID="btnSaveData" PopupControlID="pnlHidden"
BehaviorID="ShowModal">
</cc1:ModalPopupExtender>
<asp:Button ID="btnSaveData" runat="server" Text="Save Data"
OnClick="btnSaveData_Click" />
</ContentTemplate>
</asp:UpdatePanel>
Now, here is my code behind C# code:
protected void btnSaveData_Click(object sender, EventArgs e)
{
UpdateUserData(GetLoggedInUser());
modalPopup.Enabled = false;
}
Why doesn't this work? The ModalPopup displays perfectly, but the btnSaveData_Click event NEVER fires.
UPDATE: The first suggestion did not work for me. I also tried your second suggestion (insofar as it applied to me). One minor difference on my end is that there is no button on my modal panel (pnlHidden) -- it's just a message. I did try using Javascript events on the client side, which at least did fire concurrently with my server-side event. This was good news, but I can't seem to find or grab a handle on the ModalPopupExtender or its BehaviorID. This doesn't work:
function showOverlay() {
var popup = $find('modalPopup');
popup.show();
}
popup is ALWAYS equal to null.
FINAL UPDATE: This is my final solution for getting this to work (Take specific note of the use of OnClientClick AND OnClick):
<asp:UpdatePanel ID="UpdatePanel2" runat="server" ChildrenAsTriggers="true"
UpdateMode="Always">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnSaveData" EventName="Click" />
</Triggers>
<ContentTemplate>
<asp:Panel ID="pnlHidden" runat="server" style="display: none;">
<div>
<h1>Saving...</h1>
</div>
</asp:Panel>
<cc1:ModalPopupExtender ID="modalPopup"
BackgroundCssClass="modalBackground" runat="server"
TargetControlID="hdnField" PopupControlID="pnlHidden"
BehaviorID="ShowModal">
<asp:HiddenField ID="hdnField" runat="server" />
</cc1:ModalPopupExtender>
<asp:Button ID="btnSaveData" runat="server" Text="Save Data"
OnClientClick="showModal();" OnClick="btnSaveData_Click" />
</ContentTemplate>
</asp:UpdatePanel>
Using this Javascript function:
function showModal() { $find('ShowModal').show(); }
... And this code behind:
protected void btnSaveData_Click(object sender, EventArgs e)
{
UpdateUserData(GetLoggedInUser());
modalPopup.hide();
}
Try this.
Create a dummy hidden field:
<asp:HiddenField ID="hdnField" runat="server" />
Set the TargetcontrolID = "hdnField" in your Modal Popup declaration.
In your btnSaveData_Click event, do this:
modalPopup.Show();
Try this. It is 100% working
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="Btnshow" runat="server" Text="Show" OnClick="Btnshow_Click" />
<asp:Button ID="BtnTarget" runat="server" Text="Target" Style="display: none" />
<asp:TextBox ID="TextBox1" runat="server">
</asp:TextBox>
<input type="button" value="Get" onclick="abc()" />
<asp:ModalPopupExtender ID="ModalPopupExtender1" runat="server" TargetControlID="BtnTarget"
PopupControlID="Panel1">
</asp:ModalPopupExtender>
<asp:Panel ID="Panel1" runat="server" BackColor="Black" Width="300px" Height="300px">
<asp:UpdatePanel ID="UpdatePanel2" runat="server" ChildrenAsTriggers="false" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button ID="BtnHide" runat="server" Text="Hide Button" OnClick="BtnHide_Click" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="BtnHide" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
</asp:Panel>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Btnshow" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
Server side code
protected void Btnshow_Click(object sender, EventArgs e)
{
ModalPopupExtender1.Show();
}
protected void BtnHide_Click(object sender, EventArgs e)
{
ModalPopupExtender1.Hide();
}
First attempt: Try to set your ButtonID into OkControlID Tag and try again
OR
Second attempt: Call your event over javascript there seems to be some problems with click events
<div>
<cc1:ModalPopupExtender PopupControlID="Panel1"
ID="ModalPopupExtender1"
runat="server" TargetControlID="LinkButton1" OkControlID="Ok"
OnOkScript="__doPostBack('Ok','')">
</cc1:ModalPopupExtender>
<asp:LinkButton ID="LinkButton1" runat="server">LinkButton</asp:LinkButton>
</div>
<asp:Panel ID="Panel1" runat="server">
<asp:Button ID="Ok" runat="server" Text="Ok" onclick="Ok_Click" />
</asp:Panel>
From this example you can easily control panel show up depends on conditions instead of just immediately show up panel once you click button.
<asp:Button ID="btnAdd" runat="server" OnClick="btnAdd_Click"/>
<asp:HiddenField ID="hdnField" runat="server" />
<ajaxToolkit:ModalPopupExtender runat="server"
TargetControlID="hdnField"
ID="btnAdd_ModalPopupExtender"
PopupControlID="pnlPrintName">
</ajaxToolkit:ModalPopupExtender>
<asp:Panel ID="pnlPrintName" runat="server">
.
.
</asp:Panel>
//Server side code:
protected void btnAdd_Click(object sender, EventArgs e)
{
if( dt.Rows.Count == 0 )
{
btnAdd_ModalPopupExtender.Show();
}
else
{
add();
}
}
In code behind, you can do this:
if (true)
{
var script = #"Sys.Application.add_load(function() { $find('behavoirIDModal').show(); });";
ScriptManager.RegisterStartupScript(this, GetType(), "Show", script, true);
}
Change this 'behavoirIDModal'

Categories