How to set Mask property of RadMaskedTextBox( in EditItemTemplate) dynamically on Selected index changed event of RadComboBox in Radgrid - telerik

How to set the Mask property of RadMaskedTexbox, which is in EditItemTemplate in one column of Radgrid, on SelectedIndexChanged Event of RadCombobox, in EditItemTemplate of another column, in Radgrid, which is in Batch Edit Mode?
Note: RadGrid is in BatchEdit Cell Mode.
Not able to find the RadMaskedTextbox control, on selected index changed event of Combobox.
<telerik:RadComboBox ID="rdcbxCountry" SelectedValue='<%# Eval("Country") %>' runat="server" RenderMode="Auto" AutoPostBack="True" EnableLoadOnDemand="true" OnItemsRequested="rdcbxCountry_ItemsRequested" OnSelectedIndexChanged="rdcbxCountry_SelectedIndexChanged"></telerik:RadComboBox>
</EditItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn HeaderText="Office Phone" SortExpression="Office_Phone" UniqueName="Office_Phone" HeaderStyle-Width="120px">
<ItemTemplate>
<asp:Label ID="lblOfficePhone" runat="server" Text=' <%# Eval("Office_Phone") %>' Width="110px">
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<telerik:RadMaskedTextBox RenderMode="Lightweight" runat="server" Text='<%# Eval("Office_Phone") %>' Mask="(###) ###-####" Width="`enter code here`110px"
ID="txtOfficePhone">
</telerik:RadMaskedTextBox>
</EditItemTemplate>
</telerik:GridTemplateColumn>

Looks like this was posted a while back...
I was able to capture the grid_ItemDataBound event, and call a 'change' method in there. Like you, I also have a 'click-event' to the control I wanted to capture. Something like:
Private Sub grd_ItemDataBound(sender As Object, e As Telerik.Web.UI.GridItemEventArgs) Handles grd.ItemDataBound
If e.Item.OwnerTableView.Name = "Grid" Then
rdcbxCountry_SelectedIndexChanged(e.Item.FindControl("rdcbxCountry"), New System.EventArgs)
EndIf
End Sub
Protected Sub rdcbxCountry_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Dim rdcbxCountry As RadComboBox= CType(sender, RadComboBox)
Dim txtOfficePhone= CType(rdcbxCountry.Parent.FindControl("txtOfficePhone"), RadMaskedTextBox)
If rdcbxCountry.SelectedValue = "US" then
txtOfficePhone.Mask = "(###) ###-####"
Else
...
End If
End if
Good luck!

Related

How Can I get Cell Values from Telerik RadGrid

