How do you read the chosen value from a RadComboBox? - asp.net

Inside a RadGrid, I have a drop RadComboBox that is populated by a web service.
I am using an EditItemTemplate nested inside a GridTemplateColumn to hold it, as shown:
On the server side, how can I access the value chosen by the user from the RadComboBox?
<telerik:GridTemplateColumn UniqueName="UserCol" HeaderText="proto user" DataField="UserID">
<EditItemTemplate>
<telerik:RadComboBox ID="RadComboBox1" runat="server" AutoPostBack="false" CausesValidation="true"
Width="240" MaxHeight="200px" OnItemsRequested="ddEmployee_ItemsRequested" AllowCustomText="true"
EnableLoadOnDemand="true" ShowMoreResultsBox="true" EnableVirtualScrolling="true"
MarkFirstMatch="false" >
</telerik:RadComboBox>
</EditItemTemplate>
</telerik:GridTemplateColumn>

It depends on what event you're handling.
You can set AutoPostBack="true" and handle the OnSelectedIndexChanged event of the RadComboBox. This is very straightforward as you can get the selected value either from the EventArgs or from the sender object which is the RadComboBox itself.
Check this out: http://www.telerik.com/help/aspnet-ajax/combobox-server-side-selectedindexchanged.html
If you are handling a row operation event such as insert or update, you need to find the RadComboBox object from the GridItem (e.Item).
protected void RadGrid_RowOperation(object sender, Telerik.Web.UI.GridCommandEventArgs e)
{
// this will find the control
RadComboBox RadComboBox1 = (RadComboBox)(e.Item.FindControl("RadComboBox1"));
// so you can get the selected value
string value = RadComboBox1.SelectedValue;
}

Related

Event not firing as expected for control in formview asp.net

I have a LinkButton in an InsertItemTemplate which when clicked, should display a hidden DropDownList in the InsertItemTemplate. However, it doesn't seem to be working, but it will say, change the text of a label outside the Formview when the LinkButton is clicked. The event is firing, but the part to make the DropDownList visible in the InsertItemTemplate is not doing anything. Code is below:
.aspx:
<asp:FormView ID="formViewNewRecord" runat="server">
<InsertItemTemplate>
<asp:DropDownList ID="ddlAddSelection2" runat="server" DataSourceID="dSource1" DataTextField="Users" DataValueField="Users" AppendDataBoundItems="true" Visible="false">
<asp:ListItem></asp:ListItem>
</asp:DropDownList>
<asp:LinkButton runat="server" ID="lbAddAnother" OnClick="lbAddAnother_Click">+Add Another</asp:LinkButton>
</InsertItemTemplate>
</asp:FormView>
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
C#:
protected void lbAddAnother_Click(object sender, EventArgs e)
{
DropDownList addSelection2 = (DropDownList)formViewNewItem.Row.Cells[0].FindControl("ddlAddSelection2");
addSelection2.Visible = true;
Label2.Text = addSelection2.ID;
}
Your dropdown control is not an immediate child of your formview. So since the FindControl call is not recursive, you have to search for the control in the right location of your form view's child controls. See this for the details but at a high level, you need something along the lines of:
DropDownList ctrl = (DropDownList)FormView1.Row.Cells[0].FindControl("ddlAddSelection2");
After that, you should check it for null for safe measure.

Lost Focus Events for a Control Within GridView

I have multiple textboxes and dropdown lists within my GridView. For one particular textbox I need trigger a server event which gets data from the database and fills it in other columns of the Grid. Is there a simple way to do it or a slightly complicated way as detailed here
I have no problems implementing the above method or thinking of a work around but then thought that there is Cell Lost Focus in a grid control surprises me a little. Am I missing something ? Any help on this would appreciated.
You can set AutoPostBack to true and handle it's TextChanged event.
<asp:GridView ID="GridView1" runat="server" EmptyDataText="It's Empty.">
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:TextBox ID="txtName"
runat="server"
Text='<%#Eval("Name") %>'
AutoPostBack="true"
OnTextChanged="NameChanged" >
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</GridView>
in codebehind:
protected void NameChanged(Object sender, EventArgs e)
{
var txtName = (TextBox) sender;
var row = (GridViewRow) txtName.NamingContainer;
// you could find other controls in this GridViewRow via
// row.FindControl("ControlID") in case of a TemplateField or
// row.Cells[0].Text (0 = index of column) in case of a BoundField
}

