Unable to download file using AsyncPostBackTrigger in RadGrid - telerik

Hi all I am using RadGrid in my application, as I want my RadGrid not to refresh I had my RadGrid in an Update Panel as follows
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="RadGrid1" EventName="ItemCommand" />
<%--<asp:PostBackTrigger ControlID="RadGrid1" />--%>
</Triggers>
<ContentTemplate>
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnItemCommand="RadGrid1_ItemCommand"
OnNeedDataSource="RadGrid1_NeedDataSource">
<MasterTableView Width="950" AutoGenerateColumns="false" DataKeyNames="EmpID" GridLines="None"
TableLayout="Auto">
<Columns>
<telerik:GridBoundColumn DataField="EmpID" HeaderText="Emp ID" ReadOnly="true" HeaderStyle-HorizontalAlign="Left"
ItemStyle-HorizontalAlign="Left" UniqueName="EmpID" FilterControlWidth="30px"
AutoPostBackOnFilter="true" CurrentFilterFunction="Contains" />
<telerik:GridButtonColumn DataTextField="ButtonName" ItemStyle-ForeColor="Blue" CommandName="Generate"
ConfirmTextFields="ButtonName" ConfirmTextFormatString="Would you like to {0} ACH file ?"
ConfirmDialogType="RadWindow" Reorderable="false" UniqueName="ButtonName" ConfirmTitle="ACH File">
</telerik:GridButtonColumn>
<telerik:GridBoundColumn DataField="EmployeeName" HeaderText="Employee Name" ReadOnly="true"
HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left" UniqueName="EmployeeName"
FilterControlWidth="60px" AutoPostBackOnFilter="true" CurrentFilterFunction="Contains" />
</Columns>
</MasterTableView>
</telerik:RadGrid>
</ContentTemplate>
</asp:UpdatePanel>
When I click on download button I am unable to download the file this is my code in Itemcommand
protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
{
if (e.CommandName == "Generate")
{
Response.ContentType = "text/plain";
Response.AppendHeader("Content-Disposition", "attachment;filename= errorLog.txt");
Response.AddHeader("content-length", "0");
Response.Flush();
Response.End();
}
}
Can some one help me how can I work out this using AsyncPostBackTrigger

You cannot call Response with AsyncPostBack. It is Ajax Framework limitation.
Updated 1/28/2013
Since you are using telerik, I prefer using RadAjaxManager. Basically, when Generate button is clicked, it uses regular post back instead of ajax. In my example, sorting still uses ajax.
<telerik:RadGrid ID="RadGrid1" AutoGenerateColumns="false" AllowSorting="True" runat="server"
OnNeedDataSource="RadGrid1_NeedDataSource" OnItemCommand="RadGrid1_ItemCommand">
<MasterTableView DataKeyNames="EmpID">
<Columns>
<telerik:GridTemplateColumn>
<ItemTemplate>
<asp:Button runat="server" CommandName="Generate" ID="GenerateButton"
Text="Generate" OnClientClick="Generate(this, event); return false;" />
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridBoundColumn DataField="EmpID" HeaderText="Emp ID" UniqueName="EmpID" SortExpression="EmpID" />
<telerik:GridBoundColumn DataField="EmployeeName" HeaderText="Employee Name" UniqueName="EmployeeName" />
</Columns>
</MasterTableView>
</telerik:RadGrid>
<%-- RadAjaxManager --%>
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
<AjaxSettings>
<telerik:AjaxSetting AjaxControlID="RadGrid1">
<UpdatedControls>
<telerik:AjaxUpdatedControl ControlID="RadGrid1" LoadingPanelID="RadAjaxLoadingPanel1" />
</UpdatedControls>
</telerik:AjaxSetting>
</AjaxSettings>
</telerik:RadAjaxManager>
<%-- RadAjaxLoadingPanel --%>
<telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" Skin="Default">
</telerik:RadAjaxLoadingPanel>
<telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
<script type="text/javascript">
function Generate(sender, e) {
$find("<%= RadAjaxManager1.ClientID %>").__doPostBack(sender.name, "");
}
</script>
</telerik:RadCodeBlock>
public class Employee
{
public int EmpID { get; set; }
public string EmployeeName { get; set; }
}
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
RadGrid1.DataSource = new List<Employee>
{
new Employee {EmpID = 1, EmployeeName = "John"},
new Employee {EmpID = 2, EmployeeName = "Marry"},
new Employee {EmpID = 3, EmployeeName = "Eric"}
};
}
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
if (e.CommandName == "Generate")
{
Response.ContentType = "text/plain";
Response.AppendHeader("Content-Disposition", "attachment;filename= errorLog.txt");
Response.AddHeader("content-length", "0");
Response.Flush();
Response.End();
}
}

