I have created a radgrid that displays labels in the first column and input controls in column 2. Column 2 can have either textbox or DropDownList controls. What I can't figure out is how to get the values of the input controls when the form is submitted
I have tried the following but the XControl is always null. I would like to return the grid contents as XML if possible
foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
{
TextBox xControl = item["AttributeType"].FindControl("tb") as TextBox;
}
Any help/ideas would greatly be appreciated!!!
ASPX Markup
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ESManager.aspx.cs" Inherits="ESManager" %>
<%@ Register assembly="Telerik.Web.UI" namespace="Telerik.Web.UI" tagprefix="telerik" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server" style="height: 503px;" >
<telerik:RadScriptManager ID="RadScriptManager1" runat="server">
</telerik:RadScriptManager>
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
<AjaxSettings>
<telerik:AjaxSetting AjaxControlID="RadGrid1">
<UpdatedControls>
<telerik:AjaxUpdatedControl ControlID="SpaceType" />
</UpdatedControls>
</telerik:AjaxSetting>
<telerik:AjaxSetting AjaxControlID="SpaceType">
<UpdatedControls>
<telerik:AjaxUpdatedControl ControlID="RadGrid1" />
</UpdatedControls>
</telerik:AjaxSetting>
</AjaxSettings>
</telerik:RadAjaxManager>
<div>
</div>
<telerik:RadGrid ID="RadGrid1" runat="server" GridLines="None"
OnItemCreated="RadGrid1_ItemCreated" Width="350px" Height="200px"
style="z-index: 1; left: 137px; top: 111px; position: absolute; height: 46px; width: 366px"
AutoGenerateColumns="False" BorderStyle="None" >
<MasterTableView>
<ItemTemplate>
<%--This is required for Dynamic Creation of ItemTemplate Controls--%>
</ItemTemplate>
<Columns>
<telerik:GridBoundColumn DataField="AttributeName" UniqueName="AttributeName"
HeaderText="Space Attribute">
<HeaderStyle Width="100px" />
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="AttributeType" UniqueName="AttributeType"
HeaderText="Value">
<HeaderStyle Width="75px" />
</telerik:GridBoundColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
<asp:Button ID="Button1" runat="server" Text="Button"
style="z-index: 1; left: 516px; top: 254px; position: absolute"
onclick="Button1_Click" />
<telerik:RadComboBox ID="SpaceType" Runat="server"
style="z-index: 5; left: 273px; top: 63px; position: absolute; height: 22px; width: 160px; right: 486px;"
DataSourceID="SqlDataSource1" DataTextField="Description" DataValueField="ID"
onselectedindexchanged="SpaceType_SelectedIndexChanged"
AutoPostBack="True">
</telerik:RadComboBox>
<div style="width: 231px; height: 31px; position: absolute; top: 525px; left: 13px; z-index: 1;">
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:UserConn %>"
SelectCommand="SELECT ID,Description from SpaceTypes order by ID">
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!Code Behind!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
using System;
using System.Web.UI.WebControls;
using System.Data;
using Telerik.Web.UI;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
public partial class ESManager : System.Web.UI.Page
{
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
else
{
foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
{
TextBox xControl = item["AttributeType"].FindControl("tb") as TextBox;
}
}
}
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
try
{
if (e.Item is GridDataItem)
{
int idex = Convert.ToInt32(e.Item.ItemIndexHierarchical);
string type = ds.Tables[0].Rows[idex]["type"].ToString();
Label LabelControl = new Label();
LabelControl.ID = "l1";
LabelControl.Text = ds.Tables[0].Rows[idex].ItemArray[0].ToString();
LabelControl.Width = 200;
GridDataItem item = e.Item as GridDataItem;
item["AttributeType"].Controls.Add(LabelControl);
if (type.ToLower() == "textbox")
{
TextBox xControl = new TextBox();
xControl.ID = "tb";
xControl.Text = "";
try { xControl.ToolTip = ds.Tables[0].Rows[idex]["doc"].ToString(); }
catch { }
xControl.Width = 75;
xControl.MaxLength = 7;
xControl.ValidationGroup = "GridControls";
item["AttributeType"].Controls.Add(xControl);
RequiredFieldValidator validator = new RequiredFieldValidator();
validator.ControlToValidate = xControl.ID;
validator.ErrorMessage = "*";
}
if (type.ToLower() == "listbox")
{
DropDownList xControl = new DropDownList();
xControl.ID = "lb1";
string[] items = ds.Tables[0].Rows[idex]["items"].ToString().Split(',');
foreach (string xitem in items)
{
xControl.Items.Add(xitem);
}
xControl.SelectedValue = items[0];
xControl.ValidationGroup = "GridControls";
item["AttributeType"].Controls.Add(xControl);
}
}
}
catch (Exception ex)
{
}
}
protected void SpaceType_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["UserConn"].ToString()))
{
SqlCommand cmd = new SqlCommand("SELECT xmlTemplate from SpaceTypes where id=@ID", conn);
cmd.Parameters.AddWithValue("@ID", SpaceType.SelectedValue.ToString());
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
try
{
StringReader strReader = new StringReader("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + reader["xmlTemplate"].ToString());
ds.ReadXml(strReader, XmlReadMode.InferSchema);
RadGrid1.DataSource = ds;
RadGrid1.DataBind();
}
catch (Exception ex)
{
}
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
}
}
I have tried the following but the XControl is always null. I would like to return the grid contents as XML if possible
foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
{
TextBox xControl = item["AttributeType"].FindControl("tb") as TextBox;
}
Any help/ideas would greatly be appreciated!!!
ASPX Markup
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ESManager.aspx.cs" Inherits="ESManager" %>
<%@ Register assembly="Telerik.Web.UI" namespace="Telerik.Web.UI" tagprefix="telerik" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server" style="height: 503px;" >
<telerik:RadScriptManager ID="RadScriptManager1" runat="server">
</telerik:RadScriptManager>
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
<AjaxSettings>
<telerik:AjaxSetting AjaxControlID="RadGrid1">
<UpdatedControls>
<telerik:AjaxUpdatedControl ControlID="SpaceType" />
</UpdatedControls>
</telerik:AjaxSetting>
<telerik:AjaxSetting AjaxControlID="SpaceType">
<UpdatedControls>
<telerik:AjaxUpdatedControl ControlID="RadGrid1" />
</UpdatedControls>
</telerik:AjaxSetting>
</AjaxSettings>
</telerik:RadAjaxManager>
<div>
</div>
<telerik:RadGrid ID="RadGrid1" runat="server" GridLines="None"
OnItemCreated="RadGrid1_ItemCreated" Width="350px" Height="200px"
style="z-index: 1; left: 137px; top: 111px; position: absolute; height: 46px; width: 366px"
AutoGenerateColumns="False" BorderStyle="None" >
<MasterTableView>
<ItemTemplate>
<%--This is required for Dynamic Creation of ItemTemplate Controls--%>
</ItemTemplate>
<Columns>
<telerik:GridBoundColumn DataField="AttributeName" UniqueName="AttributeName"
HeaderText="Space Attribute">
<HeaderStyle Width="100px" />
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="AttributeType" UniqueName="AttributeType"
HeaderText="Value">
<HeaderStyle Width="75px" />
</telerik:GridBoundColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
<asp:Button ID="Button1" runat="server" Text="Button"
style="z-index: 1; left: 516px; top: 254px; position: absolute"
onclick="Button1_Click" />
<telerik:RadComboBox ID="SpaceType" Runat="server"
style="z-index: 5; left: 273px; top: 63px; position: absolute; height: 22px; width: 160px; right: 486px;"
DataSourceID="SqlDataSource1" DataTextField="Description" DataValueField="ID"
onselectedindexchanged="SpaceType_SelectedIndexChanged"
AutoPostBack="True">
</telerik:RadComboBox>
<div style="width: 231px; height: 31px; position: absolute; top: 525px; left: 13px; z-index: 1;">
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:UserConn %>"
SelectCommand="SELECT ID,Description from SpaceTypes order by ID">
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!Code Behind!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
using System;
using System.Web.UI.WebControls;
using System.Data;
using Telerik.Web.UI;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
public partial class ESManager : System.Web.UI.Page
{
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
else
{
foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
{
TextBox xControl = item["AttributeType"].FindControl("tb") as TextBox;
}
}
}
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
try
{
if (e.Item is GridDataItem)
{
int idex = Convert.ToInt32(e.Item.ItemIndexHierarchical);
string type = ds.Tables[0].Rows[idex]["type"].ToString();
Label LabelControl = new Label();
LabelControl.ID = "l1";
LabelControl.Text = ds.Tables[0].Rows[idex].ItemArray[0].ToString();
LabelControl.Width = 200;
GridDataItem item = e.Item as GridDataItem;
item["AttributeType"].Controls.Add(LabelControl);
if (type.ToLower() == "textbox")
{
TextBox xControl = new TextBox();
xControl.ID = "tb";
xControl.Text = "";
try { xControl.ToolTip = ds.Tables[0].Rows[idex]["doc"].ToString(); }
catch { }
xControl.Width = 75;
xControl.MaxLength = 7;
xControl.ValidationGroup = "GridControls";
item["AttributeType"].Controls.Add(xControl);
RequiredFieldValidator validator = new RequiredFieldValidator();
validator.ControlToValidate = xControl.ID;
validator.ErrorMessage = "*";
}
if (type.ToLower() == "listbox")
{
DropDownList xControl = new DropDownList();
xControl.ID = "lb1";
string[] items = ds.Tables[0].Rows[idex]["items"].ToString().Split(',');
foreach (string xitem in items)
{
xControl.Items.Add(xitem);
}
xControl.SelectedValue = items[0];
xControl.ValidationGroup = "GridControls";
item["AttributeType"].Controls.Add(xControl);
}
}
}
catch (Exception ex)
{
}
}
protected void SpaceType_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["UserConn"].ToString()))
{
SqlCommand cmd = new SqlCommand("SELECT xmlTemplate from SpaceTypes where id=@ID", conn);
cmd.Parameters.AddWithValue("@ID", SpaceType.SelectedValue.ToString());
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
try
{
StringReader strReader = new StringReader("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + reader["xmlTemplate"].ToString());
ds.ReadXml(strReader, XmlReadMode.InferSchema);
RadGrid1.DataSource = ds;
RadGrid1.DataBind();
}
catch (Exception ex)
{
}
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
}
}