locked
Auto fill data on textchanged event of 1 field while insert RRS feed

  • Question

  • User-2046426286 posted

    Can you please help me in dynamic data model. in dynamic data model when i used to insert data in database. I have a 4 textbox in the form.

    So on the basis of first textbox remainig textbox should be fill and after click on insert data should be insert in database.

    ex : first Textbox is for UserID, so on basis of this UderID, FirstName, LastName, EmailID should be display & and insert.

    So please tell me is there any textchage event or other option for this solution.

    Please help me out in this situation....

    Thanks,

    Suraj 

    Tuesday, May 8, 2012 3:53 PM

Answers

  • User350138131 posted

    Hi to All.

    I act in such a way, if I understood correctly.

    [AttributeUsage(AttributeTargets.Property)]
    public class AutoPostBackAttribute : Attribute
    {
    	public Boolean Yes { get; private set; }
    	public AutoPostBackAttribute(Boolean yes)
    	{
    		Yes = yes;
    	}
    	// this will allow us to have a default set to false
    	public static AutoPostBackAttribute Default = new AutoPostBackAttribute(false);
    }
    
    [AttributeUsage(AttributeTargets.Property)]
    public class SaveFilterValueAttribute : Attribute
    {
    	public String Key { get; private set; }
    	public SaveFilterValueAttribute(String key)
    	{
    		Key = key;
    	}
    	// this will allow us to have a default set to false
    	public static SaveFilterValueAttribute Default = new SaveFilterValueAttribute(null);
    }
    <asp:TextBox ID="TextBox1" runat="server" Text='<%# FieldValueEditString %>' 
    	OnPreRender="TextBox1_PreRender" ontextchanged="TextBox1_TextChanged"></asp:TextBox>
    
    public partial class Text_EditField : ChildChangingAwareFieldTemplate
    {
    	protected void Page_Init(object sender, EventArgs e)
    	{
    		var attr=Column.GetAttribute<AutoPostBackAttribute>();
    		if (attr!=null)
    		{
    			TextBox1.AutoPostBack = attr.Yes;
    		}
    	}
    
    	protected void TextBox1_PreRender(object sender, EventArgs e)
    	{
    		if (IsPostBack)
    		{
    			string key = Column.Attributes.OfType<SaveFilterValueAttribute>().DefaultIfEmpty(new SaveFilterValueAttribute(null)).First().Key;
    			if (key != null)
    			{
    				if (Session[key] != null)
    					TextBox1.Text = Session[key].ToString();
    				Session[key] = null;
    			}
    		}
    	}
    
    	protected void TextBox1_TextChanged(object sender, EventArgs e)
    	{
    		System.Reflection.MethodInfo method;
    		Type dct = Column.Table.DataContextType;
    		method = dct.GetMethod("POST_" + Column.Table.Name+"_"+Column.Name);
    		IQueryable ret = null;
    		if (method != null)
    		{
    			System.Reflection.ConstructorInfo constuct = dct.GetConstructor(new Type[] { });
    			DataContext dc = (DataContext)constuct.Invoke(null);
    			object[] prm = new object[] { TextBox1, Column.Table, true };
    			method.Invoke(dc, prm);
    		}
    	}
    }
    public class YourTableName_MD
    {
    	[AutoPostBack(true)]
    	public object UserId{ get; set; }
    
    	[SaveFilterValue("firstName")]
    	public object FirstName { get; set; }
    
    	[SaveFilterValue("lastName")]
    	public object LastName { get; set; }
    }
    
    public partial class YourDataContext
    {
    	public void POST_YourTableName_UserID(TextBox textBox, MetaTable table, bool isField)
    	{
    		var row = this.YorTable.Single(t => t.UserId == textBox.Text);
    		if (row != null)
    		{
    			HttpContext.Current.Session["firstName"] = row.FirstName;
    			HttpContext.Current.Session["lastName"] = row.LastName;
    		}
    	}
    }

    Regards.


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 10, 2012 6:28 AM

