Select many CheckBox on GridPanel Ext.Net (ASP.NET) - asp.net

How to select Rows on GridPanel using ext.net Library on ASP.NET.
When i put this code on Page_Load event, it's work fine :
protected void Page_Load(object sender, EventArgs e)
{
RowSelectionModel sm = gridRolesPermission.GetSelectionModel() as RowSelectionModel;
sm.SelectedRows.Add(new SelectedRow(1));
sm.SelectedRows.Add(new SelectedRow(2));
}
but when i would to select CheckBoxs on Ajax event, it's not working !
[DirectMethod]
public void FillPermissionForSelectedRole()
{
RowSelectionModel sm = gridRolesPermission.GetSelectionModel() as RowSelectionModel;
sm.SelectedRows.Add(new SelectedRow(1));
sm.SelectedRows.Add(new SelectedRow(2));
}
On view :
....
<Listeners>
<Command Handler="App.direct.FillPermissionForSelectedRole();" />
</Listeners>
...
Any Help Please !

In your example,when u click the button(directMethod button),a pamatercollection sent to the server called CheckboxSelectionModel1 .on server side, u can get the this parameter collection like this.
string hh= HttpContext.Current.Request["CheckboxSelectionModel1"];
*CheckboxSelectionModel1 is the model name of the grid on your example.
in your case like that
[DirectMethod]
public void FillPermissionForSelectedRole()
{
string hh= HttpContext.Current.Request["CheckboxSelectionModel1"];
}
string hh should be something like this depents on the what u checked on grid.
"[{"RecordID":5,"RowIndex":5},{"RecordID":7,"RowIndex":7},{"RecordID":3,"RowIndex":3}]"
later u can desrialize this string and get the RecordID something like this
var rw = JSON.Deserialize<Row[]>(hh);
string txt = "";
foreach (var item in rw)
{
txt += item.RecordID;
}
and the row class
public class Row{
public string RecordID { get; set; }
public string RowIndex { get; set; }
}

to resolve this probleme i should add this line to upadate modifications :
sm.UpdateSelection();

Related

Select ID from Picker

I am getting values in picker from an API, text and Value are two fields as I declared ! I am able to see values in it ! I want that whenever I select item from It, I am able to fetch respective value of that field !
async void CallInspectionMaster()
{
string Url = "192.168.xx.xx/api/QMSin/GetInspectionMasterList";
var data = await Url.GetJsonAsync<List<MyClass>>();
InspectionMasterPicker.ItemsSource = data;
InspectionMasterPicker.ItemDisplayBinding = new Binding("Text");
Binding selecteditemx = new Binding("InspectionMaster");
selecteditemx.Mode = BindingMode.TwoWay;
selecteditemx.Source = InspectionMasterPicker;
}
public class MyClass
{
public string Value { get; set; }
public string Text { get; set; }
}
I have to display text but fetch value so I can pass it other functions ! How do do that ?
assign a handler to the SelectedIndexChanged event
InspectionMasterPicker.SelectedIndexChanged += PickerSelect;
then create the handler
protected void PickerSelect(object sender, EventArgs args)
{
var item = (MyClass)InspectionMasterPicker.SelectedItem;
...
}
there is a complete example included in the docs

DataBinding a DateTimePicker raises "DataBinding cannot find a row in the list that is suitable for all bindings."

I have a simple test application which reproduces an error I encountered recently. Basically I have a simple WinForm with databound TextBox and DateTimePicker controls, and a button. When I execute the code below (on the button click), I get the error "DataBinding cannot find a row in the list that is suitable for all bindings". If I move the DataSource assignment into the form's constructor, I don't get the error.
If I remove the data binding for the DateTimePicker, it works fine.
Can anyone explain what the problem is ?
public partial class Form1 : Form
{
private BindingSource bs;
public Form1()
{
InitializeComponent();
button1.Click += new EventHandler(button1_Click);
bs = new BindingSource();
bs.DataSource = typeof(Thing);
this.textBox1.DataBindings.Add("Text", bs, "MyString");
this.dateTimePicker1.DataBindings.Add(new Binding("Value", bs, "MyDate"));
//Thing thing = new Thing { MyString = "Hello", MyNumber = 123, MyDate = DateTime.Parse("01-Jan-1970") };
//bs.DataSource = thing;
}
private void button1_Click(object sender, EventArgs e)
{
Thing thing = new Thing { MyString = "Hello", MyNumber = 123, MyDate = DateTime.Parse("01-Jan-1970") };
bs.DataSource = thing;
}
}
public partial class Thing
{
public String MyString { get; set; }
public Int32 MyNumber { get; set; }
public DateTime MyDate { get; set; }
}
}
Thanks
Edit:
It seems that if I change the data binding for the DateTimePicker control such that I bind to the "Text" property, the problem goes away. I don't understand why that would be though, because "Value" is valid for data binding.

How to implement Generics in business object class definition with DAL to create a proper user control dropdown

