Add UserControl Dynamically in Panel Control asp.net page - asp.net

I am creating a website where user gives some value in textbox and clicks a button.
On the button click the UserControl should be added for number of time the user has given input.
What I did is,
protected void btnSearchTaxi_Click(object sender, EventArgs e)
{
for (int i = 0; i < 4; i++)
{
CustomDisplayList cus = new CustomDisplayList();
cus.ID = "cus" + i;
cus.Visible = true;
pnlSearchResult.Controls.Add(cus);
}
}
CustomDisplayList is my user control which is complex control [mixture of labels, textbox, button].
<td style="width:20%; font-family: Century Gothic; font-size:12px; color:White; text-
align:center;">Click to find<br/>
<asp:Button ID="btnSearchTaxi" runat="server" Text="Search Taxi" CssClass="googleButton"
Width="200px" onclick="btnSearchTaxi_Click" />
</td>
<td style="height:150px; width:100%; background-color: #FF6600">
<table style="height:100%; width:100%">
<tr><td style="color:White; font-size:30px; ">Search Result :</td></tr>
<tr><td><asp:Panel ID="pnlSearchResult" runat="server"></asp:Panel>
</td></tr>
</table>
</td>
The code was executed without any error, but the Control was not generated inside the panel.
Now I have changed the CODE to something like
<asp:UpdatePanel ID="UpdatePanel3" runat="server" UpdateMode = "Conditional" >
<ContentTemplate>
<asp:Button ID="btnSearchTaxi" runat="server" Text="Search Taxi" CssClass="googleButton" Width="200px" onclick="btnSearchTaxi_Click" />
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanel4" runat="server" UpdateMode = "Conditional">
<ContentTemplate>
<asp:Panel ID="pnlSearchResult" runat="server" Width="100%" Height="200px" BackColor="Aquamarine"></asp:Panel>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID = "btnSearchTaxi" EventName="Click"/>
</Triggers>
</asp:UpdatePanel>
But the still the Dynamic usercontrol are not getting created.

Related

Display message after account created

I'm having Create User Wizard inside an update panel and here is how I have done:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:CreateUserWizard ID="CreateUserWizard1" runat="server"
DisableCreatedUser="True"
ContinueDestinationPageUrl="~/Login.aspx" MailDefinition-BodyFileName="~/EmailTemplates/NewAccountTemplate.htm" LoginCreatedUser="False">
<ContinueButtonStyle BorderStyle="None" CssClass="bar g-button g-button-submit" Font-Size="12px" />
<CreateUserButtonStyle CssClass="foo g-button g-button-red" Height="30px"
Width="125px" BorderStyle="None" Font-Size="12px" />
<MailDefinition BodyFileName="~/EmailTemplates/NewAccountTemplate.htm" From="no-reply#mihirauniverse.org" IsBodyHtml="True" Priority="High">
</MailDefinition>
<WizardSteps>
<asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server">
<ContentTemplate>
<table>
========//Some code here
<tr>
<td>
<asp:Label ID="confirmmsg" runat="server" Text=""></asp:Label>
</td>
</table>
<asp:UpdateProgress ID="UpdateProgressUserDetails" runat="server" DisplayAfter="0">
<ProgressTemplate>
<div style="position: absolute; top: 384px; left: 169px;">
<img src="Main/images/Loader.gif" alt="loading" /><br />
<span style="font-weight: normal; font-size: small; color: #000000;">Please wait...</span>
</div>
</ProgressTemplate>
</asp:UpdateProgress>
</ContentTemplate>
</asp:CreateUserWizardStep>
<asp:CompleteWizardStep ID="CompleteWizardStep1" runat="server">
</asp:CompleteWizardStep>
</WizardSteps>
</asp:CreateUserWizard>
</ContentTemplate>
</asp:UpdatePanel>
Now I would like to know in which event and how do I display label "confirmmsg" after user account created from codebehind.
use OnCreatedUser "Occurs after the membership provider has created the new Web site user account."
code sample
<asp:CreateUserWizard runat="server" OnCreatedUser="CreateUserWizard1_CreatedUser">
protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
{
// Display the confirm msg
}
I found the answer:
This is what I need to do in the CreatedUser event:
Dim lbl As Label
lbl = CreateUserWizard1.CompleteStep.ContentTemplateContainer.FindControl("confirmmsg")
lbl.Text = "Some text"

By refreshing gridview using timer, modal popup is automatically closing