Related

RadComboBox with checkboxes in Radgrid - select values from the table

I have a RadComboBox with checkboxes in Radgrid. User is able to select multiple values and data is saved into database. The problem I am having is, how to display already selected values when pulling data from database.
<telerik:RadGrid RenderMode="Lightweight" AutoGenerateColumns="false" ID="grd_incontact_settings" BorderWidth="0" Font-Size="Smaller" Width="100%" ShowFooter="True" AllowPaging="True" runat="server" PageSize="250" PagerStyle-AlwaysVisible="true"
OnNeedDataSource="grd_incontact_settings_NeedDataSource" AllowAutomaticInserts="True" OnInsertCommand="grd_incontact_settings_InsertCommand">
<GroupingSettings CaseSensitive="false"></GroupingSettings>
<MasterTableView AutoGenerateColumns="false" CommandItemDisplay="TopAndBottom" DataKeyNames="Id" EditMode="InPlace" ShowHeader="true">
<Columns>
<telerik:GridBoundColumn DataField="Id" UniqueName = "Id" AllowFiltering="false" Display ="false">
</telerik:GridBoundColumn>
<telerik:GridTemplateColumn HeaderText="Days" HeaderStyle-Width="150px" ItemStyle-Width="150px" UniqueName="ScheduleDays">
<ItemTemplate>
<%# Eval("ScheduleDays") %>
</ItemTemplate>
<EditItemTemplate>
<telerik:RadComboBox RenderMode="Lightweight" runat="server" ID="ddl_ScheduleDays" CheckedItemsTexts="DisplayAllInInput" CheckBoxes="true" SelectedValue='<%#Bind("ScheduleDays") %>' >
<Items>
<telerik:RadComboBoxItem Text="None" Value= 0 runat="server" />
<telerik:RadComboBoxItem Text="Monday" Value= 1 runat="server" />
<telerik:RadComboBoxItem Text="Tuesday" Value= 2 runat="server" />
<telerik:RadComboBoxItem Text="Wednesday" Value= 4 runat="server" />
<telerik:RadComboBoxItem Text="Thursday" Value= 8 runat="server" />
<telerik:RadComboBoxItem Text="Friday" Value= 16 runat="server" />
<telerik:RadComboBoxItem Text="Saturday" Value= 32 runat="server" />
<telerik:RadComboBoxItem Text="Sunday" Value= 64 runat="server" />
</Items>
</telerik:RadComboBox>
</EditItemTemplate>
</telerik:GridTemplateColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
Code Behind:
protected void grd_incontact_settings_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
{
List<SP_InContactSettings_Get_Result> lst_incontact_settings = new List<SP_InContactSettings_Get_Result>();
using (var db = new data.tarpasql())
{
lst_incontact_settings = db.SP_InContactSettings_Get().ToList();
}
grd_incontact_settings.DataSource = lst_incontact_settings;
}
public partial class SP_InContactSettings_Get_Result
{
public int Id { get; set; }
public List<int> ScheduleDays { get; set; }
}
This is what I am getting:

Telerik RadGrid Need in Edit Mode