Calling code-behind from button - No overload for matches

I have a FormView used to edit records but when the user presses the Button, it also needs to do some back-end stuff in the code-behind. For this I need to pass the ID of the record.
Currently this is my code in the page:
<asp:LinkButton ID="savebtn" runat="server" CausesValidation="True"
CommandName="Update" Text="Save Changes"
OnClick="setProcessProgress" CommandArgument='<%# Eval("id") %>' />
And this is a stripped-down version of my code-behind:
protected void setProcessProgress(object sender, FormViewCommandEventArgs e)
{
if (e.CommandName == "Update")
ID_p = Int32.Parse((string)e.CommandArgument);
}
I'm getting the No overload for 'setProcessProgress' matches delegate 'System.EventHandler'
I tried changing the EventArgs to CommandEventArgs and it didn't work either.
If I use OnCommand instead of OnClick it just doesn't call setProcessProgress. Any ideas?
According to the MSDN Documentation on FormViewCommandEventArgs.
The ItemCommand event is raised when a button within the FormView
control is clicked. This allows you to provide an event-handling
method that performs a custom routine whenever this event occurs.
Buttons within a FormView control can also invoke some of the built-in
functionality of the control. To perform one of these operations, set
the CommandName property of a button to one of the values in the
following table.
The event is not assigned to the Button's OnClick event, it is actually assigned to the FormViews ItemCommand Event
i.e
<asp:FormView ID="FormView1" runat="server"
onitemcommand="setProcessProgress">
<asp:LinkButton ID="savebtn" runat="server" CausesValidation="True"
CommandName="Update" Text="Save Changes"
CommandArgument='<%# Eval("id") %>'
protected void setProcessProgress(object sender, EventArgs e)
{
LinkButton btn = sender as LinkButton;
if (btn.CommandName=="Update")
{
...
Response.Write("Updated");
...
}
}
Maybe you can try EventArgs e in your method.

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).

give the ID as a custom attribute to checkbox in a grid for manual update

I like to update just the value of my checkbox in a asp grid.
so i thought i bind the id to the checkbox ( but how?) and fire an update in code behind by clicking the checkbox.
but how can I get the ID, and where I have to bind this id to get it in code behind in the event?
Thanks
Here is what I've managed to do. (Be aware there should be an easier way to do this. I'm very new to ASP.NET)
Here you have a TemplateField in a GridView. Inside it there is an UpdatePanel and the CheckBox is inside it. This is done to make the checking of the TextBox do post in the backgroung (ajax). You might not need it (UpdatePanel) at all.
<asp:TemplateField HeaderText="Private" SortExpression="IsPrivate">
<ItemTemplate>
<asp:UpdatePanel ID="upIsPrivate" runat="server" UpdateMode="Always" ChildrenAsTriggers="true">
<ContentTemplate>
<asp:CheckBox ID="chkIsPrivate" runat="server" OnCheckedChanged="chkIsPrivate_CheckedChanged" AutoPostBack="true" />
</ContentTemplate>
</asp:UpdatePanel>
</ItemTemplate>
</asp:TemplateField>
And this is the method that handles this. Notice that I get the Id from the GridViewRow that contains the CheckBox:
GridViewRow row = (GridViewRow)((CheckBox)sender).Parent.Parent.Parent.Parent;
protected void chkIsPrivate_CheckedChanged(object sender, EventArgs e)
{
if (editMode)
{
GridViewRow row = (GridViewRow)((CheckBox)sender).Parent.Parent.Parent.Parent;
Int32 id = (Int32)uxPhoneCallList.DataKeys[row.RowIndex]["Id"];
CheckBox isPrivate = (CheckBox)row.FindControl("chkIsPrivate");
PhoneCall phoneCall = PhoneCallManager.GetById(id);
...
}
}

Resources