I am woefully new to generics, being tied to the support of a corporate intranet web application whose upgrade process is bound to red tape and slowwwly-changing standards. Consequently, today (thankfully!) I finally find myself scrambling during our upgrade to .Net 3.5 and transitioning all the code I can to a properly tiered model.
I have been reading all morning about generics trying to digest how to transition dropdown user controls into a proper business object that gets its data from a class in the data access layer.
There is a perfectly succinct question here that details exactly what I am interested in exploring: Set selected index in a Dropdownlist in usercontrol.
What I would love to see, however, is what Travel_CarSizes.GetCarSizes() actually looks like inside and how the class Travel_CarSizes is defined. (I am having a hard time with <T> and knowing where it should occur.)
For my own specific circumstance at the moment I need a dropdown user control to contain location directionals (N, S, W, C/O, NW, SE, etc) that are stored in a SQL table in the DB and whose selected index needs to be able to be set by whichever page it happens to be in, when form data exists.
I have begun to implement the model in the example from the link above but right now without using Generics because I can't figure it out:
The dropdown user control:
public partial class DropDownStreetPrefix : System.Web.UI.UserControl
{
public string StreetPrefixValue
{
get { return ddlStreetPrefix.SelectedValue.ToString(); }
set
{
Bind();
ddlStreetPrefix.SelectedIndex = ddlStreetPrefix.Items.IndexOf(ddlStreetPrefix.Items.FindByValue(value));
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Bind();
}
}
private void Bind()
{
if (ddlStreetPrefix.Items.Count == 0)
{
SqlDataReader rdr = StreetDirectionals.GetDirectionals();
ddlStreetPrefix.DataSource = rdr;
ddlStreetPrefix.DataBind();
ddlStreetPrefix.DataValueField = "StreetSuffixPrefixAbbr";
ddlStreetPrefix.DataTextField = "StreetSuffixPrefixAbbr";
ListItem li = new ListItem("", "");
ddlStreetPrefix.Items.Insert(0, li);
ddlStreetPrefix.SelectedIndex = 0;
}
}
}
The StreetDirectionals class:
public class StreetDirectionals
{
private StreetDirectionals () { }
public static SqlDataReader GetDirectionals ()
{
string sqlText = "SELECT StreetSuffixPrefixAbbr FROM common..tblStreetSuffixPrefix " +
"ORDER BY StreetSuffixPrefixAbbr";
SqlDataReader rdr = SqlClient.ExecuteFetchReturnDataReader( theConnectionString, CommandType.Text, sqlText);
return rdr;
}
}
I will separate out the database interaction inside the StreetDirectionals class as soon as I can figure out how to change its code if I were to transform the Bind() method from my dropdown user control into this:
private void Bind()
{
if (!IsPostBack)
{
**List<StreetDirectionals> sd = StreetDirectionals.GetDirectionals();**
ddlStreetPrefix.DataSource = sd;
ddlStreetPrefix.DataTextField = "StreetSuffixPrefixAbbr";
ddlStreetPrefix.DataValueField = "StreetSuffixPrefixAbbr";
ddlStreetPrefix.DataBind();
}
}
Any assistance would be sooo much appreciated!
public class StreetDirectional
{
public string StreetSuffixPrefixAbbr { get; set; }
public static IEnumerable<StreetDirectional> GetDirectionals ()
{
string sqlText = "SELECT StreetSuffixPrefixAbbr FROM common..tblStreetSuffixPrefix "
+ "ORDER BY StreetSuffixPrefixAbbr";
SqlDataReader rdr = SqlClient.ExecuteFetchReturnDataReader( theConnectionString, CommandType.Text, sqlText);
var list = new List<StreetDirectional>();
while (rdr.Read())
{
var item = new StreetDirectional() { StreetSuffixPrefixAbbr = (string)rdr["StreetSuffixPrefixAbbr"] };
list.Add(item);
}
return list;
}
}
then you can do this
ddlStreetPrefix.DataSource = StreetDirectional.GetDirectionals();

DevExpress LookUpEdit SelectedText Problem