I have the following in my .aspx file. When the user clicks on Edit, I like it to show a form where they can Update or Cancel. Currently when I click on Edit, it does nothing. Note that am handling the datasource from the code behind. As such, I do not have a DataSource within the .aspx file for the RadGrid:
Here is my code:
<telerik:RadGrid ID="rdClosedLoop" runat="server" GridLines="None" OnDeleteCommand="DeleteClosedLoop"
Skin="Metro" ActiveItemStyle-HorizontalAlign="Center" AutoGenerateEditColumn="true" >
<MasterTableView EditMode="EditForms" AutoGenerateColumns="False" DataKeyNames="ID" >
<RowIndicatorColumn>
<HeaderStyle Width="20px" />
</RowIndicatorColumn>
<ExpandCollapseColumn>
<HeaderStyle Width="20px" />
</ExpandCollapseColumn>
<Columns>
<telerik:GridBoundColumn DataField="ID" HeaderText="ID" SortExpression="ID" EditFormColumnIndex="0"
UniqueName="ID" HeaderStyle-HorizontalAlign="Center">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="ActiveInhibitor" HeaderText="ActiveInhibitor" SortExpression="ActiveInhibitor" EditFormColumnIndex="0"
UniqueName="ActiveInhibitor" HeaderStyle-HorizontalAlign="Center">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Conductivity" HeaderText="Conductivity" SortExpression="Conductivity" EditFormColumnIndex="0"
UniqueName="Conductivity" HeaderStyle-HorizontalAlign="Center">
</telerik:GridBoundColumn>
<telerik:GridButtonColumn CommandName="Delete" Text="Delete" UniqueName="DeleteColumn">
</telerik:GridButtonColumn>
</Columns>
<EditFormSettings ColumnNumber="1" CaptionFormatString="Edit for ID {0}"
CaptionDataField="ID">
<FormTableItemStyle Wrap="False"></FormTableItemStyle>
<FormCaptionStyle CssClass="EditFormHeader"></FormCaptionStyle>
<FormMainTableStyle GridLines="None" CellSpacing="0" CellPadding="3" Width="100%">
</FormMainTableStyle>
<FormTableStyle GridLines="Horizontal" CellSpacing="0" CellPadding="2" CssClass="module"
Height="110px" Width="100%"></FormTableStyle>
<FormTableAlternatingItemStyle Wrap="False"></FormTableAlternatingItemStyle>
<FormStyle Width="100%" BackColor="#eef2ea"></FormStyle>
<EditColumn UpdateText="Update record" UniqueName="EditCommandColumn1" CancelText="Cancel edit">
</EditColumn>
<FormTableButtonRowStyle HorizontalAlign="Left" CssClass="EditFormButtonRow"></FormTableButtonRowStyle>
</EditFormSettings>
</MasterTableView>
<HeaderContextMenu CssClass="GridContextMenu GridContextMenu_Default" EnableImageSprites="True">
</HeaderContextMenu>
</telerik:RadGrid>
Code behind:
protected void btnClosedLoop_Click(object sender, EventArgs e)
{
// Note - verId is based on some conditions
var resultCLList = (from ms in db.ver_ClosedLoop
where ms.VerificationId == verId
select ms).ToList();
rdClosedLoop.DataSource = resultCLList;
rdClosedLoop.DataBind();
}
Please try with the below code snippet.
ASPX
<telerik:RadGrid ID="rdClosedLoop" runat="server" GridLines="None"
Skin="Metro" ActiveItemStyle-HorizontalAlign="Center" AutoGenerateEditColumn="true"
OnItemCommand="rdClosedLoop_ItemCommand">
<MasterTableView EditMode="EditForms" AutoGenerateColumns="False" DataKeyNames="ID">
<RowIndicatorColumn>
<HeaderStyle Width="20px" />
</RowIndicatorColumn>
<ExpandCollapseColumn>
<HeaderStyle Width="20px" />
</ExpandCollapseColumn>
<Columns>
<telerik:GridBoundColumn DataField="ID" HeaderText="ID" SortExpression="ID" EditFormColumnIndex="0"
UniqueName="ID" HeaderStyle-HorizontalAlign="Center">
</telerik:GridBoundColumn>
<telerik:GridButtonColumn CommandName="Delete" Text="Delete" UniqueName="DeleteColumn">
</telerik:GridButtonColumn>
</Columns>
<EditFormSettings ColumnNumber="1" CaptionFormatString="Edit for ID {0}" CaptionDataField="ID">
<FormTableItemStyle Wrap="False"></FormTableItemStyle>
<FormCaptionStyle CssClass="EditFormHeader"></FormCaptionStyle>
<FormMainTableStyle GridLines="None" CellSpacing="0" CellPadding="3" Width="100%">
</FormMainTableStyle>
<FormTableStyle GridLines="Horizontal" CellSpacing="0" CellPadding="2" CssClass="module"
Height="110px" Width="100%"></FormTableStyle>
<FormTableAlternatingItemStyle Wrap="False"></FormTableAlternatingItemStyle>
<FormStyle Width="100%" BackColor="#eef2ea"></FormStyle>
<EditColumn UpdateText="Update record" UniqueName="EditCommandColumn1" CancelText="Cancel edit">
</EditColumn>
<FormTableButtonRowStyle HorizontalAlign="Left" CssClass="EditFormButtonRow"></FormTableButtonRowStyle>
</EditFormSettings>
</MasterTableView>
<HeaderContextMenu CssClass="GridContextMenu GridContextMenu_Default" EnableImageSprites="True">
</HeaderContextMenu>
</telerik:RadGrid>
ASPX.CS
protected void Button1_Click(object sender, EventArgs e)
{
rdClosedLoop.DataSource = getDummyData(0);
rdClosedLoop.DataBind();
}
protected DataTable getDummyData(int verId)
{
// Note - verId is based on some conditions
DataTable dt = new DataTable();
dt.Columns.Add("ID",typeof(int));
dt.Rows.Add(1);
dt.Rows.Add(2);
dt.Rows.Add(3);
return dt;
}
protected void rdClosedLoop_ItemCommand(object sender, GridCommandEventArgs e)
{
if (e.CommandName == RadGrid.EditCommandName)
{
rdClosedLoop.DataSource = getDummyData(0);
rdClosedLoop.DataBind();
}
}
Let me know if any concern.

