ASP.NET Dynamical Creating Custom Control - asp.net

I created user control and want to add it to page. I use next code:
Controls_MultiTextInput cc = new Controls_MultiTextInput();
Controls.Add(cc);
But control doesn't appear on page. What is wrong?

this.Controls.Add(this.LoadControl("MultiTextInput.ascx")) is the correct way to load the control because it needs to know where the ascx file is.
If you want to set properties, do this:
Controls_MultiTextInput cc = (Controls_MultiTextInput) LoadControl("MultiTextInput.ascx");
cc.MyProperty = "abc";
Controls.Add(cc);

Anton you can set properties !
Controls_MultiTextInput cc
= (Controls_MultiTextInpu)Page.LoadControl("MultiTextInput.ascx");
cc.variable = 2;
cc.SetProperties(223,2311);
Controls.Add(cc);
or
PlaceHolder.Controls.Add(cc);

Related

Create own menu_button in ALV toolbar

I have to create a menu_button in my ALV toolbar,
I searched in SCN and I found some examples.
I know that we have to create a button with the btn_type = 2
like that :
CLEAR: ls_buttn.
ls_buttn-icon = gi_livra.
ls_buttn-butn_type = 2.
ls_buttn-text = 'Vue Livraison'.
ls_buttn-quickinfo = 'Vue Livraison'.
ls_buttn-function = 'LIVRAISON'.
APPEND ls_buttn TO e_object->mt_toolbar.
After that I have to handle menu button im my class
like that:
METHODS handle_menubtn FOR EVENT menu_button OF cl_gui_alv_grid
IMPORTING e_object e_ucomm,
After that to implement it
METHOD handle_menubtn.
IF e_ucomm = 'LIVRAISON'.
CALL METHOD e_object->add_function
EXPORTING
fcode = 'DISPLAY'
text = 'DISPLAY'.
ENDIF.
ENDMETHOD.
But it does not work. The debugger does not go into this method when I click on my button.
Why?
I think that what you did is right but maybe you forgot to
SET HANDLER lr_event->handle_menubtn FOR your_alv.
Regards,

findcontrol does not find dynamically added control which was just addes one line before

who can explain this to me?
CheckBox ckRequest = new CheckBox();
ckRequest.ID = "ckRequest";
ckRequest.DataBinding += new EventHandler(this.CkIsRequested_DataBinding);
container.Controls.Add(ckRequest);
Control con = container.FindControl("ckRequest");
Debugging shows that con is still null.
Debugging also shows me, that conteiner.Controls hase one Item with ID "ckRequest"
How can this be????
Many thanks for your answers.
Actually I try the following.
findcontrol does not find dynamically created control in rowUpdating eventhandler
It makes sense to me, that findcontrol works only on the created page.
At which point in time the visual tree of the page is created?
FindControl only works when the control is in the visual tree of the page
In your case you can try this
var checkBoxesInContainer = container.Controls.OfType<CheckBox>();
http://msdn.microsoft.com/en-us/library/bb360913.aspx
You can use the following:
Control con =
container.Controls.Cast<Control>().First(item => item.ID == "ckRequest");
You might want to try to following:
//GET THE CHECKBOXLIST
Control c = phCategories.Controls.Cast<Control>().First(item => item.ID == "cblCatID-" + catID && item.GetType().Name == "CheckBoxList");
if (c.GetType().Name == "CheckBoxList")
{
cbl = (CheckBoxList)c;
}
For some reason I needed to cast this as a control first. If I did not do it this way, I seemed to grab a label instead (which didn't make sense to me, because it wasn't actually grabbing a label). Hope it helps somebody.

How to display popup from code-behind in ASP.net?

