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

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?

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

updatepanel refresh whole page in asp.net

I use updatepanel in my page like this.
but when I run my app when I click on button that's refresh all of my page.
I do'nt know what I can do to solve this error.
even I use treeview in other page that when I get this error the treeview icon dont show.
please help me.
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="updPanl" runat="server" RenderMode="Block" UpdateMode="Conditional" >
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</ContentTemplate>
</asp:UpdatePanel>
try this
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="updPanl" ChildrenAsTriggers="true" runat="server" RenderMode="Block" UpdateMode="Conditional" >
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
</Triggers>
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</ContentTemplate>
</asp:UpdatePanel>
protected void Button1_Click(object sender, EventArgs e)
{
updPanl.Update();
}
after three days I can find this problem today so I answer this question maybe this help to otherone.
I use routin in my app so webresource and scriptresouce could not load in asp page
I use this code for do not route this resource
routeCollection.Add(new Route("{resource}.axd/{*pathInfo}", new StopRoutingHandler()));

update panel does not work

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 :)

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

checkbox inside a gridview within updatepanel

I have a gridview with a checkbox in first column. I set checkbox property of autopostback="true". The gridview is inside the updatepanel. When checkbox is checked i want to make one panel as visible, which panel is outside the update panel. I check the code with check point, which is go through the code, but its not working. Can any one help me?..
Thanks in advance.
My code is here for your reference...
HTML Code:
<asp:Panel ID="ploperation" runat="server" CssClass="plop" Visible="False">
<asp:LinkButton ID="lbtnasspam" runat="server" CssClass="panelbtn" Font-Names="Calibri"
Font-Size="14px" Font-Underline="False" OnClick="lbtnasspam_Click">Report As Spam</asp:LinkButton>
</asp:Panel>
<asp:UpdatePanel ID="up" runat="server">
<ContentTemplate>
<asp:GridView ID="gvmail" runat="server" AllowPaging="True" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkchild" runat="server" AutoPostBack="true" OnCheckedChanged="chkchild_CheckedChanged"/>
</ItemTemplate>
<ItemStyle Width="15px" />
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
C# Code:
protected void chkchild_CheckedChanged(object sender, EventArgs e)
{
ploperation.Visible = true;
}
You can use initializeRequest - this event is raised when an asynchronous post back occurs(When you check the checkbox in your gridview it initiates an async postback because it's inside an update panel).
Just change ploperation to a div instead of <asp:Panel and you can use javascript to show/hide it:
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<script type="text/javascript">
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_initializeRequest(initializeRequest);
function initializeRequest(sender, args) {
document.getElementById('ploperation').style.display = 'block';
}
</script>
<asp:UpdatePanel ID="up" runat="server">
<ContentTemplate>
<asp:GridView ID="gvmail" runat="server" AllowPaging="True" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkchild" runat="server" AutoPostBack="true" />
</ItemTemplate>
<ItemStyle Width="15px" />
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
<div id="ploperation" style="display:none;" runat="server">
<asp:LinkButton ID="lbtnasspam" runat="server" CssClass="panelbtn" Font-Names="Calibri"
Font-Size="14px" Font-Underline="False" OnClick="lbtnasspam_Click">Report As Spam</asp:LinkButton>
</div>
</form>
Alternatively you can stick to the way you've done it, just place <asp:Panel inside UpdatePanel -> ContentTemplate
update panel does partial page loading....
check link
so if you want to show your panel than include it also in your update panel.
Put the UpdatePanel on the control you want to change, not on the GridView. Do not forget to Reference the GridView on the Triggers section. For example, if you want to change a label text use the following code:
<asp:UpdatePanel ID="up" runat="server">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="gvmail" />
</Triggers>
</asp:UpdatePanel>
<asp:GridView ID="gvmail" runat="server" AllowPaging="True" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkchild" runat="server" AutoPostBack="true" OnCheckedChanged="chkchild_CheckedChanged"/>
</ItemTemplate>
<ItemStyle Width="15px" />
</asp:TemplateField>
</Columns>
</asp:GridView>

Resources