I have 2 textbox on my page and a Button. When I click on a button, the text from the textbox is emailed. But, then when i refresh the page and no content is there, then also i get an email.
protected void Button1_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(TextBox1.Text) && !string.IsNullOrEmpty(TextBox2.Text))
{
//email logic
TextBox1.Text = "";
TextBox2.Text = "";
}
else
{
//do nothing
}
}
Here, on clicking the button, i get an email but then when i refresh the page, even though there is no data, then also it goes inside the loop and i get an email.
How do i stop this ?
Do the following in your Page_Load event and keep your TextBoxes and Button in an <asp:UpdatePanel>. Then the page will not ask to re-submit each time you refresh the page.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
TextBox1.Text = string.Empty;
TextBox2.Text = string.Empty;
}
}
UPDATE
Keep the controls within an UpdatePanel as follows
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" >
<ContentTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Send mail" onclick="Button1_Click" />
</ContentTemplate>
</asp:UpdatePanel>
You need to use the the POST > Redirect > GET pattern. Here is explanation link
Related
ASP.Net using C# and WebForms
I am simply trying to populate a TextBox within a FormView with a value of approved when the page loads. The FormView's DEFAULT MODE is Edit and it will remain in Edit only. I am in need of some code and syntax that works. I have tried but I cant get the field to populate. I'm including the FormView markup and the Code Behind. I appreciate anyone that can help with this. I know it may be simple but I have had all kinds of trouble with it and I have not found anything that works.
Most of the solutions I have come across are for Inserting.
FormView1 Markup
<asp:FormView ID="FormView1" runat="server" DataKeyNames="id" DataSourceID="test_default" DefaultMode="Edit" AllowPaging="True" >
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" BackColor="#FFFFCC" Height="35px" Width="200px"></asp:TextBox>
<br />
id:
<asp:Label ID="idLabel1" runat="server" Text='<%# Eval("id") %>' />
<br />
status:
Code Behind
protected void Page_Load(object sender, EventArgs e)
{
if (FormView1.CurrentMode == FormViewMode.Edit)
{
TextBox TextBox2 = FormView1.FindControl("TextBox2") as TextBox;
if (TextBox2 != null)
{
TextBox2.Text = "approved";
}
This worked!
protected void Page_PreInit(object sender, EventArgs e)
{
if (submitted_to_email != null)
{
_ = Context.User.Identity.Name;
if (User.Identity.IsAuthenticated)
submitted_to_email.Text = User.Identity.Name;
}
if (TextBox1 != null)
{
TextBox1.Text = "approved";
}
}
I have calculated the two textbox values and displayed successfully it into third textbox but the issues are when I enter value to textbox1 and move to textbox2 the page get reload as I have mentioned AutoPostBack="true" for textbox1 and textbox2 and when I remove this AutoPostBack="true" the calculated value is not displayed in third textbox.
Here is my code behind
protected void txttotal_TextChanged(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txttotal.Text) && !string.IsNullOrEmpty(txtdiscount.Text))
txtgrandtotal.Text = (Convert.ToInt32(txttotal.Text) - Convert.ToInt32(txtdiscount.Text)).ToString();
}
protected void txtdiscount_TextChanged(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txttotal.Text) && !string.IsNullOrEmpty(txtdiscount.Text))
txtgrandtotal.Text = (Convert.ToInt32(txttotal.Text) - Convert.ToInt32(txtdiscount.Text)).ToString();
}
Here is aspx code
<asp:TextBox ID="txttotal" runat="server" CssClass="textstyle" OnTextChanged="txttotal_TextChanged" AutoPostBack="true"></asp:TextBox>
<asp:TextBox ID="txtdiscount" runat="server" CssClass="textstyle" OnTextChanged="txtdiscount_TextChanged" AutoPostBack="true"></asp:TextBox>
<asp:TextBox ID="txtgrandtotal" runat="server" CssClass="textstyle" ReadOnly="true"></asp:TextBox>
To add the two text box value and show it on third text box you can add them on button click event here is the code
aspx page
<div>
<asp:TextBox runat="server" ID="txt1"></asp:TextBox>
<asp:TextBox runat="server" ID="txt2"></asp:TextBox>
<br />
<asp:Label runat="server" Text="Answer"></asp:Label> <asp:TextBox runat="server" ID="txt3"></asp:TextBox>
<asp:Button runat="server" ID="btn1" Text="CLICK TO ADD" OnClick="btn1_Click"/>
</div>
.cs
protected void btn1_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txt1.Text) && !string.IsNullOrEmpty(txt2.Text))
{
txt3.Text = (Convert.ToInt32(txt1.Text) + Convert.ToInt32(txt2.Text)).ToString();
}
else {
Response.Write("Please Enter Value");
}
}
It kind of seems to me that there is an inherent difficulty in dynamically adding controls in ASP.NET Web Forms. Specifically, for a dynamically added control to be included in the ViewState, and have it's events properly wired up and so forth it is suggested that these be added during the Page_PreInit event.
That said, many times we'll probably want to add such controls according to an event, such as a user clicking a button. Control specific events like Click events always run after Init, and even after Load. Supposed the following .aspx....
<form id="form1" runat="server">
<div>
<asp:PlaceHolder ID="phAddresses" runat="server"></asp:PlaceHolder>
<br /><br />
<asp:Button ID="btnAddAddress" runat="server" Text="Add Another Address" OnClick="btnAddAddress_Click" />
...and the following .aspx.cs....
private static List<AddressUserControl> addresses = new List<AddressUserControl>();
protected void Page_PreInit(object sender, EventArgs e)
{
foreach (AddressUserControl aCntrl in addresses)
{
phAddresses.Controls.Add(aCntrl);
// Helper to find button within user control
addressButtonControl = findAddressControlRemoveButton(aCntrl);
addressUserControlButton.ID = "btnRemoveAddress" + addressCount;
addressUserControlButton.Click += new EventHandler(addressUserControlButton_Click);
addressCount++;
}
}
protected void btnAddAddress_Click(object sender, EventArgs e)
{
AddressUserControl aCntrl = LoadControl("~/UserControls/AddressUserControl.ascx") as AddressUserControl;
addresses.Add(aCntrl);
}
Now in the above situation the number of User Controls displayed is always one behind the number the user has actually added, because the Click event doesn't run until after PreInit, where controls must be added to the placeholder. I must be missing something here, because this seems inherent to the ASP lifecycle, and to require some 'hack' or other.
EDIT - Yeah for some reason EventHandlers I add during btnAddress_Click() won't run, only EventHandlers I add during Page_Init, or declaratively in markup. This is what I tried to do...
protected void btnAddAddress_Click(object sender, EventArgs e)
{
AddressUserControl aCntrl = LoadControl("~/UserControls/AddressUserControl.ascx") as AddressUserControl;
addresses.Add(aCntrl);
phAddresses.Controls.Add(aCntrl);
findAddressControlRemoveButton(aCntrl);
addressUserControlButton.ID = "btnRemoveAddress" + addresses.Count;
///////////////////////////////////////////////////////////////////////////////////
// ADDED EVENT HANDLER HERE
//////////////////////////////////////////////////////////////////////////////////////
addressUserControlButton.Click += new E ventHandler(addressUserControlButton_Click);
}
...but the event won't fire as I've said. Any ideas?
EDIT - Here's the markup for my AddressUserControl. There's no logic in the code behind file
<%# Control Language="C#" ClassName="AddressUserControl" AutoEventWireup="true" CodeBehind="AddressUserControl.ascx.cs" Inherits="XFAWithUserControl.UserControls.AddressUserControl" %>
<asp:Panel ID="pnlAddressForm" runat="server">
<asp:Label ID="lblStreet" runat="server" Text="Street Address"></asp:Label>
<asp:TextBox ID="txtStreet" runat="server"></asp:TextBox>
<br /><br />
<asp:Label ID="lblCity" runat="server" Text="City"></asp:Label>
<asp:TextBox ID="txtCity" runat="server"></asp:TextBox>
<br /><br />
<asp:Label ID="lblState" runat="server" Text="State"></asp:Label>
<asp:TextBox ID="txtState" runat="server"></asp:TextBox>
<br /><br />
<asp:Label ID="lblZip" runat="server" Text="Zip"></asp:Label>
<asp:TextBox ID="txtZip" runat="server"></asp:TextBox>
<br /><br />
<asp:Button ID="btnRemoveAddress" runat="server" Text="Remove Address" />
</asp:Panel>
right now my click event for btnRemoveAddress is just something silly like this...
private void addressUserControlButton_Click(object sender, EventArgs e)
{
Button thisButton = sender as Button;
thisButton.Text = "Why Hello";
}
but my goal is to have it remove the associated AddressUserControl, so that a user can add and/or remove an arbitrary number of AddressUserControls from the page by clicking buttons.
EDIT - Here's what I have now, still doesn't work
protected void btnAddAddress_Click(object sender, EventArgs e)
{
AddressUserControl aCntrl = LoadControl("~/UserControls/AddressUserControl.ascx") as AddressUserControl;
addresses.Add(aCntrl);
phAddresses.Controls.Add(aCntrl);
findAddressControlRemoveButton(aCntrl);
addressUserControlButton.ID = "btnRemoveAddress" + addresses.Count;
aCntrl.ChangeText += new EventHandler(addressUserControlButton_Click);
}
private void addressUserControlButton_Click(object sender, EventArgs e)
{
Button thisButton = sender as Button;
thisButton.Text = "Why Hello";
}
AddressUserControl.ascx
<asp:Panel ID="pnlAddressForm" runat="server">
<asp:Label ID="lblStreet" runat="server" Text="Street Address"></asp:Label>
<asp:TextBox ID="txtStreet" runat="server"></asp:TextBox>
<br /><br />
<asp:Label ID="lblCity" runat="server" Text="City"></asp:Label>
<asp:TextBox ID="txtCity" runat="server"></asp:TextBox>
<br /><br />
<asp:Label ID="lblState" runat="server" Text="State"></asp:Label>
<asp:TextBox ID="txtState" runat="server"></asp:TextBox>
<br /><br />
<asp:Label ID="lblZip" runat="server" Text="Zip"></asp:Label>
<asp:TextBox ID="txtZip" runat="server"></asp:TextBox>
<br /><br />
<asp:Button ID="btnRemoveAddress" runat="server" Text="Remove Address" OnClick="btnRemoveAddress_Click" />
</asp:Panel>
AddressUserControl.ascx.cs
public event EventHandler ChangeText;
protected void Page_Load(object sender, EventArgs e)
{
}
public void btnRemoveAddress_Click(object sender, EventArgs e)
{
if (this.ChangeText != null)
{
ChangeText(sender, e);
}
}
You need to have the logic in your click handler add to the panel's Controls collection as well as to the list of controls, like this:
protected void btnAddAddress_Click(object sender, EventArgs e)
{
AddressUserControl aCntrl = LoadControl("~/UserControls/AddressUserControl.ascx") as AddressUserControl;
addresses.Add(aCntrl);
phAddresses.Controls.Add(aCntrl);
}
UPDATE:
In your user control, you need to define an event that can be defined in the page that hosts the user control, like this:
AddressUserControl.cs (code-behind):
public event EventHandler RemoveAddress;
protected void removeAddressUserControlButton_Click(object sender, EventArgs e)
{
// Find out if the event has been set, if so then call it
if (this.RemoveAddress!= null)
{
RemoveAddress(sender, e);
}
}
Now on your page where you are using the user control, do this:
// Wire up the user control's event
nameOfUserControl.RemoveAddress += new EventHandler(addressUserControlButton_Click);
Finally, implement the addressUserControlButton_Click event:
protected void addressUserControlButton_Click(object sender, EventArgs e)
{
// Do your dynamic control creation here or whatever else you want on the page
}
I have 2 labels and 2 text boxes and 1 buttons displayed.
When the page loads the Name and Button (will be initially displayed). Later when i click on the Button i need to display the age label and textbox. How can i do this ?
<ol>
<li>
<asp:Label runat="server" AssociatedControlID="Name">
User name
</asp:Label>
<asp:TextBox runat="server" ID="Name" Width="167px" />
<asp:Button ID="Button1" runat="server" Text="Button" />
</li>
<li>
<asp:Label runat="server" AssociatedControlID="age">age</asp:Label>
<asp:TextBox runat="server" ID="age" TextMode="age" Width="240px" />
</li>
</ol>
code for button press
protected void Button1_Click(object sender, EventArgs e)
{
}
You could set the label/textbox Visible property to True in server side. Alternatively, you could use JavaScript to avoid post backs to the server.
Add OnClientClick to your button :
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="ShowLabel();"/>
and declare the JavaScript function on page:
<script type="text/javascript">
function ShowLabel() {
// Note that the client ID might be different from the server side ID
document.getElementById('lblAge').style.display = 'inherit';
}
</script>
You need to set the Label Display style to none initially.
<asp:Label ID="lblAge" style="display: none;" runat="server" AssociatedControlID="age">age</asp:Label>
Try below code:
You need to set Visible property of controls to True or False according to your requirement. By default, all controls are visible on the screen whenever they are added on the page.You need to do following thing:
You need to remove TextMode="age" as there is not any supported textmode of type age.
Need to define id of control if you want to access a control server side in code behind. So define the ID of Label that you put corresponding to Age textbox.
By Default age label and textbox will not be visible by using below code:
<asp:Label ID="lblAge" runat="server" AssociatedControlID="age" Visible="false">age</asp:Label>
<asp:TextBox runat="server" ID="age" Width="240px" Visible="false"/>
Code behind:
After button click age label and the textbox will be visible by using below code:
protected void Button1_Click(object sender, EventArgs e)
{
lblAge.Visible = true;
age.Visible = true;
}
First add id to elements and set visible false
<asp:Label runat="server" AssociatedControlID="age" Visible="false" Id="lbl1">age</asp:Label>
<asp:TextBox runat="server" ID="age" TextMode="age" Width="240px" Visible="false" />
button click event set visible true
protected void Button1_Click(object sender, EventArgs e)
{
lbl1.Visible = True;
age.Visible = True;
}
this is the basic concept of asp.net. you can use visible property of the control.
your TextMode enumeration is wrong. there is no Age enumeration for Textbox.TextMode TextMode
<li>
<asp:Label runat="server" AssociatedControlID="age" id="lblAge">age</asp:Label>
<asp:TextBox runat="server" ID="age" TextMode="age" Width="240px" />
</li>
in code behind
protected void Button1_Click(object sender, EventArgs e)
{
lblAge.Visible=true;
age.Visible=true;
}
protected void Page_Load(object sender, EventArgs e)
{
NameLabel.Visible = false;
NameBox.Visible = false;
}
protected void Button1_Click(object sender, EventArgs e)
{
NameLabel.Visible = true;
NameBox.Visible = true;
}
i have an image button in datalist to open a new page to send mail, but when i click on it for the first time , it doesn't work , it works when click for the second time , i use update panel inside datalist . here's the code
<asp:UpdatePanel ID="up1" runat="server">
<ContentTemplate>
<asp:ImageButton ID="ImgBtnMail" runat="server" ImageUrl="../../../../Icons/Send-Email.jpg"
Width="22" Height="18" CommandName="DoMail"
CommandArgument='<%#DataBinder.Eval(((System.Web.UI.WebControls.DataListItem)(Container)).DataItem, "AdID")%>' />
</ContentTemplate>
</asp:UpdatePanel>
and the code behind :
protected void DLAds_ItemCommand(object source, DataListCommandEventArgs e)
{
if(e.CommandName=="DoMail")
{
string TargetPage = "window.open('SendMail.aspx?',null,'height=150, width=150,left=500 ,top=300 ,status= no, resizable= no, scrollbars=no, toolbar=no,location=no,menubar=no ');";
ImageButton MailBtn = (ImageButton)e.Item.FindControl("ImgBtnMail");
MailBtn.Attributes.Add("onclick", TargetPage);
}
}
With the example code you've posted, you'd be better off assigning the OnCommand event handler directly to the button.
<asp:ImageButton ID="ImageButton1" runat="server" OnCommand="ImageButton1_Command" ...>
And in your code:
protected void ImageButton1_Command(object sender, EventArgs e)
{
string targetPage = "..."
((ImageButton)sender).Attributes.Add("onclick", targetPage);
}
Set UpdateMode="Conditional" in the UpdatePanel and Update your UpdatePanel in the CodeBehind like below:
<asp:UpdatePanel ID="up1" runat="server" UpdateMode="Conditional">
protected void ImageButton1_Command(object sender, EventArgs e)
{
up1.Update();
string targetPage = "..."
((ImageButton)sender).Attributes.Add("onclick", targetPage);
}