Unable to get selected item of dx:ASPxListBox on ASPxGridView_Updating event of devexpress control - devexpress

I have used dx:ASPxGridView where I have one dx:GridViewDataTextColumn with EditItemTemplate which contain dx:ASPxListBox. When I click on update command I am not able to get selected items of dx:ASPxListBox under ASPxGridView_Updating.
<dx:ASPxGridView
ID="ASPxGridView"
runat="server"
AutoGenerateColumns="False"
DataSourceID="TradersDS"
KeyFieldName="TraderId">
<SettingsEditing Mode="PopupEditForm" PopupEditFormWidth="600px" />
<ClientSideEvents RowDblClick="function(s, e)
{
s.StartEditRow(e.visibleIndex);
}"
EndCallback="PrepareGridViewForNewDesing" Init="PrepareGridViewForNewDesing" />
<Columns>
....
<dx:GridViewDataTextColumn FieldName="CounterParty" VisibleIndex="3">
<EditItemTemplate>
<dx:ASPxListBox ID="lstCounterParty" runat="server" SelectionMode="CheckColumn" EnableSelectAll="true"
Width="285"
Height="300"
DataSourceID="CounterPartiesDS"
ValueField="ID"
ValueType="System.String"
TextField="Name">
<%-- <ClientSideEvents SelectedIndexChanged="function(s, e) {lbModels.PerformCallback('1');}" />--%>
</dx:ASPxListBox>
</EditItemTemplate>
</dx:GridViewDataTextColumn>
...
</Columns>
</dx:ASPxGridView>
C# Code..
protected override void ASPxGridView_Updating(object sender, ASPxDataUpdatingEventArgs e)
{
var gridView = sender as ASPxGridView;
GridViewDataColumn list = gridView.Columns["CounterParty"] as GridViewDataColumn;
var counterPartyListBox = (ASPxListBox)gridView.FindEditRowCellTemplateControl(list, "lstCounterParty");
string selectedItemsAsString = string.Empty;
foreach (ListEditItem item in counterPartyListBox.SelectedItems)
selectedItemsAsString += item.Value + ";";
base.ASPxGridView_Updating(sender, e);
}
Here I always get count 0 of SelectedItems. On addition to this My devExpress control version are 9.3.3
What is wrong with this code any help , guidance really appreciated.

Make sure that you set the ASPxListBox.ValueType property to the corresponding .NET type that matches the type of the "ValueField" column.
May be, according to the (ValueField="ID"), the (ASPxListBox.ValueType) should be numeric? (System.Int32, etc.)

Related

How to set disable or readonly in a datagrid view

