Add a single control to multiple placeholders - asp.net-placeholder

There are two placeholders in my page.aspx:
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
// Other tags
<asp:PlaceHolder ID="PlaceHolder2" runat="server"></asp:PlaceHolder>
I've created one HtmlGenericControl in page.aspx.cs and want to add it in both PlaceHolders:
HtmlGenericControl NewControl = new HtmlGenericControl("div");
NewControl.ID = "newDIV";
NewControl.Attributes.Add("class", "myClass");
NewControl.InnerHtml = "**myContent**";
PlaceHolder1.Controls.Add(NewControl);
PlaceHolder2.Controls.Add(NewControl);
The problem is that just the last Add takes effect !
The Line
PlaceHolder1.Controls.Add(NewControl);
does not work !
Am I wrong ?
Thanks in advance.

A control cannot be a child of more than 1 parent control. You must create your HtmlGenericControl twice:
Func<HtmlGenericControl> createControl = () => {
HtmlGenericControl newControl = new HtmlGenericControl("div");
newControl.ID = "newDIV";
newControl.Attributes.Add("class", "myClass");
newControl.InnerHtml = "**myContent**";
return newControl;
};
PlaceHolder1.Controls.Add( createControl() );
PlaceHolder2.Controls.Add( createControl() );

Related

nativescript - how set global variables

I'm starting with nativescript with latest version.
I've finished tutorial on offical page, but now i have more questions than answers.
Can anybody tell me, what strategies can i use to set some variables, for example after succesfull login, how to set variable or even better, run some function that is doing checks globally, and not on every view or model file ?
I see that app.js is starting point for app, but looks like it cannot do any global checks ?
I think, second question is related :
Almost every model view file (file called as {viewname}).js is using:
var frameModule = require('ui/frame');
Is there a way to declare this once ? Or i have to run this every time when i need frame methods ? Or maybe if this is possible, lead to poor perforance?
Thanks for any advices.
NativeScript has a global Variable Scope.
To use it add global. in front of a variable name:
global.userName = "usernameXYZ";
To call it:
console.log("Username= "+global.userName);
Thanks Nikolay. Im using this pattern (maybe will help somebody)
register JS:
var page = require ("ui/core/view");
var frameModule = require('ui/frame');
var appSettings = require('application-settings');
exports.loaded = function(args) {
page = args.object;
};
exports.register = function() {
var userName = page.getViewById('userName');
var userPass = page.getViewById('userPass');
if (userName.text != '' && userPass.text != '') {
var name = userName.text;
var pass = userPass.text;
appSettings.setString('name', name);
appSettings.setString('pass', pass);
appSettings.setBoolean('auth', true);
var topmost = frameModule.topmost();
var navigationEntry = {
moduleName: "main-page",
context: { info: "something you want to pass to your page" },
animated: true,
transition: "curlDown",
clearHistory: true
};
topmost.navigate(navigationEntry);
} else {
alert('Please, fill form');
}
};
i use model as a global like this, in model.js (or whatever you want to call it)
`
var viewModel=require("data/observable");
var model= new viewModel.Observable();
module.exports= model;`
Then say in your login.js
` const model=require('./model')
const password = //a password from page
const userName = //a name from page
//after some logic to verify credentials set them accordingly as below
model.password=password
model.userName=userName
`
now if say you are navigated to home.js or any page for that matter, just call it whenever required like so const pass=model.password,name=model.userName all with courtesy from 'model.js'

how to update observable collection group

I have implemented group observable collection like below.
Grouped Property
private ObservableCollection<Grouping<String, Request>> _groupedList = null;
public ObservableCollection<Grouping<String, Request>> GroupedList {
get {
return _groupedList;
}
set {
_groupedList = value;
RaisePropertyChanged(() => GroupedList);
}
}
Creating List
var list = new List<Request>();
var grouped = from Model in list
group Model by Model.Done into Group
select new Grouping<string, Request>(Group.Key, Group);
GroupedList = new ObservableCollection<Grouping<string, TModel>>(grouped);
Now i need to update one item in the list without reloading full list for performance.
i did tried like this , mylist.FirstOrDefault(i => i.Id== mymodel.Id); Not worked for me.
I need to pick that particular item and edit and update into the list again using linq or something but i stuck here for group observable collection no efficient details to do this., anybody having idea about this help please.
And finally i get updated single item, but i need to do that without
GroupedList = new ObservableCollection<Grouping<string, TModel>>(grouped);
Because everytime it create new list and bind into my view.Thats again big performance though.
Thanks in advance.
What I understand from your question is that you want to push an updated group without overwriting the entire ObservableCollection.
To do that:
var targetGroup = GroupedList.FirstOrDefault(i => i.Id == mymodel.Id);
var targetIndex = GroupedList.IndexOf(targetGroup);
var modifiedGroup = ... //Do whatever you want to do
GroupedList[targetIndex] = modifiedGroup;
This will trigger a 'replace' operation of the target grouping.