I wonder how it would be possible to launch a series of popups, containing a form,
from code-behind.
I possess a list of objects 'Products'
and I wish I could change one property (quantity) of each "product".
Here's how I build my list (normally I use a database).
Private List<Product> listProduct;
listProduits = new List<Product>();
Product objProduit_1 = new Produit;
objProduct_1.ref = "001";
objProduct_1.article = "G900";
objProduct_1.quantity = 30;
listProducts.Add(objProduct_1);
ProductobjProduit_2 = new Product;
objProduct_2.ref = "002";
objProduct_2.article = "G900";
objProduct_2.quantity = 35;
listProduits.Add(objProduct_2);
And I would like displayed popup one after one.
Thank you in advance for your help
you'll need to write some client side code that produces what you're looking for. The AJAX Control tool kit may be along the lines of what you're looking for.

ASP.NET EnqityDataSource WhereParameters, creates new property

I am trying to populate GridView, using EntityDataSource(code behind), I need to able to sort GridView. However when I sort i get error:
A property with name 'aspnet_Users.UserId1' does not exist in metadata for entity type
So I beleive it is because I generate where parameter in code behind:
ActiveEnqDataSource.WhereParameters.Add(new SessionParameter("aspnet_Users.UserId", TypeCode.Object, "UserName"));
Full code is :
ActiveEnqDataSource.ConnectionString = db.Connection.ConnectionString;
ActiveEnqDataSource.DefaultContainerName = "Entities";
ActiveEnqDataSource.EntitySetName = "Enquiries";
ActiveEnqDataSource.Include = "UserCars.CarModel.CarMake, Category, aspnet_Users";
ActiveEnqDataSource.EnableUpdate = true;
ActiveEnqDataSource.EnableInsert = true;
ActiveEnqDataSource.EnableDelete = true;
ActiveEnqDataSource.AutoGenerateWhereClause = true;
ActiveEnqDataSource.WhereParameters.Add(new SessionParameter("aspnet_Users.UserId", TypeCode.Object, "UserName"));
Any suggestions? Thank you very much! The gridview itself renders perfectly, only thing I cannot sort it, any "whereParameters" I add, Add 1 to the property e.g UserId1,EnquiryStatus1, ProdauctName1. etc...
I got a similar error because I was adding the where clause each time the page was posted back. Dropping my code that generated the where clause inside an IsPostback statement fixed the problem:
if (!IsPostBack) {
// code to add where parameters
}
I got the same error when I used markup to define a Where parameter AND then I added the same parameter in code. Somewhere along the line, the 1 at the end of the parameter name was added.

I want to add items to an ASP.Net combobox using Javascript

I want to add an item to an ASP.Net combobox using Javascript. I can retrieve the ID (No Masterpage). How can I add values to the combobox from Javascript? My present code looks like this.
//Fill the years (counting 100 from the first)
function fillvarYear() {
var dt = $('#txtBDate').val();
dt = dt.toString().substring(6);
var ye = parseInt(dt);
//Loop and add the next 100 years from the birth year to the combo
for (var j = 1; j <= 100; j++) {
ye += 1; //Add one year to the year count
var opt = document.createElement("OPTION");
opt.text = ye;
opt.value = ye;
document.form1.ddlYear.add(opt);
}
}
To see the value on postback:
string selectedValue = Request.Params[combobox.UniqueId]
Remember, changing the values in a combobox with javascript will cause an Event Validation exception to be thrown, and is generally a bad idea, as you'll have to explicitly disabled event validation.
I'd recommend placing the combobox in an update panel, so you can read txtBirthDate on the server and generate the appropriate data. You then won't have to manually preserve state either.
Always remember, ASP.NET controls are nothing "fancy" - they always end up at some point becoming standard HTML elements.
Try checking out this site. It has a pretty nice demo and overview. Take note however that you are altering the data at the client side - this means you will need to do it on each and every request because the ViewState will not be updated.
TBH, you are probably better off just using a HTML control rather than ASP ComboBox..
Can I ask why you are changing items via Javascript? (out of curiosity) :)
I found a possible solution. I don't know why the earlier code didn't work for me, but the line below
document.form1.ddlYear.appendChild(new Option(ye, ye));

Resources