Rad Grid Pager With Web Api

I try using Rad Grid with web Api as data source
<telerik:RadGrid runat="server" ID="grdUsers" AllowPaging="true" AllowSorting="true"
AllowFilteringByColumn="true" PageSize="5">
<MasterTableView AutoGenerateColumns="False" DataKeyNames="Id" ClientDataKeyNames="Id,PasswordHash">
<PagerStyle Mode="NumericPages" AlwaysVisible="true" />
<Columns>
<telerik:GridImageColumn DataType="System.String" DataImageUrlFields="Image" AlternateText="User image"
UniqueName="Image"
ImageAlign="Middle" ImageHeight="50px" ImageWidth="50px" AllowFiltering="false" HeaderText="">
</telerik:GridImageColumn>
<telerik:GridBoundColumn DataField="UserName" HeaderText="User Name" UniqueName="UserName"
DataType="System.String">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="FullName" HeaderText="Name" UniqueName="FullName"
DataType="System.String">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Email" HeaderText="Email" UniqueName="Email"
DataType="System.String">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="RegistrationDate" HeaderText="Registration Date" UniqueName="RegistrationDate"
DataFormatString="{0:dd/MM/yyyy}">
</telerik:GridBoundColumn>
<telerik:GridButtonColumn UniqueName="btnEdit" ButtonType="PushButton" Text="Edit" CommandName="Edit"></telerik:GridButtonColumn>
<telerik:GridButtonColumn UniqueName="btnDelete" ButtonCssClass="del" ButtonType="PushButton" Text="Delete" CommandName="Delete"></telerik:GridButtonColumn>
</Columns>
</MasterTableView>
<ClientSettings>
<Selecting AllowRowSelect="True" />
<ClientEvents OnCommand="RadGridCommand" />
<DataBinding Location="/SecuHostapi/Security/User/GetAll" ResponseType="JSON">
<DataService TableName="SecuHostUser" Type="OData" />
</DataBinding>
</ClientSettings>
</telerik:RadGrid>
Wep Api code
[HttpGet]
public List<SecuHostUser> GetAll(ODataQueryOptions<SecuHostUser> options)
{
UsersRep userRep = new UsersRep();
return userRep.GetAll();
}
although, I have put PageSize="5", the final result show all the data from the data base
This problem can be solved by returning a PageResult instead of a list.
[HttpGet]
public PageResult GetAll(ODataQueryOptions<SecuHostUser> options)
{
UsersRep userRep = new UsersRep();
//return userRep.GetAll();
var users = userRep.GetAll().AsQueryable();
var results = options.ApplyTo(users);
return new PageResult<SecuHostUser>(results as IEnumerable<SecuHostUser>,
Request.GetNextPageLink(), Request.GetInlineCount());
}

