I have this weird problem when putting textboxes on the page in reverse. The whole event system is messed up. Changing one textbox fires TextChange on all textboxes. I can fix this by putting the controls in a list first and then call add while iterating trough the list in reverse. But i just want to know why this fails. Heres some code (.net 2.0)
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
InitFields();
}
private void InitFields()
{
int nrFields;
//We have a static textbox called nrElements, this determines the number
//of fields to initialize
if (int.TryParse(nrElements.Text, out nrFields))
{
//Put all the dynamic fields on the screen in reverse order
foreach(Control t in GetDynamicFields(nrFields))
{
//Calling Controls.Add works fine
//Calling Controls.AddAt messes up the events
//Try changing different textboxes
plhFields.Controls.AddAt(0, t);
}
}
}
private IEnumerable<Control> GetDynamicFields(int nrFields)
{
for (int i = 0; i < nrFields; i++)
{
TextBox txtBox = new TextBox();
txtBox.ID = string.Format("dynTextBox{0}", i.ToString());
txtBox.AutoPostBack = true;
txtBox.TextChanged += t_TextChanged;
yield return txtBox;
}
}
private void t_TextChanged(object sender, EventArgs e)
{
TextBox txtBox = sender as TextBox;
if (txtBox != null)
txtBox.Text = txtBox.Text + "Changed ";
}
}
Try calling InitFields() on the Page_PreInit event rather than Page_Load.
Or an alternative would be to override the CreateChildControls() method (MSDN Article), if you use CreateChildControls() you'll need to call EnsureChildControls() on the Page_Load method to make sure the CreateChildControls() method has been called before you try to access any controls which have been created within that method.
Always put dynamic controls in OnInit event. Then viewstate serializer/deserializer will work. And you have to add controls on every request, not just in !IsPostBack.
Related
I've got a program with a lot of text boxes that I've got text in that I want to be cleared on _click and then reset to default if nothing is entered and the user clicks away.
The way I was going to do it is clearly inefficient, having to name the text box each time and I'd like to know how I could go about streamlining it.
this is what I've got at the minute, and I'd have to change the txtUserName for the text box field name each time
private void txtUserName_Click(object sender, EventArgs e)
{
txtUserName.Text = ""
txtUserName.ForeColor = Color.Black;
}
is there a way I can do essentially
private void txtAnyTextBox_Click(object sender, EventArgs e)
{
string caller = //Get this textbox name
this.ClearBoxes(caller)
}
void ClearBoxes(string Caller)
{
Caller.txt.Text = "";
//..... and so on
}
Yes, you can try this (though it's not generic but there is no need for generics in this case):
private void txtAnyTextBox_Click(object sender, EventArgs e)
{
TextBox tb = sender as TextBox;
if(tb != null) tb.Text = "";
}
And you can attach this method to all your textBoxes Click event.
textBox1.Click += txtAnyTextBox_Click;
textBox2.Click += txtAnyTextBox_Click;
I don't think this is gonna work:
void ClearBoxes(string Caller)
{
Caller.txt.Text = "";
//..... and so on
}
If you want to use ClearBoxes method you should pass it your TextBox element.But there is no need for this,you can directly clear your textBox as shown above code.
Also if you want to clear all TextBoxes in the same time,for example one button click you can use this:
private void button1_Click(object sender, EventArgs e)
{
foreach (var tBox in this.Controls.OfType<TextBox>())
{
tBox.Text = "";
}
}
You can use the sender argument for that.
private void txtAnyTextBox_Click(object sender, EventArgs e)
{
var textbox = sender as TextBox;
this.ClearTextbox(textbox)
}
private void ClearTextbox(TextBox textbox)
{
textbox.Text = "";
//...
}
You can get name of textbox from sender of event:
private void txtAnyTextBox_Click(object sender, EventArgs e)
{
TextBox textBox = (TextBox)sender;
string caller = textBox.Name;
this.ClearBoxes(caller); // call your custom method
}
If you want to simply clear textbox text, then you don't need to get its name - you can use it's Clear() method:
private void txtAnyTextBox_Click(object sender, EventArgs e)
{
TextBox textBox = (TextBox)sender;
textBox.Clear();
}
Also you can consider creation of custom textbox, which will have some default value and will resent itself to default when clicked:
public class CustomTextBox : TextBox
{
public string DefaultText { get; set; }
protected override void OnClick(EventArgs e)
{
base.OnClick(e);
Text = DefaultText;
}
}
Use custom textboxes instead of default textboxes, and provide DefaultText value for each custom textbox which should reset itself to something more meaningful than empty string (you can use Properties window for that).
This would be quite nasty - as you'd cause a page reload every time someone clicked in the text box.
A far simpler way would be to do it in javascript.
just add a function to clear the text box, and then maybe use a css selector to enable the function for every text box you want to use it in.
e.g.
<input type="text" class="clearme" />
$(".clearme").click(function() {
$(this).val('');
});
this will do it all client side without causing any post backs.
Here is the complete code. I want to display a radiogroup when I select 1 in the dropdown list box. I get the error 'System.Web.HttpException: Control 'RadioButton1' of type 'RadioButton' must be placed inside a form tag with runat=server'.
namespace HostelRoomManagement
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
}
protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
{
if (DropDownList1.SelectedValue == "1")
{
RadioButton rb1 = new RadioButton();
rb1.ID = "RadioButton1";
rb1.Text = "C block";
rb1.GroupName = "BlockGroup";
RadioButton rb2 = new RadioButton();
rb2.ID = "RadioButton2";
rb2.Text = "C block";
rb2.GroupName = "BlockGroup";
Page.Controls.Add(rb1);
Page.Controls.Add(rb2);
}
}
protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
{
}
}
}
I get the error 'System.Web.HttpException: Control 'RadioButton1' of type 'RadioButton' must be placed inside a form tag with runat=server'
You can add the radio buttons you want the user to see based on his choice to the page and set the Visibility of them to false. Then, once the user choose a value change the visibility of the radio buttons you want to true. It might be easier.
I suspect your problem is that you are referencing to SelectedValue whereas you want to be refering to selectedindex.
Hope this helps.
You have created two radio button but where have you added them on the page?
Start by creating a place holder for you radio-button lists and add these controls over there.
The dynamically created control will be lost on the post back. This means you will have to manage you dynamically created controls.
Here is a good [example]: 1 ASP.NET dynamically created controls and Postback.
I have a web form which dynamically loads controls upon selection in combobox(devexpress). I have the following code on main form
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
if (Session["_active_control"] != null)//persist control on postbacks
{
Control cntrl = Session["_active_control"] as Control;
pnl_main.Controls.Clear();
pnl_main.Controls.Add(cntrl);
}
}
protected void cmb_control_SelectedIndexChanged(object sender, EventArgs e)
{
Control cntrl= Page.LoadControl("~/" + cmb_control.SelectedItem.Value);
pnl_main.Controls.Clear();
pnl_main.Controls.Add(cntrl);
Session["_active_control"] = cntrl;
}
also I have a user control having three Textboxes and a button having code
protected void btn_save_Click(object sender, EventArgs e)
{
lbl.Text = ASPxTextBox1.Text + "<br>" + ASPxTextBox2.Text + "<br>" + ASPxTextBox3.Text;
}
My problem is that the save button of user control is not firing if i load it dynamically (I have checked using breakpoints and also the code shown above. however it runs smoothly if I use it statically.(i.e. by dragging in design mode)
You are right that you have to persist the control across postbacks.
However the Page Load event is too late to add back your controls. Do this on the Init event of your page and you should be good. To receive a postback event, the control should be present when ProcessPostData(called before PreLoad) is called.
Also for textboxes you will want to receive the values entered by the user. This too happens on ProcessPostData, if you add you control after that, you will not receive the values entered by the user.
Refer: ASP.NET Page Life Cycle
hey i found the solution
instead on creating the controls in combobox_selectedindexchanged i put my control creation code on Pageload based in combobox.selectedindex i.e.
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (cmb_control.SelectedItem != null)
{
Control cntrl = Page.LoadControl("~/" + cmb_control.SelectedItem.Value);
cntrl.ID = "_new_ctrl" + cmb_control.SelectedItem.Value;
pnl_main.Controls.Clear();
pnl_main.Controls.Add(cntrl);
}
}
see Button click event not firing within use control in ASP .Net
I am trying to assign a ViewState value in my application with a SelectedIndexChanged function. Once it's assigned the postback will use the value to change some data and then set the value to zero but I can't seem to get it to work correctly. The controls are all created dynamically on Page_Load.
Page Load
protected void Page_Load(object sender, EventArgs e)
{
CreateAttributeControls();
TempProductVariantId = 0;
}
Create Attribute Controls
public void CreateAttributeControls()
{
...
var ddlArtistArtworks = new DropDownList();
ddlArtistArtworks.ID = "ddlArtistArtworksTest";
divAttribute.Controls.Add(ddlArtistArtworks);
ddlArtistArtworks.Items.Clear();
ddlArtistArtworks.SelectedIndexChanged += new EventHandler(ArtistArtwork_SelectedIndexChange);
ddlArtistArtworks.AutoPostBack = true;
...
}
ArtistArtwork_SelectedIndexChange
protected void ArtistArtwork_SelectedIndexChange(object sender, EventArgs e)
{
DropDownList ddl = sender as DropDownList;
TempProductVariantId = int.Parse(ddl.SelectedValue);
}
TempProductVariantId ViewState Save
public int TempProductVariantId
{
get
{
if (ViewState["TempProductVariantId"] == null)
return 0;
else
return (int)ViewState["TempProductVariantId"];
}
set
{
ViewState["TempProductVariantId"] = value;
}
}
When I load the page everything is fine. I change the DropDownList's value, It posts back, and the value is not set. Change it again the value is set and continues to change as I change the value of the DropDownList.
Any guidance on this would be greatly appreciated.
Note: I have tried changing when CreateAttributeControls() is called. In OnPreRender for example. I was given this to understand the lifecycle of the page Life Cycle
That's because you are essentially recreating the dropdown on every postback..
try this
public void CreateAttributeControls()
{
...
DropDownList ddlArtistArtworks;
if (!IsPostBack)
{
ddlArtistArtworks = new DropDownList();
ddlArtistArtworks.ID = "ddlArtistArtworksTest";
divAttribute.Controls.Add(ddlArtistArtworks);
ddlArtistArtworks.Items.Clear();
ddlArtistArtworks.AutoPostBack = true;
}
else
{
ddlArtistArtworks = (DropDownLise)divAttribute.FindControl("ddlArtistArtworksTest");
}
ddlArtistArtworks.SelectedIndexChanged += new EventHandler(ArtistArtwork_SelectedIndexChange);
...
}
For dynamically added controls, the event handler has to be linked up everytime so that has to be done outside the if-block, unconditionally.
Inside asp.net form I have few dynamically generated buttons, all of this buttons submit a form, is there a way to get which button was submit the form in page load event?
The sender argument to the handler contains a reference to the control which raised the event.
private void MyClickEventHandler(object sender, EventArgs e)
{
Button theButton = (Button)sender;
...
}
Edit: Wait, in the Load event? That's a little tricker. One thing I can think of is this: The Request's Form collection will contain a key/value for the submitting button, but not for the others. So you can do something like:
protected void Page_Load(object sender, EventArgs e)
{
Button theButton = null;
if (Request.Form.AllKeys.Contains("button1"))
theButton = button1;
else if (Request.Form.AllKeys.Contains("button2"))
theButton = button2;
...
}
Not very elegant, but you get the idea..
protected void Page_Load(object sender, EventArgs e) {
string id = "";
foreach (string key in Request.Params.AllKeys) {
if (!String.IsNullOrEmpty(Request.Params[key]) && Request.Params[key].Equals("Click"))
id = key;
}
if (!String.IsNullOrEmpty(id)) {
Control myControl = FindControl(id);
// Some code with myControl
}
}
This won't work if your code is inside a user control:
Request.Form.AllKeys.Contains("btnSave") ...
Instead you can try this:
if (Request.Form.AllKeys.Where(p => p.Contains("btnSave")).Count() > 0)
{
// btnSave was clicked, your logic here
}
You could try:
if (this.Page.Request.Form[this.btnSave.ClientID.Replace("_", "$")] != null) {
}
please try this code in page load event
string eventtriggeredCategory = Request.Form["ctl00$ContentPlaceHolder1$ddlCategory"];
if eventtriggeredCategory is returning any value its fired the event of ddlCategory
this is works fine for me
Thanks
Jidhu
Request.Form["__EVENTTARGET"] will give you the button that fired the postback
Use CommandArgument property to determine which button submits the form.
Edit : I just realized, you said you need this at PageLoad, this works only for Click server side event, not for PageLoad.