update panel does not work - asp.net

im trying to get the value of a dropdownlist and insert it into a label inside an update panel like the following:
<asp:UpdatePanel ID="udpTutorialDropDown" runat="server">
<ContentTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="TutorialSeries" DataTextField="SeriesName" DataValueField="VideoSeriesNameID" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"></asp:DropDownList><br />
<asp:SqlDataSource ID="TutorialSeries" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>" SelectCommand="ViewSeasonName" SelectCommandType="StoredProcedure"></asp:SqlDataSource>
<asp:Label ID="lblEpisode" runat="server" Text="Label"></asp:Label><br />
<asp:TextBox ID="tbxURL" runat="server"></asp:TextBox><br />
<asp:TextBox ID="tbxDiscription" runat="server"></asp:TextBox><br />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="DropDownList1" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
and in code behind i have
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
lblEpisode.Text = DropDownList1.SelectedValue.ToString();
}
but i dont know why it doesnt update the label!! the text of the label remains the same!!! can someone spot the problem??

You have to put drop down list into existing Update panel as well...

Try to remove the trigger to get something like so
<br />
<asp:SqlDataSource ID="TutorialSeries" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>" SelectCommand="ViewSeasonName" SelectCommandType="StoredProcedure"></asp:SqlDataSource>
<asp:UpdatePanel ID="udpTutorialDropDown" runat="server">
<ContentTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="TutorialSeries" DataTextField="SeriesName" DataValueField="VideoSeriesNameID" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"></asp:DropDownList>
<asp:Label ID="lblEpisode" runat="server" Text="Label"></asp:Label><br />
<asp:TextBox ID="tbxURL" runat="server"></asp:TextBox><br />
<asp:TextBox ID="tbxDiscription" runat="server"></asp:TextBox><br />
</ContentTemplate>

You need to call the Update() method of the update panel in your Event as shown below.
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
lblEpisode.Text = DropDownList1.SelectedValue.ToString();
udpTutorialDropDown.Update();
}
All the best!
UPDATE
You must add AutoPostBack="true" property in the drop down list. And, ignore my previous instructions. I.e. calling the Update() method of the update panel. That's required only when you have UpdateMode="Conditional"
This should work :)

Related

Refresh Captcha Code with Updatepanel control in ASP>NET

I have a captcha code in my site.
sometimes needs to chenge that picture , but i dont want the whole page refreh.
i used updatepanel in this way :
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="lblmsg" runat="server" Font-Bold="True"
ForeColor="Red" Text=""></asp:Label>
<asp:TextBox ID="txtimgcode" runat="server"></asp:TextBox>
<asp:Image ID="Image1" runat="server" ImageUrl="~/CImage.aspx" />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Check" />
<asp:Button ID="Button3" runat="server" Text="refresh" OnClick="Button3_Click" />
</ContentTemplate>
</asp:UpdatePanel>
Here is Button3 code behind :
protected void Button3_Click(object sender, EventArgs e)
{
Image1.ImageUrl = "~/CImage.aspx";
}
Button1 Works correctly But Button3 which should change the captcha picture doesn't work.
Am I missing something?
In addition if Iwant to use a refresh image instead of Button3,What controls should I use?
The image likely isn't updating during partial postback because the browser doesn't realize that the image content has changed since it been targeted to same page over and over. Adding querystring might help in this case one need to pass random id as querystring to the ImageUrl of the captcha.
protected void Button3_Click(object sender, EventArgs e)
{
Image1.ImageUrl = string.Format("~/CImage.aspx?img={0}", Guid.NewGuid());
}
There is nothing wrong with your code Kindly check if "~/CImage.aspx" is returning correct values or not. I have modified your code Kindly check:
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:Label ID="lblmsg" runat="server" Font-Bold="True"
ForeColor="Red" Text=""></asp:Label>
<asp:TextBox ID="txtimgcode" runat="server"></asp:TextBox>
<asp:Image Width="100" Height="100" ID="Image1" runat="server" ImageUrl="~/CImage.aspx" />
<asp:Button ID="Button1" runat="server" Text="Check" />
<asp:Button ID="Button3" OnClick="Button3_Click" runat="server" Text="refresh" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button3" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
protected void Button3_Click(object sender, EventArgs e)
{
if (Image1.ImageUrl == "Koala.jpg")
{
Image1.ImageUrl = "Close.png";
}
else
{
Image1.ImageUrl = "Koala.jpg";
}
}

Popup control not staying open on action