Dynamic menu configuration section with conditional inputs on Magento custom module

I've followed this tutorial to create a custom dynamic backend configuration with serialized data, and everything is working as expected. yay
But now I want to take another step and only show some inputs when a specific value is selected in a select box. I know that I can use when doing this with system.xml, but how can I accomplish the same thing via code with dynamics serialized tables?
I ended up doing some kind of Javascript workaround to enable/disable a certain input.
function togleSelect(element)
{
var val = element.value;
var name = element.name;
if (val == 0) // select value to be triggered
{
name = name.substr(0, name.lastIndexOf("[")) + "[name_of_my_input]";
var target = document.getElementsByName(name);
target[0].disabled = false;
}
else
{
name = name.substr(0, name.lastIndexOf("[")) + "[name_of_my_input]";
var target = document.getElementsByName(name);
target[0].disabled = true;
}
}
It's not the best solution but it's working.

Yii2 call 2 functions on change of a dropdown

I have several dropdown imputs and on change of any of them I wanna a value to be offered for a textbox.
I have a code that i placed on each of my dropdown's and it works perfect for me:
<?= $form->field($model4, 'prevoditelj')->dropDownList(ArrayHelper::map(
\app\models\Prevoditelj::find()->orderBy('idprevoditelj')->asArray()->all(),
'idprevoditelj',
'naziv'
),['onchange'=>'
$.get( "'.Url::base().'/index.php?r=zadatak/trosak&id='.$model->projekt.'_"+$("#'.Html::getInputId($model3, 'usluga').'").val()+"_"+$("#'.Html::getInputId($model3, 'dodatak').'").val()+"_"+$("#'.Html::getInputId($model3, 'obr_jedinica').'").val()+"_"+$("#'.Html::getInputId($model4, 'prevoditelj').'").val(), function( data ) {
$( "#'.Html::getInputId($model, 'trosak').'" ).val( data );
});
']) ?>
in a controller I have:
public function actionCijena($id){
$sve=explode("_",$id);//0 - projekt_id, 1 - usluga, 2 - dodatak/jez_kombinacija, 3 - obr_jedinica
$projekt = Projekt::findone($sve[0]);
$klijent = Klijent::findone($projekt['klijent']);
$cjenik_klijent = CjenikKlijent::find()
->asArray()
->where('klijent = :id and usluga = :usluga_id and obr_jedinica = :obr_jedinica and jez_kombinacija = :jez_kombinacija and valuta = :valuta',
['id'=>$klijent['idklijent'],'usluga_id'=>$sve[1],'obr_jedinica'=>$sve[3],'jez_kombinacija'=>$sve[2],'valuta'=>$klijent['valuta']])
->all();
//ako nema, gledaj opci cjenik
if($cjenik_klijent==array()){
$cjenik_klijent = CjenikOpci::find()
->asArray()
->where('usluga = :usluga_id and obr_jedinica = :obr_jedinica and jez_kombinacija = :jez_kombinacija and valuta = :valuta',
['usluga_id'=>$sve[1],'obr_jedinica'=>$sve[3],'jez_kombinacija'=>$sve[2],'valuta'=>$klijent['valuta']])
->all();
}
return $cjenik_klijent[0]['cijena'];
}
The problem I have is that now I wanna add another calculated value to another textbox. but it needs to trigger on the same dropdown's. Unfortunately Get can't return an array so I need to split it up into 2 functions, but how do I call 2 of them?
Ok, I managed to find a solution in returning 2 values in 1 string and then exploding them in jquery. Don't know why I didn't think of that sooner.

What's problem on linq databinding

<dx:ASPxGridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="False"
KeyFieldName="CategoryID">
<SettingsEditing Mode="Inline" />
<Columns>
<dx:GridViewCommandColumn VisibleIndex="0">
<EditButton Visible="True"></EditButton>
<NewButton Visible="True"></NewButton>
<DeleteButton Visible="True"></DeleteButton>
</dx:GridViewCommandColumn>
<dx:GridViewDataTextColumn Caption="CategoryID" FieldName="CategoryID"
VisibleIndex="1">
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn Caption="CategoryName" FieldName="CategoryName"
VisibleIndex="2">
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn Caption="Description" FieldName="Description"
VisibleIndex="3">
</dx:GridViewDataTextColumn>
</Columns>
</dx:ASPxGridView>
C# syntax:
NorthwindDataContext db = new NorthwindDataContext();
var lresult = (db.Categories
.Select(p => new { p.CategoryID, p.CategoryName, p.Description}));
ASPxGridView1.DataSource = lresult;
ASPxGridView1.DataBind();
If you run the code, you get a gridview which is filled by NorthWind Categories table. If you click on command button of grid whose are on left side, you get insert/update field, but you have not access to give input. They are gone to read only mode.
If I replace the above C# syntax with below
NorthwindDataContext db = new NorthwindDataContext();
var lresult = (db.Categories);
ASPxGridView1.DataSource = lresult;
ASPxGridView1.DataBind();
then it works fine. Now you can work with command button with out facing any problem.
I want to know what the problem is, why the first syntax does not work. Maybe you say
Anonymous types are class types that consist of one or more public read-only properties. But when you need to join more than one table and need to select several fields not all than what you do. Hope you not say linq is fail to do that or Don't think it is possible. Hope there must be any technique or else something to bind control with Anonymous type. Plz show some syntax .
The problem is that the result set is collection of Anonymous type as you supposed and the grid doesn't know how to treat it. What you have to do is to use RowInserting and RowUpdating events of the grid.
Here is an example of how I use DevExpress grid with NHibernate:
protected void gridAgentGroups_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
{
ASPxGridView currentGrid = sender as ASPxGridView;
var currentAgentGroup = new AgentGroup();
if (e.NewValues.Contains("Name"))
{
var newValue = (string)e.NewValues["Name"];
currentAgentGroup.Name = newValue;
}
if (e.NewValues.Contains("PhysicalAddress"))
{
var newValue = (string)e.NewValues["PhysicalAddress"];
currentAgentGroup.PhysicalAddress = newValue;
}
AgentGroupsDataAccess.SaveAgentGroup(currentAgentGroup);
e.Cancel = true;
currentGrid.CancelEdit();
currentGrid.DataBind();
}
protected void gridAgentGroups_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
{
ASPxGridView currentGrid = sender as ASPxGridView;
int currentAgentGroupId = (int)((AgentGroup)currentGrid.GetRow(currentGrid.EditingRowVisibleIndex)).Id;
var currentAgentGroup = AgentGroups.Where(ag => ag.Id == currentAgentGroupId).FirstOrDefault();
if (e.NewValues.Contains("Name"))
{
var newValue = (string)e.NewValues["Name"];
currentAgentGroup.Name = newValue;
}
if (e.NewValues.Contains("PhysicalAddress"))
{
var newValue = (string)e.NewValues["PhysicalAddress"];
currentAgentGroup.PhysicalAddress = newValue;
}
AgentGroupsDataAccess.SaveAgentGroup(currentAgentGroup);
e.Cancel = true;
currentGrid.CancelEdit();
currentGrid.DataBind();
}
I hope this will help.
Just a wild guess - you're binding your data to the grid using field names - yet, your anonymous type doesn't really have any field names.
Does it make any difference if you try this code:
NorthwindDataContext db = new NorthwindDataContext();
var lresult = (db.Categories
.Select(p => new { CategoryID = p.CategoryID,
CategoryName = p.CategoryName,
Description = p.Description}));
ASPxGridView1.DataSource = lresult;
ASPxGridView1.DataBind();
Again - I don't have the means to test this right now, it's just a gut feeling..... try it - does that help at all??
You actually can bind with anonymous type as you see the already filled rows. But: the grid itself cannot know how you build the query and what to add additionally to the visible columns (if there are valid default values).
As you use Developer Express' grid you have the option to provide your own update / edit form and handle everything needed on your own.

Resources