All replies

  • User-330204900 posted

    Hi Suraj, if you are happy for these values to be filled in after you click insert then there are a couple of methods you can use.

    1. You can use the FormViews Inserting event - not very DRY as you need to do this on a custom page and I try to avoid custom pages except where essential, but this is an easy method,
    2. handle this in the data model (very DRY) you can do this in Entity Frmaework and Linq to SQL.
    Wednesday, May 9, 2012 4:20 AM
  • User-2046426286 posted

    Hi,

    Actually i dont want to do like that.

    I want all the information ontextchage event of first Textbox which is UserID, then all remaining Textbox should be fill related with this UserID.

    after that i have to insert all the information in database.

    Wednesday, May 9, 2012 2:34 PM
  • User-330204900 posted

    there is no event for the change of the text box sorry. each control is in a field template so perhaps DD is not for you :(

    Wednesday, May 9, 2012 2:38 PM
  • User3866881 posted

    I want all the information ontextchage event of first Textbox which is UserID, then all remaining Textbox should be fill related with this UserID.

    Hi:)

    I think you have two choices:

    1)Maybe you can do that in the general ASP.NET WebForm instead of using DD,so you can drag and drop a TextBox onto the specific position and then handle the event of TextBox_Changed(Make your TextBox's AutoPostBack = true,and then put it into the updatepanel to avoid its refreshing the whole page……)

    2)Also if possible,I think you can use js or jQuery to handle the event of changed to deal with the problem(It will do without refreshing……)。

    Wednesday, May 9, 2012 9:41 PM
  • User350138131 posted

    Hi to All.

    I act in such a way, if I understood correctly.

    [AttributeUsage(AttributeTargets.Property)]
    public class AutoPostBackAttribute : Attribute
    {
    	public Boolean Yes { get; private set; }
    	public AutoPostBackAttribute(Boolean yes)
    	{
    		Yes = yes;
    	}
    	// this will allow us to have a default set to false
    	public static AutoPostBackAttribute Default = new AutoPostBackAttribute(false);
    }
    
    [AttributeUsage(AttributeTargets.Property)]
    public class SaveFilterValueAttribute : Attribute
    {
    	public String Key { get; private set; }
    	public SaveFilterValueAttribute(String key)
    	{
    		Key = key;
    	}
    	// this will allow us to have a default set to false
    	public static SaveFilterValueAttribute Default = new SaveFilterValueAttribute(null);
    }
    <asp:TextBox ID="TextBox1" runat="server" Text='<%# FieldValueEditString %>' 
    	OnPreRender="TextBox1_PreRender" ontextchanged="TextBox1_TextChanged"></asp:TextBox>
    
    public partial class Text_EditField : ChildChangingAwareFieldTemplate
    {
    	protected void Page_Init(object sender, EventArgs e)
    	{
    		var attr=Column.GetAttribute<AutoPostBackAttribute>();
    		if (attr!=null)
    		{
    			TextBox1.AutoPostBack = attr.Yes;
    		}
    	}
    
    	protected void TextBox1_PreRender(object sender, EventArgs e)
    	{
    		if (IsPostBack)
    		{
    			string key = Column.Attributes.OfType<SaveFilterValueAttribute>().DefaultIfEmpty(new SaveFilterValueAttribute(null)).First().Key;
    			if (key != null)
    			{
    				if (Session[key] != null)
    					TextBox1.Text = Session[key].ToString();
    				Session[key] = null;
    			}
    		}
    	}
    
    	protected void TextBox1_TextChanged(object sender, EventArgs e)
    	{
    		System.Reflection.MethodInfo method;
    		Type dct = Column.Table.DataContextType;
    		method = dct.GetMethod("POST_" + Column.Table.Name+"_"+Column.Name);
    		IQueryable ret = null;
    		if (method != null)
    		{
    			System.Reflection.ConstructorInfo constuct = dct.GetConstructor(new Type[] { });
    			DataContext dc = (DataContext)constuct.Invoke(null);
    			object[] prm = new object[] { TextBox1, Column.Table, true };
    			method.Invoke(dc, prm);
    		}
    	}
    }
    public class YourTableName_MD
    {
    	[AutoPostBack(true)]
    	public object UserId{ get; set; }
    
    	[SaveFilterValue("firstName")]
    	public object FirstName { get; set; }
    
    	[SaveFilterValue("lastName")]
    	public object LastName { get; set; }
    }
    
    public partial class YourDataContext
    {
    	public void POST_YourTableName_UserID(TextBox textBox, MetaTable table, bool isField)
    	{
    		var row = this.YorTable.Single(t => t.UserId == textBox.Text);
    		if (row != null)
    		{
    			HttpContext.Current.Session["firstName"] = row.FirstName;
    			HttpContext.Current.Session["lastName"] = row.LastName;
    		}
    	}
    }

    Regards.


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 10, 2012 6:28 AM
  • User-2046426286 posted

    Hi valz,

    Really very thanks to you..

    You provided such a very nice example.  It helps me a lot, Now i have solved my problem.

    Thanks,

    Suraj

    Thursday, May 10, 2012 2:14 PM
  • User350138131 posted

    Hi, Suraj. I m glad I could help.

    I use a similar technique when it is necessary to specifically fill ComboBox for a ForeignKeyColumn. It really helps.

    Good luck.

    Thursday, May 10, 2012 11:54 PM