I am using the ajaxtoolkit in order to make a popup where users can interact with server controls. Here is a simple implemetation:
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="Server" />
<asp:Button ID="Button1" runat="server" Text="Button" />
<ajaxToolkit:PopupControlExtender ID="PopEx" runat="server"
TargetControlID="Button1"
PopupControlID="Panel1"
Position="Center" />
<asp:Panel ID="Panel1" runat="server">
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="AppName" DataValueField="PK_Application"></asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>" SelectCommand="SELECT [AppName], [PK_Application] FROM [Application]"></asp:SqlDataSource>
</asp:Panel>
It appears to be working correctly, but when I press the button the panel flashes open then dissapears and I can't interact with it. How do I fix this?
The solution is to prevent the postback of the button click:
protected void Page_Load(object sender, EventArgs e)
{
Button1.Attributes.Add("onclick", "return false;");
}

updatepanel asynpostbacktrigger does not throw defined event?

I have the following code.
What i want is when the ddlProvince dropdown is changed, to throw the event SelectedIndexChanged, however that method is never accessed.
<asp:UpdatePanel ID="pnlCountries" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlProvince" EventName="SelectedIndexChanged" />
</Triggers>
<ContentTemplate>
<tr>
<td><asp:Literal ID="Literal37" Text="<%$Resources:glossary,country %>" runat="server"/></td>
<td>
<asp:DropDownList ID="ddlCountries" CssClass="textbox" runat="server">
</asp:DropDownList>
<br />
<cc1:cascadingdropdown ID="cddCountries" runat="server" Category="Country" Enabled="True" LoadingText="<%$Resources:Glossary,loading %>" PromptText="<%$Resources:Glossary,country_choose %>"
ServiceMethod="GetCountries" TargetControlID="ddlCountries">
</cc1:cascadingdropdown>
<asp:RequiredFieldValidator CssClass="errortext" Text="<%$Resources:Glossary,required %>" SetFocusOnError="true" ID="rfvcboScenario" runat="server" InitialValue="" ControlToValidate="ddlCountries" Display="Dynamic" />
</td>
</tr>
<tr>
<td><strong><asp:Literal ID="Literal9" Text="<%$Resources:Glossary,province %>" runat="server" /> *</strong></td>
<td>
<asp:DropDownList ID="ddlProvince" CssClass="textbox" runat="server">
</asp:DropDownList>
<asp:RequiredFieldValidator CssClass="errortext" Text="<%$Resources:Glossary,required %>" SetFocusOnError="true" ID="RequiredFieldValidator1" runat="server" InitialValue="" ControlToValidate="ddlProvince" Display="Dynamic" />
<cc1:CascadingDropDown ID="cddProvince" runat="server" TargetControlID="ddlProvince" ParentControlID="ddlCountries"
Category="Province" LoadingText="<%$Resources:Glossary,loading %>" prompttext="<%$Resources:Glossary,province_select %>" ServiceMethod="GetProvincesForCountry" >
</cc1:CascadingDropDown>
</td>
</tr>
</ContentTemplate>
</asp:UpdatePanel>
Currently this codeline is never hit:
Protected Sub ddlProvince_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddlProvince.SelectedIndexChanged
ReportError("ddlProvince_SelectedIndexChanged", "")
End Sub
update:
previously I had the Autopostback="true" attribute on the ddlProvince control, however, that caused a full postback (issue also described here: Drop Down List (in Update Panel) causing FULL PostBack!)
What am I missing?
EDIT:
You need to set AutoPostBack="true" for the dropdownlist. Change this:
<asp:DropDownList ID="ddlProvince" CssClass="textbox" runat="server">
</asp:DropDownList>
to this:
<asp:DropDownList ID="ddlProvince" CssClass="textbox" runat="server" AutoPostBack="true" >
</asp:DropDownList>
Possibly you haven't set the OnSelectedIndexChanged event in the Markup as seen above.
Three properties you should set: OnSelectedIndexChanged, AutoPostback & EnableViewState
<asp:DropDownList ID="ddlProvince" runat="server"
AutoPostBack="true" EnableViewState="true"
OnSelectedIndexChanged="ddlProvince_SelectedIndexChanged">
</asp:DropDownList>
Incase you are binding your dropdownlist in page_Load event, place it inside !IsPostback condition check:
protected void page_Load ( object sender, EventArgs e )
{
if(!IsPostBack)
{
//DropDownList data bind and all...
}
}

How can i work with the ASP.NET Update Panel?