I have some lookupedits binded to some lists where the user can choose values and then save in database. I use EditValueChanged events to handle the values. So far all good!
Now i need to grab the values from the database and assign them to the lookupedits. I don't use BindingSource for the whole object cause lookupedits are binded to independent lists.
As i supposed and read from the documentation, SelectedText is what i need, but when I'm assigning the string i want, it just don't work and sets an empty string. Same Behavior for the DateEdit control, I'm assigning a DateTime value and seems to have this value but doesn't shows it. I could set the EditValue property but i get nothing showed up in the LookUpEdit again.
How to force the LookUpEdit to show me the value i want, basically go to the row with the value i set and show the text in the editor too, or set the SelectedText and match it with its list and show it!
I guess this should be easier...Any help appreciated!
Example:
myLookUpEdit.SelectedText = "George" // The LookUpEdit is Binded to a List<Names> and has the name George.
Thank you
Whenever I am setting the value of a LookupEdit I use EditValue.
You need to make sure that you set the ValueMember property of the LookupEdit to whatever you want to appear in EditValue. DisplayMember will what is displayed when the LoodupEdit is closed. You can pass in a string to the name of the property you want in your object to these properties.
Setting the SelectedText value has the same effect as typing into the control as far as I am aware.
public partial class Form1 : Form
{
List<Name> MyNames = new List<Name>();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
MyNames.Add(new Name("John", "Smith"));
MyNames.Add(new Name("John", "Doe"));
MyNames.Add(new Name("Jane", "Doe"));
MyNames.Add(new Name("Jane", "Smith"));
lookUpEdit1.Properties.DataSource = MyNames;
lookUpEdit1.Properties.DisplayMember = "FirstName";
lookUpEdit1.Properties.ValueMember = "FirstName";
}
private void lookUpEdit1_EditValueChanged(object sender, EventArgs e)
{
string mystring = lookUpEdit1.EditValue.ToString();
lookUpEdit1.EditValue = mystring;
}
}
public class Name
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Name(string firstName, string lastName)
{
this.FirstName = firstName;
this.LastName = lastName;
}
}

Why am I losing object references on the postback?

I am developing an asp.net (3.5) application and I am puzzled with the behavior of the postbacks.
Consider the following scenario: I have a web user control that is basically a form. However each form field is a web user control in itself.
In the click event of the save button I iterate through all controls in my form and I retrieve the field value and the field name that refers to the database field that I am saving the value to.
The click event triggers a postback and it is during the postback that I visit the controls and here is the funny thing: the property value for the database field has become null! Could anyone shed a light here?
Here is some basic code:
[Serializable]
public partial class UserProfileForm : CustomIntranetWebappUserControl
{
protected override void OnInit(EventArgs e)
{
//AutoEventWireup is set to false
Load += Page_Load;
CancelLinkButton.Click += CancelButtonClickEvent;
SaveLinkButton.Click += SaveButtonClickEvent;
base.OnInit(e);
}
private void SaveButtonClickEvent(object sender, EventArgs e)
{
VisitFormFields();
}
private void VisitFormFields()
{
var userProfileVisitor = new UserProfileVisitor();
foreach (var control in Controls)
{
if (control is FormFieldUserControl)
{
var formField = (FormFieldUserControl) control;
formField.Visit(userProfileVisitor);
}
}
userProfileVisitor.Save();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindText();
}
}
private void BindText()
{
LastNameFormLine.LabelText = string.Format("{0}:", HomePage.Localize("Last Name"));
LastNameFormLine.InputValue = UserProfile.LastName;
LastNameFormLine.IsMandatoryField = true;
LastNameFormLine.IsMultilineField = false;
LastNameFormLine.ProfileField = "UserProfile.LastName";
//... the rest of this method is exactly like the 4 lines above.
}
}
[Serializable]
public abstract class FormFieldUserControl : CustomIntranetWebappUserControl
{
public string ProfileField { get; set; }
public abstract void Visit(UserProfileVisitor userProfileVisitor);
}
[Serializable]
public partial class FormLineTextBox : FormFieldUserControl
{
//... irrelevant code removed...
public override void Visit(UserProfileVisitor userProfileVisitor)
{
if (userProfileVisitor == null)
{
Log.Error("UserProfileVisitor not defined for the field: " + ProfileField);
return;
}
userProfileVisitor.Visit(this);
}
}
[Serializable]
public class UserProfileVisitor
{
public void Visit(FormLineTextBox formLine)
{
// The value of formLine.ProfileField is null!!!
Log.Debug(string.Format("Saving form field type {1} with profile field [{0}] and value {2}", formLine.ProfileField, formLine.GetType().Name, formLine.InputValue));
}
// ... removing irrelevant code...
public void Save()
{
Log.Debug("Triggering the save operation...");
}
}
Remember ASP.NET is stateless. Any properties created are destroyed after the page has been render to the browser. So you have to recreate objects on each post back or store them in View, Session, or Application State.
When you do a property you have to tell it to save the view state it does not do it automatically. Here is a sample of a view state property.
public string SomePropertyAsString
{
get
{
if (this.ViewState["SomePropertyAsString"] == null)
return string.Empty;
return (string)this.ViewState["SomePropertyAsString"];
}
set { this.ViewState["SomePropertyAsString"] = value; }
}
public MyCustomType ObjectProperty
{
get
{
if (this.ViewState["ObjectProperty"] == null)
return null;
return (MyCustomType)this.ViewState["ObjectProperty"];
}
set { this.ViewState["ObjectProperty"] = value; }
}
First guess would be that BindText() shouldn't be in Page_Load, but in Page_Init, so the control state will be saved.
#David Basarab, this is not true afaik, and was only the case in .Net 1.1, in .Net2 and up this is all handled by the framework if you do all the magic stuff in the Init.
Your problem is that 'ProfileField' isn't available on the Postback, right?
The solution is to store the value for that in ViewState (instead of an auto-implemented property). Without that, it won't be available on the postback.

Resources