Am using a modal pop for a grid view column where the grid view is set with timer.
<asp:UpdatePanel ID="GridPanel" runat="server">
<ContentTemplate>
<asp:Timer ID="autorefresh" runat="server" Interval="5000" />
<asp:GridView ID="SigmaGrid" runat="server" AutoGenerateColumns="False"
onrowcommand="SigmaGrid_RowCommand" CssClass="mGrid" PagerStyle-CssClass="pgr"
AlternatingRowStyle-CssClass="alt" AllowPaging="True"
onpageindexchanging="SigmaGrid_PageIndexChanging">
<AlternatingRowStyle CssClass="alt" />
<PagerStyle />
<Columns>
<asp:TemplateField HeaderText="FirstName" SortExpression="FirstName">
<ItemTemplate>
<asp:LinkButton ID="FirstName" runat="server" Text='<% # Eval("FirstName") %>' CommandName="Select"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle CssClass="pgr" />
</asp:GridView>
<asp:Panel ID="DtlsPanel" runat="server" BackColor="White" Height="400" Width="500px" >
<table style="border: Solid 3px #626262; width: 100%; height: 100%"
cellpadding="0" cellspacing="0">
<tr style="background-color: #626262">
<td colspan="2" style="height: 10%; color: White; font-weight: bold; font-size: larger"
align="center">
Records
</td>
</tr>
<tr>
<td style="color: Black">
First Name:
</td>
<td align="center" style="color: Black">
<asp:Label ID="FNamelbl2" runat="server"></asp:Label>
</td>
</tr>
</table>
</asp:Panel>
<asp:Button ID="btnPopUp" runat="server" Style="display: none" />
<asp:ModalPopupExtender ID="DetailsPopUp1" runat="server" BackgroundCssClass="modalBackground"
TargetControlID="btnPopUp" PopupControlID="DtlsPanel" CancelControlID="btnCancel"
PopupDragHandleControlID="PopupHeader">
</asp:ModalPopupExtender>
</ContentTemplate>
</asp:UpdatePanel>
Code
protected void SigmaGrid_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Select")
{
LinkButton FirstName = (LinkButton)e.CommandSource;
GridViewRow row = (GridViewRow)FirstName.NamingContainer;
FNamelbl2.Text = FirstName.Text;
this.DetailsPopUp1.Show();
}
}
Problem
Whne i click on firstname colunn i get a pop up which displays the details of a row.. As i have used timer gridview is refreshed and pop up is automatically closing.
If you're using a ModalPopupExtender with postbacks, you should ensure in codebehind that the popup will be shown. Therefore you can use it's method Show.
For example in the page's or UserControl's PreRender:
C#
protected void Page_PreRender(object sender, System.EventArgs e)
{
if (this.Visible) {
this.DetailsPopUp1.Show();
}
}
VB.NET
Private Sub Page_PreRender(sender As Object, e As System.EventArgs) Handles Me.PreRender
If Me.Visible Then
Me.DetailsPopUp1.Show()
End If
End Sub

How to get text in popup control extender?

i am using a popup control extender.but i am getting only index of selected value from radiobutton list.I want to get the text
Below is my source code
<div class="FloatRight">
<asp:TextBox ID="txtTeam" runat="server" Width="150px" autocomplete="off"></asp:TextBox>
<br />
<asp:Panel ID="panel" runat="server">
<div style="border: 1px outset white; width: 100px">
<asp:UpdatePanel runat="server" ID="up1">
<ContentTemplate>
<asp:RadioButtonList ID="rbteam" runat="server" AutoPostBack="true" OnSelectedIndexChanged="rbteam_SelectedIndexChanged">
</asp:RadioButtonList>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</asp:Panel>
<cc1:PopupControlExtender ID="txtTeam_PopupControlExtender" runat="server" Enabled="True"
and this is server side
protected void rbteam_SelectedIndexChanged(object sender, EventArgs e) {
if (!string.IsNullOrEmpty(rbteam.SelectedValue))
{
txtTeam_PopupControlExtender.Commit(rbteam.SelectedValue);
}
else
{
txtTeam_PopupControlExtender.Cancel();
}
rbteam.ClearSelection();
}
txtTeam_PopupControlExtender.Commit(rbteam.SelectedItem.Text);

UpdatePanel reset textbox background color