radio button in gridview always returns checked = false inside ajax updatepanel

I am using UpdatePanel and Ajax Tabcontainer. I have one item template column which has both checkbox and radiobutton. Either one of them is visible at a time based on one bit kind of field.
<asp:GridView ID="gvAutoMatchFund" runat="server" AutoGenerateColumns="False" ClientIDMode="Static"
AllowPaging="True" PageSize="50" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None"
OnRowDataBound="gvAutoMatchFund_RowDataBound" BorderWidth="1px" CellPadding="3"
onpageindexchanging="gvAutoMatchFund_PageIndexChanging"
Width="100%">
<Columns>
<asp:BoundField DataField="DYNAMO_FUNDNAME" HeaderText="Dynamo Fund Name" ItemStyle-Width="25%" />
<asp:BoundField DataField="DYNAMO_FUNDID" HeaderText="Dynamo Fund ID" ItemStyle-Width="25%" />
<asp:BoundField DataField="INVESTRAN_FUNDNAME" HeaderText="Investran Fund Name" ItemStyle-Width="25%" />
<asp:BoundField DataField="INVESTRAN_SYSTEMFUNDID" HeaderText="Inv. Sys. Fund ID" />
<asp:TemplateField ItemStyle-Width="80px" ItemStyle-HorizontalAlign="Center">
<HeaderTemplate>
<asp:CheckBox ID="chkSelAll" Text="Select" runat="server" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkSel" runat="server" Visible='<%#(!(bool)Eval("ISMULTIPLE"))%>' />
<asp:RadioButton ID="rdoSel" runat="server" Visible='<%#((bool)Eval("ISMULTIPLE"))%>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#C6E5F5" Font-Bold="True" ForeColor="#4695BD" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E" />
<EmptyDataRowStyle ForeColor="Maroon" />
</asp:GridView>
I am populating it after checking postback
if (!Page.IsPostBack)
{
...
PopulateMyGV();
}
When a button is clicked I am trying to loop thru each item in this gridview and using FindControl method to find checkbox and radiobutton. Upto this point is works however radiobutton checked is always false even if it is selected. However checkbox works fine(gives correct value for checked property) if selected or not. See code below
In button click
foreach (GridViewRow gvr in gvAutoMatchFund.Rows)
{
CheckBox chkSel = (CheckBox)gvr.FindControl("chkSel");
RadioButton rdoSel = (RadioButton)gvr.FindControl("rdoSel");
if (chkSel != null)
{
if (chkSel.Checked)
{
...
}
}
if (rdoSel != null)
{
if (rdoSel.Checked)
{
...
}
}
}
What am I missing?
After spending lot of hours, finally we found that this strange behavior occurs because we were changing the ID of radio button programmatically in RowDataBound event. See the code below.
protected void gvAutoMatchFund_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
USP_MDM_SUGGESTED_AUTOMATCH_FUNDResult objUSP_MDM_SUGGESTED_AUTOMATCH_FUNDResult = (USP_MDM_SUGGESTED_AUTOMATCH_FUNDResult)e.Row.DataItem;
RadioButton rdoSel = (RadioButton)e.Row.FindControl("rdoSel");
rdoSel.ID = "rdoSel_" + objUSP_MDM_SUGGESTED_AUTOMATCH_FUNDResult.DYNAMO_FUNDID
+ "_" + objUSP_MDM_SUGGESTED_AUTOMATCH_FUNDResult.INVESTRAN_SYSTEMFUNDID
+ "_" + objUSP_MDM_SUGGESTED_AUTOMATCH_FUNDResult.INVESTRAN_FUNDID;
}
}
So I removed above logic and used following logic instead to pass various parameters along with radiobutton
protected void gvAutoMatchFund_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
USP_MDM_SUGGESTED_AUTOMATCH_FUNDResult objUSP_MDM_SUGGESTED_AUTOMATCH_FUNDResult = (USP_MDM_SUGGESTED_AUTOMATCH_FUNDResult)e.Row.DataItem;
RadioButton rdoSel = (RadioButton)e.Row.FindControl("rdoSel");
string sValue = "rdoSel_" + objUSP_MDM_SUGGESTED_AUTOMATCH_FUNDResult.DYNAMO_FUNDID
+ "_" + objUSP_MDM_SUGGESTED_AUTOMATCH_FUNDResult.INVESTRAN_SYSTEMFUNDID
+ "_" + objUSP_MDM_SUGGESTED_AUTOMATCH_FUNDResult.INVESTRAN_FUNDID;
rdoSel.Attributes.Add("value", sValue);
}
}
That solved the both issues I described above.