I am having a problem getting the values from the Radgrid auto generated Insert form.
I have my radgrid setup as shown in code shown below. I am only doing inserts and deletes and am using the edit form that is automatically opened when I click the add new record button.
<div id ="specialrequestGrid" class="container">
<div class="row">
<div class="col-md-12">
<telerik:RadAjaxPanel runat="server">
<telerik:RadGrid ID="SpecialRequests" runat="server" OnInsertCommand="SpecialRequests_InsertCommand"
OnUpdateCommand="SpecialRequests_UpdateCommand"
OnItemCommand="SpecialRequests_ItemCommand"
AllowAutomaticInserts="false"
AutoGenerateColumns="false"
OnNeedDataSource="SpecialRequests_NeedDataSource">
<MasterTableView CommandItemDisplay="Top" DataKeyNames="CaseId_FK, ReqId" Font-Size="Medium" NoMasterRecordsText="No Special Requests">
<CommandItemSettings AddNewRecordImageUrl="../Images/Add.png" />
<Columns>
<telerik:GridTemplateColumn HeaderText="Request Date" UniqueName="DateTemplateColumn">
<ItemTemplate>
<asp:Label ID="DateEditItemTemplate" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Date","{0:MM/dd/yyyy}") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<telerik:RadDatePicker ID="dpRideDate" runat="server" DateInput-DateFormat="MM/dd/yyyy" DbSelectedDate='<%# Bind("Date") %>' MinDate="1999/1/1" UniqueName="dpRideDate">
</telerik:RadDatePicker>
</EditItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn HeaderText="Requested By ID" UniqueName="RequestorTemplateColumn">
<ItemTemplate>
<asp:Label ID="Requestor" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "RequesterEID_FK") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<button id="btnRequestEID" type="button" class="btn" data-toggle="modal" data-target="#myModal" >--Please Select--</button>
<div>
<input type="text" Id="TxtSPRequestName" disabled />
</div>
</EditItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn HeaderText="Request Reason" UniqueName="RequestReason">
<ItemTemplate>
<asp:Label ID="ItemTemplatelblRequestReason" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Reason") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblRequestReason" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Reason") %>' Visible="false">
</asp:Label>
<asp:TextBox runat="server" ID="txtReason" Text='<%# DataBinder.Eval(Container.DataItem, "Reason") %>'></asp:TextBox>
</EditItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridCheckBoxColumn DataField="IsWOO" HeaderText="Is Out of Order" UniqueName="ISWOO"></telerik:GridCheckBoxColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
</telerik:RadAjaxPanel>
</div>
</div>
</div>
This is the screenshot of the auto generated form I get when I click the add new record
When I click the Insert link on the form I run this C# code
protected void SpecialRequests_InsertCommand(object sender, GridCommandEventArgs e)
{
GridEditableItem editedItem = e.Item as GridEditableItem;
UserControl userControl = (UserControl)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);
T_SpecialRequests t = new T_SpecialRequests();
t.IsWOO = (userControl.FindControl("IsWOO") as CheckBox).Checked;
}
Note the IsWOO is a control in the Radgrid that I did not show in the Screenshot. When I click the save link the Insert Function, shown above Runs but I get the following error:
Unhandled exception at line 1, column 123034 in http://localhost:52028/bundles/MsAjaxJs?v=c42ygB2U07n37m_Sfa8ZbLGVu4Rr2gsBo7MvUEnJeZ81
0x800a139e - JavaScript runtime error: Sys.WebForms.PageRequestManagerServerErrorException: Object reference not set to an instance of an object.
The strange part is I use this same code in several other projects without any problems. The only difference is this is the first time I have used this auto generated form. I don't want to use the another type of control for this Radgrid due to such a small number of fields, only have 4 control.
I have spent several hours searching for an answer but just can't find the solution.
How can I get the values from this auto generated form?
You can use
t.IsWOO = (editedItem["ISWOO"].Controls[0] as CheckBox).Checked;
or
var newValues = new Dictionary<string, object>();
e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);
t.IsWOO = (bool)newValues["IsWOO"];
The first approach references the control by index because unfortunately it has no ID.
The second one is a bit verbose for just one property but I would surely prefer it if there were more properties.
While #Michal Nawrocik was correct I found that I still had some problems reading all of the controls in the Radgrid. I was able to track down the answer on the Telerik forum and found that you have to use different code based on the Column Type. I had 2 different type of columns.
the code below shows what I used to get all of the control values on the form
protected void SpecialRequests_InsertCommand(object sender, GridCommandEventArgs e)
{
try
{
using(LatentEntities db = new LatentEntities())
{
if (e.Item is GridEditableItem)
{
T_SpecialRequests T = new T_SpecialRequests();
var editableItem = ((GridEditableItem)e.Item);
RadDatePicker picker = (RadDatePicker)editableItem.FindControl("dpDate"); // in EditItem Template
DateTime dt = Convert.ToDateTime(picker.DbSelectedDate);
T.Date = dt;
TextBox Reason = (TextBox)editableItem.FindControl("txtReason"); //This Control was in an Edit Item Template
T.Reason = Reason.Text;
T.IsWOO = (editableItem["ISWOO"].Controls[0] as CheckBox).Checked; //In CheckBox Column
}
}
And if the control is in a Telerik:GridtboundColumn I used this code:
string strFirstName = (editableItem["firstname"].Controls[0] as TextBox).Text;

Telerik : Get Column name from FilterTemplate controls Client Event

I am trying to add RadCombobox to a telerik:GridTemplateColumn as a FilterTemplate control. I have multiple columns and all have RadCombobox with same filter values.
<telerik:GridTemplateColumn AutoPostBackOnFilter="true" HeaderStyle-Width="90px"
UniqueName="Date1" HeaderText="Date1" DataField="Day1">
<FilterTemplate>
<telerik:RadComboBox ID="RadComboBoxDate1" Height="90px" AppendDataBoundItems="true"
Width="105px" runat="server" OnClientSelectedIndexChanged="Date1IndexChanged">
</telerik:RadComboBox>
<telerik:RadScriptBlock ID="RadScriptBlock1" runat="server">
<script type="text/javascript">
function Date1IndexChanged(sender, args) {
var tableView = $find("<%# TryCast(Container,GridItem).OwnerTableView.ClientID %>");
var selectedValue = sender.get_value();
if (selectedValue) {
if (parseInt(selectedValue) < 0) {
tableView.filter("Date1", selectedValue, "NoFilter");
}
else { tableView.filter("Date1", selectedValue, "EqualTo");
}
}
}
</script>
</telerik:RadScriptBlock>
</FilterTemplate>
<ItemTemplate>
<asp:Label runat="server" ID="lblday1"></asp:Label>
<asp:HiddenField ID="hdDay1" runat="server" Value='<%# DataBinder.Eval(Container.DataItem,"Day1") %>' Visible="false" />
</ItemTemplate>
</telerik:GridTemplateColumn>
I have 7 such columns and I want to use a single RadCodeBlock to handle the filter. Is there any better way to do this?
Solution given on Telerik website worked for me
http://www.telerik.com/forums/how-to-get-column-name-from-filter-template-control-s-js-event

How to find TextBox ID inside EditFormSettings using jquery

<EditFormSettings EditFormType="Template" PopUpSettings-Width="80%">
<EditColumn FilterControlAltText="Filter EditCommandColumn column"></EditColumn>
<FormTemplate>
<asp:TextBox ID="txtLastName" Text='<%# Bind( "last_name") %>' runat="server" CssClass="form-control"> </asp:TextBox>
</FormTemplate>
</EditFormSettings>
How Can I get TextBox(txtLastName) id from Client Site
To find any control inside template/itemtemplate you need to find the element by attributeEndsWith selector.
As when the textbox renders the clientID of the textbox is assign at the last of the id attribute(which is dynamically generated) of the rendered control (in your case textbox)
var txtLastNameID=$("[id$='txtLastName']").attr("id");// will give you id(dynamically generated)
var txtLastNameI=$("[id$='txtLastName']");// will give you object of control

Telerik rad grid combo box event

I m using telerik rad grid
I have used combo box to display company Type.
If company Type is ABC than Design Firm type should not be displayed
how to write code on combobox in telerik radgrid ![enter image description here][1]
You will need to take two steps to make this work. First, set up an EditForm Template in your RadGrid. Next, you'll need to use related RadComboBoxes (as shown in this example) to achieve your desired functionality.
EditForm Template example code
<telerik:GridTemplateColumn UniqueName="ContactTitle" HeaderText="ContactTitle">
<EditItemTemplate>
<telerik:RadComboBox ID="RadComboBox1" AutoPostBack="true" runat="server" SelectedValue='<%# Bind("CountryID") %>' DataSourceID="SqlDataSource1" DataTextField="CountryID" OnSelectedIndexChanged="RadComboBox1_SelectedIndexChanged">
</telerik:RadComboBox>
</EditItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn UniqueName="TemplateColumn">
<EditItemTemplate>
<telerik:RadComboBox ID="RadComboBox2" runat="server">
</telerik:RadComboBox>
</EditItemTemplate>
</telerik:GridTemplateColumn>
Cascading ComboBox example
protected void RadComboBox1_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
{
RadComboBox rdcbx = (RadComboBox)o;
GridEditableItem editedItem = rdcbx.NamingContainer as GridEditableItem;
RadComboBox ddList = editedItem.FindControl("RadComboBox2") as RadComboBox;
// change the data source for ddList here
.....
}
source

Accessing radgrid row through object data source

I have a radgrid and within said radgrid, I have a templatecolumn that is a lovely drop down list:
<telerik:GridTemplateColumn HeaderText="Feedback" UniqueName="Feedback">
<ItemTemplate>
<asp:DropDownList ID="ddlFeedback" runat="server" AppendDataBoundItems="True" AutoPostBack="True" OnSelectedIndexChanged="ddlFeedback_SelectedIndexChanged">
</asp:DropDownList>
</ItemTemplate>
</telerik:GridTemplateColumn>
I have a column in my radgrid that is the primary key and it has a lovely value in it.
How can I get the primary key value from the radgrid on the SelectedIndexChanged event of the drop down list i.e. essentially finding the row of the radgrid where the drop down list has been changed?
Found it. Thanks!
DropDownList ddlFeedback = (DropDownList)sender;
GridDataItem item = (GridDataItem)ddlFeedback.NamingContainer;
String prikey = item.GetDataKeyValue("PriKey").ToString();

Resources