in my asp.net application i have two dropdownlist,if i select the first one's one value means automatically want to change the values of dropdown two,but its not working.here is my asp code.
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="State"></asp:Label>
<asp:DropDownList ID="ddlState" runat="server" Height="23px" Width="195px" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
AutoPostBack="True">
<asp:ListItem>--Select--</asp:ListItem>
<asp:ListItem>Kerela</asp:ListItem>
<asp:ListItem>Tamilnadu</asp:ListItem>
<asp:ListItem>Karnataka</asp:ListItem>
</asp:DropDownList>
<br />
<br />
<br />
<br />
<asp:Label ID="Label2" runat="server" Text="District"></asp:Label>
<asp:DropDownList ID="ddlDistrict" runat="server" Height="23px" Width="189px" AutoPostBack="True">
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
I copied your code as it is, and gave definition to selected index changed event as
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
ListItem li = new ListItem("text");
ddlDistrict.Items.Add(li);
}
and it worked beautifully. You might have some other issue. Can you copy your selected index changed event code?

asp EntityDataSource detailsview OnItemUpdating e.cancel

I will explain my problem.
So I have an asp page with 2 entityDataSource.
- The first one is used to fill my obout table:
<asp:EntityDataSource ID="EntityDataSource1" ContextTypeName="RichDataBusinessApplication.Web.RichDataEntities" EnableFlattening="false" EnableInsert="true"
EnableUpdate="true" EnableDelete="true" EntitySetName="UserInformations" runat="server" />
My obout table is in an updatepanel. When I want to edit a row in my table, this method is handled:
protected void Button_Command(object sender, CommandEventArgs e)
{
SelectedUserHiddenField.Value = e.CommandArgument.ToString();
UsersCustomerDetailsView.ChangeMode(DetailsViewMode.Edit);
OpenPopupDialog(String.Format("#{0}", UpdatePopupPanel.ClientID));
}
The function OpenPopupDialog open a jquery popup dialog with the informations of my user I can now edit:
<asp:Panel ID="UpdatePopupPanel" runat="server" CssClass="popup-dialog">
<asp:UpdatePanel runat="server" ID="UpdatePanel1" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="UsersGrid" EventName="Select" />
</Triggers>
<ContentTemplate>
<asp:DetailsView runat="server" ID="UsersCustomerDetailsView" DataSourceID="SelectedUserDataSource2" DefaultMode="Edit" AutoGenerateEditButton="true" AutoGenerateRows="false"
OnItemUpdating="UserDetailsView_ItemUpdating" OnItemUpdated="UserDetailsView_ItemUpdated"
OnItemCommand="UserDetailsView_ItemCommand"
DataKeyNames="UserId" Width="100%">
<Fields>
<asp:TemplateField>
<HeaderTemplate>
<uc:CulturalLabel ID="CulturalLabel1" runat="server" Text="Lastname" />
</HeaderTemplate>
<EditItemTemplate>
<asp:TextBox ID="LNameTextBox" runat="server" Text='<%# Bind("Lastname") %>' CssClass="textEntry" ValidationGroup="CoordonneValidationGroupUp"/>
<asp:RequiredFieldValidator ID="CustomRequieredValidator2" runat="server" ControlToValidate="LNameTextBox" CssClass="failureNotification" ErrorMessage="test" ValidationGroup="CoordonneValidationGroupUp">*</asp:RequiredFieldValidator>
</EditItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Panel>
The second EntityDataSource allow to fill the fields of my popup of my detailsview is this one:
<asp:EntityDataSource ID="SelectedUserDataSource2" ContextTypeName="RichDataBusinessApplication.Web.RichDataEntities" EnableFlattening="false" EnableInsert="true" EnableUpdate="true" EnableDelete="true" EntitySetName="UserInformations" runat="server" AutoGenerateWhereClause="True">
<WhereParameters>
<asp:ControlParameter Name="UserId" ControlID="SelectedUserHiddenField" DbType="Guid" PropertyName="value" />
</WhereParameters>
</asp:EntityDataSource>
When I clic on the update button of my popup I handled the "OnItemUpdating" of my detailsview:
protected void UserDetailsView_ItemUpdating(Object sender, DetailsViewUpdateEventArgs e){
Page.Validate("CoordonneValidationGroupUp");
if (Page.IsValid)
{
//code to execute if page is valid
}
else
{
e.cancel = true;
}
}
The problem is the following:
When my page is not valid because of the lastname is blank, "e.cancel=true" is executed but the "*" and error message don't appear and the field of my popup is reloaded with the data of the first row in my database.
So, if someone can help me!
Thanks!

Resources