RadComboBox in a RadGrid and getting the unique row

I have a radgrid with a radcombobox in each row. I want to get the row's ID after the combo box has been chosen (someone selecting a value in the drop down). I'm using the onitemcreated property of the radgrid to get my method called in code behind. However, I'm not able to read the value of the ID that belongs to the row in which the chosen combo box belongs to. Can anyone provide any suggestions. I have attempted all of Telerik's samples but I'm not getting good results.
As you see in my code below I have a radbutton in the gridtemplatecolumn and this works as expected. When I'm in debug I get &nbsp for the value to intID which is not a result I would expect after I select a value from the combo box.
Here is all of my code:
HTML markup:
<telerik:RadGrid ID="rdg1"
runat="server"
ItemStyle-Wrap="false"
TabIndex="1000"
GridLines="Horizontal"
BorderColor="#738BA4"
BorderWidth="1px"
OnNeedDataSource="ds1"
OnItemDataBound="oidb1"
AutoGenerateColumns="False"
OnItemCommand = "oicommand1"
EnableLinqExpressions="true"
OnItemCreated="oicreated1">
<MasterTableView DataKeyNames="ID"
HorizontalAlign="Center"
HeaderStyle-BorderWidth="0"
ItemStyle-BorderWidth="2"
ItemStyle-BorderColor="#738BA4"
FooterStyle-BorderWidth="0"
BorderWidth="0">
<CommandItemSettings ExportToPdfText="Export to Pdf" />
<RowIndicatorColumn FilterControlAltText="Filter RowIndicator column">
</RowIndicatorColumn>
<ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column">
</ExpandCollapseColumn>
<Columns>
<telerik:GridBoundColumn DataField="ID"
ItemStyle-Font-Size="8"
UniqueName="ID"
Visible="false">
<HeaderStyle Wrap="False" />
<ItemStyle Font-Size="8pt" Wrap="False" />
</telerik:GridBoundColumn>
<telerik:GridTemplateColumn ItemStyle-Font-Size="8" HeaderText="Level" UniqueName="Level">
<ItemTemplate>
<telerik:RadComboBox ID="rdcb1" runat="server" AutoPostBack="true"></telerik:RadComboBox>
</ItemTemplate>
<HeaderStyle Wrap="False" />
<ItemStyle Font-Size="8pt" Wrap="False" />
</telerik:GridTemplateColumn>
<telerik:GridBoundColumn ItemStyle-Font-Size="8"
DataField="Name"
UniqueName="Name"
Visible="true"
HeaderText="Name">
<HeaderStyle Wrap="False" />
<ItemStyle Font-Size="8pt" Wrap="true" />
</telerik:GridBoundColumn>
<telerik:GridTemplateColumn>
<ItemTemplate>
<asp:ImageButton ID="imgbtn" runat="server" ImageUrl="/Images/Delete-Small.PNG" />
</ItemTemplate>
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</telerik:GridTemplateColumn>
</Columns>
<EditFormSettings>
<EditColumn FilterControlAltText="Filter EditCommandColumn column">
</EditColumn>
</EditFormSettings>
<ItemStyle BorderColor="#738BA4" BorderWidth="2px" />
<AlternatingItemStyle />
<HeaderStyle BorderWidth="0px" />
<FooterStyle BorderWidth="0px" />
</MasterTableView>
<ItemStyle Wrap="False" />
<FilterMenu EnableImageSprites="False">
</FilterMenu>
<HeaderContextMenu CssClass="GridContextMenu GridContextMenu_Default">
</HeaderContextMenu>
</telerik:RadGrid>
</asp:Panel>
Code behind:
protected void ds1(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e) {
var Role = (from r in db.Role
select new { r.ID, r.Name });
rdg1.DataSource = Role;
Level = Role.Count();
_dataTableLevel.Clear();
_dataTableLevel.Columns.Add("Level");
for (int i = 1; i <= Level; i++) {
DataRow drLevel = _dataTableLevel.NewRow();
drLevel["Level"] = i;
_dataTableLevel.Rows.Add(drOrderLevel);
}
protected void oidb1(object sender, GridItemEventArgs e) {
if (e.Item is GridDataItem) {
GridDataItem Item1 = (GridDataItem)e.Item;
(Item1.FindControl("rdcb1") as RadComboBox).DataValueField = "Level";
(Item1.FindControl("rdcb1") as RadComboBox).DataTextField = "Level";
(Item1.FindControl("rdcb1") as RadComboBox).DataSource = _dataTableLevel.DefaultView;
(Item1.FindControl("rdcb1") as RadComboBox).DataBind();
}
}
protected void oicommand1(object sender, GridCommandEventArgs e)
{
if (e.Item is GridDataItem)
{
GridDataItem Item2 = (GridDataItem)e.Item;
TableCell ID = Item2["ID"] as TableCell;
int intID = Convert.ToInt32(ID.Text);
var deleteRole = (from r in db.Role
where r.ID == intID).First();
db.Role.DeleteObject(deleteRole);
db.SaveChanges();
rdg1.Rebind();
}
}
protected void oicreated1(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
GridDataItem Item3 = (GridDataItem)e.Item;
TableCell ID = Item3["ID"] as TableCell;
string ID = ID.Text;
}
}
The guys at Telerik provided the solution below. The solution by Telerik works.
Thanks.
Thank you for contacting us.
If you want go get the row's ID when you change the selected index of particular combobox, my suggestion is to subscribe on the service-side OnSelectedIndexChanged event and use the following implementation of the event handling function:
protected void OnSelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
var combobox = sender as RadComboBox;
GridDataItem dataItem = combobox.Parent.Parent as GridDataItem;
var text = dataItem["ID"].Text;
}
I hope this would help you out.
Kind regards,
Dimitar Terziev
the Telerik team
your solution works but there is another approach with DataKeyName of RadGrid, if somebody does not ID in columns
protected void RadComboBox1_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
var combobox = sender as RadComboBox;
GridDataItem dataItem = combobox.Parent.Parent as GridDataItem;
string id = dataItem.OwnerTableView.DataKeyValues[dataItem.ItemIndex]["Id"].ToString();
}

Resources