I have the following code:
public partial class queryTerm : System.Web.UI.UserControl
{
private static readonly List<string> BooleanOperators = new List<string> { ".", "AND", "AND NOT", "OR", "OR NOT" };
protected void BuildBoolPanel()
{
var parensOpen = _labelBoolean.Text;
foreach (var #operator in BooleanOperators)
{
if (parensOpen == #operator)
{
continue;
}
var linkButton = new LinkButton();
linkButton.Text = #operator;
linkButton.CommandArgument = #operator;
linkButton.CommandName = "parensOpen";
linkButton.Command += new CommandEventHandler(linkButton_Command);
_popupMenuParensOpen.Controls.Add(linkButton);
var literalLineBreak = new Literal();
literalLineBreak.Text = "<BR/>";
_popupMenuParensOpen.Controls.Add(literalLineBreak);
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
BuildBoolPanel();
}
void linkButton_Command(object sender, CommandEventArgs e)
{
_labelBoolean.Text = (string)e.CommandArgument;
BuildBoolPanel();
}
}
I have a panel(it's _popupMenuParensOpen) that is shown with the hoverextender whenever the cursor finds itself over a specific label in my user control.
This panel has all the boolean operators and '.' meaning not set.
I programatically add the boolean operators as a label in my panel, and I only add those that don't match what it is currently set to. For instance if my label is set to 'AND', when I hover over it, I display everything but 'AND'.
The problem is these never call linkButton_Command even though I instruct them to.
Weirder yet, if I remove the 'if (!this.IsPostBack) in page load, it will call it.
My control is inside an updatePanel.
The issue is that you're dynamically adding the controls during the Page_Load event. When a postback occurs those controls aren't going to exist and will need to be created every time. This is why when you remove your if (!Page.IsPostBack), and the controls are rebuilt, it works.
Since you're building this as a user control, you might want to look in to overriding the CreateChildControls method.
Related
I have two pages. first.aspx and second.aspx. In order to get all the control values from first.aspx, i added directive to second.aspx
<%# PreviousPageType VirtualPath="~/PR.aspx" %>
I have no problem to get all the previous page controls and set it to labels, but i have a big problem to save those values to a private variable, and reuse it after page load event is done. Here is code example. when i try to get the value from input in another method, it has nothing added. Why?
public partial class Second : System.Web.UI.Page
{
List<string> input = new List<string>();
protected void Page_Load(object sender, EventArgs e)
{
if (Page.PreviousPage != null&&PreviousPage.IsCrossPagePostBack == true)
{
TextBox SourceTextBox11 (TextBox)Page.PreviousPage.FindControl("TextBox11");
if (SourceTextBox11 != null)
{
Label1.Text = SourceTextBox11.Text;
input.Add(SourceTextBox11.Text);
}
}
}
protected void SubmitBT_Click(object sender, EventArgs e)
{
//do sth with input list<string>
//input has nothing in it here.
}
}
The SubmitBT_Click-click event happens in a postback. But all variables (and controls) are disposed at the end of the page's lifecycle. So you need a way to persist your List, e.g. in the ViewState or Session.
public List<String> Input
{
get
{
if (Session["Input"] == null)
{
Session["Input"] = new List<String>();
}
return (List<String>)Session["Input"];
}
set { Session["Input"] = value; }
}
Nine Options for Managing Persistent User State in Your ASP.NET Application
I'm extending a web control. I need to call server methods on every event fired by the control instead of javascript.
public partial class MyTextBox : RadTextBox, IScriptControl
{
public MyTextBox()
{
Attributes.Add("onBlur", "handleLostFocus();");
Attributes.Add("runat", "server");
}
public void handleLostFocus()
{
MyObject obj = new MyObject();
obj.someproperty = this.Text; //or somehow get the user entered text.
MyService1 service = new MyService1();
service.sendRequest(obj);
}
}
As I said in my comment, TextBox will post by default if AutoPostBack = "True", however, you need to handle your event. Supposing your TextBox is named TextBox1:
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
string str = TextBox1.Text;
}
Get rid of handleLostFocus() or have it be the handler for your TextBox control.
Good luck mate.
I have a page to do a heirarchical search, it starts with a dropdownlist and based on the value selected in the dropdown it will query the database and show the childs in another dropdown list and this continues as long as it hits the leaf... so I've first dropdown added dynamically and it has the event handler on SelectedIndexChanged, when I change the selected value, it triggers the postback but however not calling event handler method.. Not sure what i'm doing wrong here.. or is it a bug??
Using a session variable to keep track the created controls
private List<DynamicControlProperties> PersistedControls
{
get
{
if (_persistedControls == null)
{
if (Session[PersistedControlsKey] == null)
{
Session[PersistedControlsKey] = new List<DynamicControlProperties>();
}
_persistedControls = Session[PersistedControlsKey] as List<DynamicControlProperties>;
}
return _persistedControls;
}
}
And in Page Init, recreating the dynamically generated controls
protected override void LoadViewState(object savedState)
{
base.LoadViewState(savedState);
// regenerate the persisted controls
foreach (var prop in PersistedControls)
{
CreateControl(prop);
}
}
In page load, created the very first dropdown
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// create the control
CreateControl(....)
// bind the data to the dropdown
}
}
In create control method, just creating a label and a dropdown wrap it inside a and adding it to place holder
private DropDownList CreateControl(DynamicControlProperties dynamiccntrlprop)
{
// create a new HTML row
HtmlGenericControlWithParentID tr = new HtmlGenericControlWithParentID("tr");
HtmlGenericControlWithParentID td1 = new HtmlGenericControlWithParentID("td");
HtmlGenericControlWithParentID td2 = new HtmlGenericControlWithParentID("td");
// make sure we set the id and parentid
tr.ID = string.Format("tr{0}", dynamiccntrlprop.ID);
tr.ParentID = dynamiccntrlprop.ParentID;
tr.EnableViewState = true;
// create a new label for dropdown
Label lbl = new Label() { ID = string.Format("lbl{0}", dynamiccntrlprop.DisplayName), Text = dynamiccntrlprop.DisplayName };
// create a new dropdown list
DropDownList ddl = new DropDownList()
{
ID = string.Format("ddl{0}", dynamiccntrlprop.DisplayName),
// set the postback
AutoPostBack = true,
EnableViewState = true
};
// subscribe for the select index changed event
ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);
// add the controls to table row
td1.Controls.Add(lbl);
td2.Controls.Add(ddl);
tr.Controls.Add(td1);
tr.Controls.Add(td2);
// add the control to place holder
this.filtersPlaceHolder.Controls.Add(tr);
return ddl;
}
Here is the index changed handler,
protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
}
Enabled the viewstate,autopostback blah blah blah... recreated the controls with same id in post back.. tried all the answers in google.. but NO luck.. It does trigger the postback when i changed the index but not calling the event handler method..
Any ideas, please???
Many Thanks,
K
You have to make sure that The CreateControl method is called on each and every page postback. This needs to happen to ensure that the dynamic control's event handler is hooked up after the postback.
protected void Page_Load(object sender, EventArgs e)
{
// you shouldn't wrap the call to CreateControl in this 'if' statement
//if (!Page.IsPostBack)
//{
// create the control
CreateControl(....)
// bind the data to the dropdown
//}
}
once you do this, the selected index changed event will fire.
Maybe It is beacuse new value of the dropdownlist don't be loaded.
protected override void LoadViewState(object savedState)
{
// regenerate the persisted controls
foreach (var prop in PersistedControls)
{
CreateControl(prop);
}
base.LoadViewState(savedState);
}
I have a problem that I have been struggling with for some time, and it is regarding transfering values from one control to another.
Basically I have two .ascx controls. On control1: I have an email textbox called txtEmail. The txtEmail is used to save the email in the SQL table, and on update button click, I load Control2 that has a email textbox as well. I need the emailtext box from control1 to be available on email textbox on control2.
I have tried all kinds of different ways but to no avail. I even tried using delegates and events but I can't make it work.
Does anyone know how I can do this.
Regards
Please find below the code:
public event EventHandler Notify;
public string Email
{
get { return txtEmail.Text; }
set {Email= value ; }
}
//button that will handle the update
protected void btnUpdateDB_Click(object sender, EventArgs e)
{
var email = txtEmail.Text.ToString();
public BaseClass.BAL.MBAL m = new BaseClass.BAL.MBAL();
var s = new BaseClass.Controllers.m();
s.email=email;
if(m.save(s)!=0) txtMsave.Text="Saved....";
}
//second control
public void notifyEmailChange(object sender,EventArgs e)
{
txtUsername.Text = member1.Email;
}
protected void Page_Load(object sender, EventArgs e)
{
if(Page.IsPostBack)
{
member1.Notify += new EventHandler(notifyEmailChange);
}
}
public string email {
set { txtUsers.Text = value; }}
Maybe I am trivializing the problem, but if you are wanting to be able to read/write to the text box on each of the custom controls, just make a public property that reads and writes to the textbox on each of the two controls.
public string EmailAddress {
get {
return txtEmailAddress.Text;
}
set {
txtEmailAddress.Text = value;
}
}
Now the page that contains the two controls can read the email address from the first control and write it into the email address text box in the second control.
If I am misunderstanding the problem, let me know.
The way that I have done this in the past is to have
UserControl1 have a custom event called (for instance) Notify.
The containing control wires Notify to an EventHandler
When notify fires (on the update) the consuming event handler fires and this event handler updates the email on UserControl2
Might seem overengineered but because UserControl2 can't "see" UserControl1 I think this is the way to go
Example
In UserControl1
public event EventHandler Notify;
and within the update button click event handler
if(Notify != null)
{
Notify(this, new EventArgs());
}
In parent control
in Page_Load
ucUserControl2.Notify += new EventHandler(NotifyUserControl);
and to set the message
protected void NotifyUserControl(object sender, EventArgs args)
{
ucUserControl2.Email = ucUserControl1.Email;
}
You obviously need public properties in UserControls to expose the Email text
I have a code snippet like below and I want to add imagebuttons into my asp:Panel during page load. But the events are firing already when I run the page. I want it to be fired when it is clicked.
Thanks in advance for all helps
protected void Page_Load(object sender, EventArgs e)
{...
foreach (Gift g in bonusGifts)
{
ImageButton ib = new ImageButton();
ib.ImageUrl = g.GiftBanner;
ib.ID = g.GiftID.ToString();
ib.Click += Purchase(g);
BonusGiftPanel.Controls.Add(ib);
}
}
private ImageClickEventHandler Purchase(Gift g)
{
_giftRep.Purchase(g, _userSession.CurrentUser);
lblGifts.Text = "You have purcased " + g.GiftName + " for " + g.BonusPoints;
return null;
}
Add controls in your Page_Init, not in your Page_Load. [1]
Furthermore, you are not doing this the way it should. Consider this code
//your collection of objects goes here. It might be something different than
//this, but basically a Dictionary<int, YourType> goes fine
public Dictionary<Int32, string> Ids
{
get { return (ViewState["ids"] ?? new Dictionary<Int32, string>()) as Dictionary<Int32, string>; }
set { ViewState["ids"] = new Dictionary<Int32, string>(); }
}
protected void Page_Init(object sender, EventArgs e)
{
//load the data using your DAO
Ids = new Dictionary<int, string>();
Ids.Add(1, "http://www.huddletogether.com/projects/lightbox2/images/image-2.jpg");
Ids.Add(2, "http://helios.gsfc.nasa.gov/image_euv_press.jpg");
foreach (var item in Ids)
{
ImageButton imb = new ImageButton()
{
ImageUrl = item.Value,
CommandArgument = item.Key.ToString(),
CommandName = "open"
};
imb.Click += new ImageClickEventHandler(imb_Click);
PH1.Controls.Add(imb);
}
}
void imb_Click(object sender, ImageClickEventArgs e)
{
Response.Write("You purchased " + Ids[int.Parse(((ImageButton)sender).CommandArgument)]);
}
[1] (CTRL+C/CTRL+V from some other question I answered last week):
Everything that has to be maintained between page cycles should be declared in Page_Init, not Page_Load.
All the initialization, like adding event handlers, and adding controls should be added during initialization, as the state is saved between page cycles. Handling with the content of controls and the viewstate, should be done in Load.
Check also http://msdn.microsoft.com/en-us/library/ms178472.aspx.
Init
Raised after all controls have been initialized and any skin
settings have been applied. Use this
event to read or initialize control
properties.
.
Load
The Page calls the OnLoad event method
on the Page, then recursively does the
same for each child control, which
does the same for each of its child
controls until the page and all
controls are loaded.
Use the OnLoad event method to set
properties in controls and establish
database connections.
You should add the controls in the Page Init event, as other have said.
Your image click event handler does not conform to the ImageButton Click event handler signature. That should look something like this:
private void ImageButton_Click(ByVal sender As Object, ByVal e As ImageClickEventArgs)
{
}
Note that you can't pass your "Gift" object directly to the ImageButton_Click. You will have to find another method of doing that.
You need to take a look at the Page Lifecycle - http://msdn.microsoft.com/en-us/library/ms178472.aspx