Can you please tell me the view state for the control is enble or not? If not then enable it.
If this post has helped you then please mark as Answer. It will helpful for other users to find Thread easily. Thanks & Regards,
Anand Mani Tiwari
Software Engineer.
Hi Steve, yes is related. I did a custom insert page. I copied default insert page and configured datasource to get the formview controls name . In my page init I did this:
In my page init I did this:
string license;
if (Request.Form["User_Name"] != null)
license = Request.Form["User_Name"].ToString();
TextBox txt = this.Form.FindControl("User_Name") as TextBox;
But both, license and txt, get null values. I've been reading and I know all values of a form controls are gotten into a collection but, how it can be extracted from that collection?
This is because the actual controls are DynamicControl have a look at these extension methods
/// <summary>
/// Get the control by searching recursively for it.
/// </summary>
/// <param name="Root">The control to start the search at.</param>
/// <param name="Id">The ID of the control to find</param>
/// <returns>The control the was found or NULL if not found</returns>
public static Control FindControlRecursive(this Control Root, string Id)
{
if (Root.ID == Id)
return Root;
foreach (Control Ctl in Root.Controls)
{
Control FoundCtl = FindControlRecursive(Ctl, Id);
if (FoundCtl != null)
return FoundCtl;
}
return null;
}
/// <summary>
/// Finds the control recursive.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="root">The root.</param>
/// <returns></returns>
public static T FindControlRecursive<T>(this Control root) where T : Control
{
var control = root as T;
if (control != null)
return control;
foreach (Control Ctl in root.Controls)
{
T FoundCtl = Ctl.FindControlRecursive<T>();
if (FoundCtl != null)
return FoundCtl;
}
return null;
}
You should be able to use one of these to fine your control.
See my blog C# Bits | Twitter @sjnaughton Always seeking an elegant solution.
public static class
ControlHelper
{ public static Control FindControlRecursive(this Control Root,
string id)
{ if (Root.ClientID.IndexOf(id)>0)
{
return Root;
}
foreach (Control Ctl
in Root.Controls)
{ Control FoundCtl = FindControlRecursive(Ctl, id);
if (FoundCtl != null)
{ return FoundCtl;
}
}
return null;
}
}
Call from your edit.aspx (formview control on this page).
The control returned will be a DynamicControl or the actual field template you will need to cast it first and then refrence the field templates DataControl property and then cast that to the correct control type.
See my blog C# Bits | Twitter @sjnaughton Always seeking an elegant solution.
lgarciap
Member
28 Points
20 Posts
extract controls values from the formview
Sep 19, 2011 07:07 AM|LINK
Hello everyone.
I need to get the values of insert.aspx controls because I need to insert some of the values on another table. I tried doing this:
MetalAsp.Net
All-Star
112032 Points
18231 Posts
Moderator
Re: extract controls values from the formview
Sep 19, 2011 07:11 AM|LINK
It depends on a lot of things, but generally speaking:
TextBox name = FormView1.FindControl("User_Name") as TextBox; if (name != null) { // Use name.Text here, for example... }Anand Mani T...
Member
720 Points
138 Posts
Re: extract controls values from the formview
Sep 19, 2011 07:11 AM|LINK
Dear friend,
Can you please tell me the view state for the control is enble or not? If not then enable it.
Thanks & Regards,
Anand Mani Tiwari
Software Engineer.
sjnaughton
All-Star
27308 Points
5458 Posts
MVP
Re: extract controls values from the formview
Sep 19, 2011 12:12 PM|LINK
Hi lgarciap, is this relating to the Dynamic Data Framework?
Always seeking an elegant solution.
lgarciap
Member
28 Points
20 Posts
Re: extract controls values from the formview
Sep 19, 2011 03:55 PM|LINK
In my page init I did this:
string license; if (Request.Form["User_Name"] != null) license = Request.Form["User_Name"].ToString(); TextBox txt = this.Form.FindControl("User_Name") as TextBox;sjnaughton
All-Star
27308 Points
5458 Posts
MVP
Re: extract controls values from the formview
Sep 19, 2011 05:19 PM|LINK
This is because the actual controls are DynamicControl have a look at these extension methods
/// <summary> /// Get the control by searching recursively for it. /// </summary> /// <param name="Root">The control to start the search at.</param> /// <param name="Id">The ID of the control to find</param> /// <returns>The control the was found or NULL if not found</returns> public static Control FindControlRecursive(this Control Root, string Id) { if (Root.ID == Id) return Root; foreach (Control Ctl in Root.Controls) { Control FoundCtl = FindControlRecursive(Ctl, Id); if (FoundCtl != null) return FoundCtl; } return null; } /// <summary> /// Finds the control recursive. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="root">The root.</param> /// <returns></returns> public static T FindControlRecursive<T>(this Control root) where T : Control { var control = root as T; if (control != null) return control; foreach (Control Ctl in root.Controls) { T FoundCtl = Ctl.FindControlRecursive<T>(); if (FoundCtl != null) return FoundCtl; } return null; }You should be able to use one of these to fine your control.
Always seeking an elegant solution.
lgarciap
Member
28 Points
20 Posts
Re: extract controls values from the formview
Sep 20, 2011 05:11 AM|LINK
I still get null value. I tried this:
txt = Master.FindControlRecursive("User_NameTextBox") as TextBox; Control txt = FindControlRecursive(this.FormView1, "User_NameTextBox"); TextBox txt = ExtensionsClass.FindControlRecursive(this.Master, "User_NameTextBox") as TextBox;What am I doing wrong?
pratiksolank...
Member
270 Points
73 Posts
Re: extract controls values from the formview
Sep 20, 2011 08:54 PM|LINK
Hi,
the code you use can work but you need to make changes at two places. see the link below
http://forums.asp.net/post/4563604.aspx
public static class ControlHelper
{
public static Control FindControlRecursive(this Control Root, string id)
{
if (Root.ClientID.IndexOf(id)>0)
{
return Root;
}
foreach (Control Ctl in Root.Controls)
{
Control FoundCtl = FindControlRecursive(Ctl, id);
if (FoundCtl != null)
{
return FoundCtl;
}
}
return null;
}
}
Call from your edit.aspx (formview control on this page).
protected void FormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e){
Control Ctl = FormView1.FindControlRecursive("CategoryNew1_CheckBoxList1");
CheckBoxList ChkList = (CheckBoxList)Ctl;
string bookname = ChkList.SelectedItem.Text;
}
In your case you might need to write "User_Name_TextBox1". Where User_Name is your coloumn name which is uses "TextBox1" fieldTemplate.
Thanks
Pratik
lgarciap
Member
28 Points
20 Posts
Re: extract controls values from the formview
Sep 21, 2011 05:05 AM|LINK
Thank you pratiksolanki but That, doesn't worked for me either. Any other idea???
sjnaughton
All-Star
27308 Points
5458 Posts
MVP
Re: extract controls values from the formview
Sep 21, 2011 10:29 AM|LINK
The control returned will be a DynamicControl or the actual field template you will need to cast it first and then refrence the field templates DataControl property and then cast that to the correct control type.
Always seeking an elegant solution.