I have an UpdatePanel within it a textbox with a background color of yellow and a trigger for text-changed for the textbox and everything works fine except that the background color reverts to white when I write some text in the text box and then focus somewhere else.
What is causing this? Thanks.
Using asp.net 4.0
Here is the asp.net markup:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<span>
<asp:TextBox ID="sticky" runat="server" AutoPostBack="true"
Text='<%# Bind("sticky") %>' TextMode="MultiLine"
OnTextChanged="cSticky" />
</span>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="sticky" EventName="TextChanged" />
</Triggers>
</asp:UpdatePanel>
Here is the css:
#StickyDiv textarea
{
height:170px;
width:185px;
resize:none;
margin-top:1px;
border:none;
font-family:Comic Sans MS;
font-size:1.2em;
padding:3px;
line-height:1.1em;
}
And here is the jQuery:
$(function () {
$("#StickyDiv textarea:even").css("background-color","#ffff95");
$("#StickyDiv textarea:odd").css("background-color", "#fe8ab9");
});
Your problem is that when you do a update in the ajax panel your jquery is not called. If the text is changed you update the box to yellow and not call the javascript because it is rendered outside of your panel. To fix this you can register the script in you codebehide like:
ScriptManager.RegisterClientScriptBlock(Page,typeof(string),"JavaScriptCall",script.ToString(), false);
I did in the folowing example
in ASPX file
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:TextBox ID="TextBox1" runat="server" BackColor="Yellow" AutoPostBack="true" OnTextChanged="textChanged"></asp:TextBox>
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
In Codebehind File
protected void textChanged(object sender, EventArgs e)
{
TextBox1.BackColor = System.Drawing.Color.Yellow;
Label1.Text = TextBox1.Text;
}

Losing Button.Click events after first partial postback in UpdatePanel

I have a Page that has a single instance of a UserControl that itself has a single UpdatePanel. Inside the UpdatePanel are several Button controls. The Click event for these controls are wired up in the code-behind, in the Init event of the UserControl.
I get the Click event for the first button I push, every time, no problem. After that, I only get Click events for one button (SearchButton) - the rest are ignored. I have included the code for the control below - for sake of brevity, I have excluded the click event handler methods, but they are all of the standard "void Button_Click(object sender, EventArgs e)" variety. Any ideas?
<asp:UpdatePanel ID="PickerUpdatePanel" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Panel ID="Container" runat="server">
<div>
<asp:TextBox ID="PickerResults" runat="server" style="margin-right: 3px;" SkinID="Plain" />
<asp:Image
ID="LaunchPopup" runat="server" ImageUrl="~/images/icons/user_manage.png"
ImageAlign="Top" BorderColor="#294254" BorderStyle="Dotted" BorderWidth="1px"
Height="20px" Width="20px" style="cursor: pointer;" />
</div>
<asp:Panel ID="PickerPanel" runat="server" DefaultButton="OKButton" CssClass="popupDialog" style="height: 227px; width: 400px; padding: 5px; display: none;">
<asp:Panel runat="server" id="ContactPickerSearchParams" style="margin-bottom: 3px;" DefaultButton="SearchButton">
Search: <asp:TextBox ID="SearchTerms" runat="server" style="margin-right: 3px;" Width="266px" SkinID="Plain" />
<asp:Button ID="SearchButton" runat="server" Text="Go" Width="60px" SkinID="Plain" />
</asp:Panel>
<asp:ListBox ID="SearchResults" runat="server" Height="150px" Width="100%" SelectionMode="Multiple" style="margin-bottom: 3px;" />
<asp:Button ID="AddButton" runat="server" Text="Add >>" style="margin-right: 3px;" Width="60px" SkinID="Plain" />
<asp:TextBox ID="ChosenPeople" runat="server" Width="325px" SkinID="Plain" />
<div style="float: left;">
<asp:Button ID="AddNewContact" runat="server" SkinID="Plain" Width="150px" Text="New Contact" />
</div>
<div style="text-align: right;">
<asp:Button ID="OKButton" runat="server" Text="Ok" SkinID="Plain" Width="100px" />
</div>
<input id="SelectedContacts" runat="server" visible="false" />
</asp:Panel>
<ajax:PopupControlExtender ID="PickerPopEx" runat="server" PopupControlID="PickerPanel" TargetControlID="LaunchPopup" Position="Bottom" />
</asp:Panel>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="AddButton" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="SearchButton" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="AddNewContact" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
public partial class ContactPicker : System.Web.UI.UserControl
{
protected void Page_Init(object sender, EventArgs e)
{
SearchButton.Click += new EventHandler(SearchButton_Click);
AddButton.Click += new EventHandler(AddButton_Click);
OKButton.Click += new EventHandler(OKButton_Click);
}
// Other code left out
}
It seems that adding UseSubmitBehavior="false" to the button definitions has solved my problem. Still don't know why that first button click worked at all.
The most likely reason for this would be the client IDs that .Net generates for its controls changing. These are dynamically assigned and could change between postbacks / partial postbacks.
If controls are being added to the panel dynamically, the ID of your button could be different between postbacks causing .Net to be unable to tie the click event to the correct event handler in your code.
In my case, i had a LinkButton within a dgPatients_ItemDataBound event handler that used the PostBackUrl property.
The moment i changed the LinkButton to a HyperLink, the problem went away.

Resources