I have the following datagrid view that is data bound with some data at the DB
I need to load the information in the data grid and make all the textbox in some columns(in the example only comes on but there are many) to be readonly or disable.
<asp:DataGrid ID="grdRequestTypeItem" TabIndex="1" runat="server" CssClass="Grid" AutoGenerateColumns="False"
AllowSorting="True" Visible="true">
<SelectedItemStyle CssClass="GridSelectedItem"></SelectedItemStyle>
<AlternatingItemStyle CssClass="GridAlternatingItem"></AlternatingItemStyle>
<ItemStyle CssClass="GridItem"></ItemStyle>
<HeaderStyle CssClass="GridHeader"></HeaderStyle>
<Columns>
<asp:TemplateColumn HeaderText="Lot Number">
<ItemTemplate >
<asp:TextBox ID="txtLot" runat="server" Width="100%" Text='<%# DataBinder.Eval(Container, "DataItem.Lot") %>' />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
How can I disable that column in the code behind ??
Neither of the following are working
grdRequestTypeItem.Columns[1].IsReadOnly = true;
((BoundField)grdRequestTypeItem.Columns[0]).ReadOnly = true;
((TemplateField)grdRequestTypeItem.Columns[0]).EditItemTemplate = null;
Even if your DataGrid is not in EditMode it displays the TextBox inside the ItemTemplate. Otherwise you use an EditItemTemplate. That's why neither of your solutions work. You have several options:
Put a <asp:Literal> Control into your ItemTemplate
Bind the ReadOnly property of the TextBox to a bool value of your viewModel
from code behind, you have to address the correct control
you could use something like this to reference the textbox within the item template
foreach (GridViewRow row in grdRequestTypeItem.Rows)
{
var txtLot = row.FindControl("txtLot") as TextBox;
txtLog.IsReadOnly = true;
}
You could also use the DataBinding Event on the row instead of looping through the rows like in the answer of #Stealth22
Try this:
<asp:DataGrid ID="grdRequestTypeItem" TabIndex="1" runat="server" CssClass="Grid" AutoGenerateColumns="False" AllowSorting="True" Visible="true"
OnItemDataBound="grdRequestTypeItem_ItemBound">
And then in your code-behind...
protected void grdRequestTypeItem_ItemBound(Object sender, DataGridItemEventArgs e)
{
if((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
{
TextBox txtLot = e.Item.FindControl("txtLot");
if (txtLot != null)
{
txtLot.ReadOnly = true;
}
}
}
Someone can correct me if I am wrong anywhere, as I work more with GridViews than DataGrids, but as far as I know, the principle is the same.

Radio list with SelectedIndexChanged not firing inside radgrid

I have a user control that contains a radio list that on SelectIndexChanged it updates a drop down.
I put together a basic page and add the user control to the page it works fine but when I move the control to inside a radgrid it doesn't work, it will post back but never call the SelectIndexChanged event.
I've pulled up 2 previous questions on this Q. 1 and Q. 2 which say that OnSelectedIndexChanged needed to be set in the aspx page. My issue is that the control doesn't exist in the aspx page and is created later so that solution does not work for me.
Working code
working.aspx
<TT:ToolTipControl ID="ToolTipEdit" runat="server" />
working.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
ToolTipEdit.getEditToolTip("POL_TERM_CD", "DataPolTermDropDownlistEdit");
}
User Control
userControl.ascx.cs
public void getEditToolTip(string fieldName, string ddlName)
{
DataPolTermRadioListBox ccPolTermRadioListBox = new DataPolTermRadioListBox(); //custom radio list
ccPolTermRadioListBox.ID = "PolTermRadioListBox";
ccPolTermRadioListBox.AutoPostBack = true;
ccPolTermRadioListBox.SelectedIndexChanged += new System.EventHandler(updateParent);
ToolTip.Controls.Add(ccPolTermRadioListBox);
}
Broken Code
brokenPage.aspx
<telerik:RadGrid ID="rgState" Skin="WebBlue" runat="server" OnNeedDataSource="rgState_NeedDataSource"
AutoGenerateColumns="False" OnPreRender="rgState_PreRender">
<MasterTableView DataKeyNames="wrtnStPolId" AllowAutomaticUpdates="false" AllowAutomaticDeletes="true"
AllowAutomaticInserts="false" CommandItemDisplay="Top" AllowMultiColumnSorting="True"
EditMode="InPlace" GroupLoadMode="Server" Caption="State(s) and Exposure(s)">
<Columns>
<telerik:GridTemplateColumn AllowFiltering="false" HeaderText="Pol Type Nstd" SortExpression="nonStdPolTypeCd"
UniqueName="nonStdPolTypeCd">
<ItemTemplate>
<asp:Label ID="lblNonStdPolTypeCd" runat="server" align="center" Text='<%#DataBinder.Eval(Container.DataItem, "nonStdPolTypeCd")%>' />
</ItemTemplate>
<EditItemTemplate>
<cc1:DataNonStdTypeCdDropDownList ID="ddlNonStdTypeCd" runat="server" ClientIDMode="Predictable">
</cc1:DataNonStdTypeCdDropDownList>
<TT:ToolTipControl ID="ttcNonStdPolTypeCdEdit" runat="server" />
</EditItemTemplate>
</telerik:GridTemplateColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
brokenPage.aspx.cs
protected void rgState_PreRender(object sender, EventArgs e)
{
RadGrid rgExpMod = (RadGrid)sender;
foreach (GridDataItem row in rgExpMod.Items)
{
GridDataItem gdiItem = (GridDataItem)row;
if (row.FindControl("ttcNonStdPolTypeCdEdit") != null)
{
DropDownList ddl = (DropDownList)row.FindControl("ddlNonStdTypeCd");
ddl.ID += row.RowIndex;
ddl.SelectedIndex = 2;
NCCI.PDC.Web.Controls.ucToolTip ttcNonStdPolTypeCdEdit = (NCCI.PDC.Web.Controls.ucToolTip)row.FindControl("ttcNonStdPolTypeCdEdit");
ttcNonStdPolTypeCdEdit.getEditToolTip("non_std_pol_type_cd", ddl.ID);
}
}
}

Trying to convert functional asp:CommandField to asp:LinkButton via asp:TemplateField

I want to take advantage of the breadth of attributes available to a LinkButton compared to a CommandField. When calling the code behind method from on click I get this error message:
CS0123: No overload for 'ViewHandler' matches delegate
'System.EventHandler'.
GridView definition:
<asp:GridView ID="wc" EmptyDataText="Empty WC table..." runat="server"
AutoGenerateColumns="False" GridLines="Horizontal" DataKeyNames="WC_ID,PW_ID,R_Qty"
CssClass="mGrid" PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt"
AllowSorting="true" OnSorting="Sorting" AllowPaging="true" PageSize="15" ViewStateMode="Enabled"
OnSelectedIndexChanging="ViewHandler" OnPageIndexChanging="HandlePageIndexChanging" OnRowDataBound="gvRowDataBound"
OnRowEditing="gvRowEditing" OnRowCancelingEdit="gvRowCancelingEdit" OnRowUpdating="gvRowUpdating">
Orig button...
<asp:CommandField ControlStyle-Font-Size="Smaller" ControlStyle-ForeColor="#717171" HeaderText="..." ShowSelectButton="True" ItemStyle-HorizontalAlign="Center" SelectText="..." />
Convert to....
<asp:TemplateField HeaderText="..." ShowHeader="True" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:LinkButton style="font-size: 0.9em; color: #717171" ID="Details" runat="server" CausesValidation="False" OnClick="ViewHandler" CommandName="ViewHandler" Text="..." ToolTip="Click here to see purchase history and notes"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
Code behind:
protected void ViewHandler(object sender, GridViewSelectEventArgs e)
{
string WC_ID =wc.Rows[e.NewSelectedIndex].Cells[0].Text;
string PW_ID = wc.Rows[e.NewSelectedIndex].Cells[1].Text;
string VIN = wc.Rows[e.NewSelectedIndex].Cells[2].Text;
string PROD = wc.Rows[e.NewSelectedIndex].Cells[3].Text;
string W = wc.Rows[e.NewSelectedIndex].Cells[4].Text;
string SIZE = wc.Rows[e.NewSelectedIndex].Cells[5].Text;
e.Cancel = true;
Session.Add(WebConstants.WC_ID, WC_ID);
Session.Add(WebConstants.PW_ID, PW_ID);
Session.Add(WebConstants.VIN, VIN);
Session.Add(WebConstants.PROD, PROD);
Session.Add(WebConstants.W, W);
Session.Add(WebConstants.SIZE, SIZE);
DetailsNavigateMethod.DynamicInvoke();
HandlePageNavigation(WebConstants.WC_DETAILS, "WC View for WC_PurchaseID-> " + Session[WebConstants.WC_ID].ToString());
}
What do I need to do to make the signature match with the ViewHandler method? I did notice if I switch from GridViewSelectEventArgs to EventArgs the overload error goes away but then e.NewSelectedIndex does not exists...
Thanks!
Your problem is that the OnClick event handler for a LinkButton must have this signature:
protected void LinkButton_Click(Object sender, EventArgs e)
But you are defining it as
protected void ViewHandler(object sender, GridViewSelectEventArgs e)
However, you won't have access to e.NewSelectedIndex, obviously. What you can do is change your markup to (notice the CommandArgument part):
<asp:TemplateField HeaderText="..." ShowHeader="True" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:LinkButton style="font-size: 0.9em; color: #717171" ID="Details" runat="server" CausesValidation="False" OnClick="ViewHandler" CommandName="ViewHandler" CommandArgument='<%=string.Format("{0},{1},{2},{3}",Eval("Prop1"),Eval("Prop2"),Eval("Prop3"),Eval("Prop4")) %>' Text="..." ToolTip="Click here to see purchase history and notes"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
And your code to:
protected void LinkButton_Click(Object sender, EventArgs e)
{
string [] values = ((LinkButton)(sender)).CommandArgument.Split(new char[]{','});
//where values[0] will have WC_ID, values[1]=PW_ID, etc,
Session.Add(WebConstants.WC_ID, values[0]);
Session.Add(WebConstants.PW_ID, values[1]);//etc...
//Session.Add(WebConstants.VIN, VIN);
//Session.Add(WebConstants.PROD, PROD);
//Session.Add(WebConstants.W, W);
//Session.Add(WebConstants.SIZE, SIZE);
DetailsNavigateMethod.DynamicInvoke();
HandlePageNavigation(WebConstants.WC_DETAILS, "WC View for WC_PurchaseID-> " + Session[WebConstants.WC_ID].ToString());
}
Note:
Prop1, Prop2, etc. have to be changed to actual property names from your data source.
Above code hasn't been tested but that's the idea.

How to programmatically reach any AspxControl inside an AspXGridView's EditItemTemplate

Its very simple and i feel myself as an idiot :(
I newly started to using DevX Controls. Its documentation and sample projects are SUCKS!
My problem is:
I have an ASPxGridView on my aspx page:
<dx:ASPxGridView ID="dxdgMyGrid" runat="server" AutoGenerateColumns="False" OnStartRowEditing="DxGridStartRowEditing">
<SettingsEditing Mode="PopupEditForm" PopupEditFormHeight="200px" PopupEditFormWidth="500px"
EditFormColumnCount="2" PopupEditFormHorizontalAlign="Center" PopupEditFormVerticalAlign="Middle"
PopupEditFormModal="true" />
<Columns>
<dx:GridViewDataTextColumn FieldName="MyField1" VisibleIndex="1">
<EditFormSettings VisibleIndex="0" />
<EditItemTemplate>
<dx:ASPxDateEdit ID="dxdateMyField1" runat="server">
</dx:ASPxDateEdit>
</EditItemTemplate>
</dx:GridViewDataTextColumn>
<dx:GridViewDataColumn FieldName="MyField2" VisibleIndex="4">
<EditFormSettings VisibleIndex="1" />
<EditItemTemplate>
<dx:ASPxComboBox ID="dxcomboMyField2" runat="server">
</dx:ASPxComboBox>
</EditItemTemplate>
</dx:GridViewDataColumn>
</Columns>
How can i reach dxdateMyField1 or dxcomboMyfield2 on ASPX.CS file? I want to write:
dxcomboMyField2.DataSource = GetMyData2List();
dxcomboMyField2.SelectedItemIndex = 0;
... etc.
Thanks a lot.
You cannot access the EditItemTemplate Control Directly. You can access them at the HtmlRowCreated event as:
if (e.RowType != GridViewRowType.InlineEdit) return;
ASPxTextBox txtBox = ASPxGridView1.FindEditRowCellTemplateControl(ASPxGridView1.Columns["Name"]
as GridViewDataColumn, "ASPxTextBox1") as ASPxTextBox;
Check the documentation on Accessing Controls Contained within Templates
It is possible to cast the ASPxLabel.NamingContainer property to GridViewEditItemTemplateContainer and get a column value via the GridViewEditItemTemplateContainer.Text property.
But I like the technique of using the Init/Load event handler.When the grid switches to edit mode, the ASPxLabel.Load event is raised. Check this article The general technique of using the Init/Load event handler for implementation help.
[ASPx]
<dxe:ASPxTextBox ID="txtName" runat="server" Width="170px" OnInit="txtName_Init">
</dxe:ASPxTextBox>
[C#]
ASPxTextBox txtName;
protected void txtName_Init(object sender, EventArgs e)
{
txtName = (ASPxTextBox)sender;
GridViewEditFormTemplateContainer container = txtName.NamingContainer as GridViewEditFormTemplateContainer;
// You can remove the if statement, and try to insert a new record. You'll catch an exception, because the DataBinder returns null reference
if (!container.Grid.IsNewRowEditing)
txtName.Text = DataBinder.Eval(container.DataItem, "CategoryName").ToString();
}
Update Event:
protected void grid_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
{
e.NewValues["CategoryName"] = txtName.Text;
}
There is already an question - ASPxGridView - How to find a control inside the EditItemTemplate on DevExpress fourm .
You can use combo box init/load event handler for setting combo datasource. If that doesn't work for you, use FindEditRowCellTemplateControl (use link in comments for further explanation).

Unable to access the checkbox for Serverside coding

Below is my gridview ,
<asp:GridView ID="gridview1" AutoGenerateColumns="False" runat="server"
EnableModelValidation="True" >
<Columns>
<asp:CommandField ButtonType="Link" HeaderText="Delete" InsertImageUrl="~/_layouts/images/TBS.WebParts/Button-Delete-icon.png" ShowDeleteButton="true"/>
<asp:BoundField HeaderText ="Size" DataField="FileSize" />
<asp:BoundField HeaderText ="File" DataField="Size" />
<asp:TemplateField HeaderText="SupportIncluded">
<ItemTemplate>
<asp:CheckBox ID="Checkbox1" runat="server" Checked="false" />
</ItemTemplate>
Now on server side i want to check if the checkbox is checked or not on Submit button click event.
private void btnSubmit_Click(object sender, EventArgs e)
{
if (IsValidPost())
{
bool flag = false;
for( int i=0; i < gridview1.Rows.Count ; i++)
{
if(dgdUpload.Rows[i].FindControl("Checkbox1"),CheckBox).Checked) erorr here...I also tried ....if(Checkbox1.checked)...but unable to access Checkbox1..it says it does not exist in the current context....
flag = true;
}
if(flag)
{
}
}
}
You need to access not only the row but also the template field control. Another option is to develop your own recursive version of FindControl that does a search on the whole tree and not just on one level.
Something like:
dgdUpload.Rows[i].Controls[5].FindControl("Checkbox1")
if I counted the columns in your gridview correctly 5 should be the index of the template field.

Resources