dcsimg
Results 1 to 24 of 24

Thread: [RESOLVED] Programatically determine bindings on a textbox

  1. #1

    Thread Starter
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051

    Resolved [RESOLVED] Programatically determine bindings on a textbox

    I have a method that takes a LINQtoSQL object and a field name and will tell me the length of that field based on the attributes of that field within the class object.

    I want to use this method to set the maxlength of any bound textboxes in a gridview.

    I can loop through all textboxes, and if the textbox created from a bound field then i can get at the fieldname ok, however if it's created from a template i can't find any way of getting to the field name the textbox is bound to (since a template isn't bound to anything).

    What i'm after is a way of getting the field name the Text property of a TextBox is bound to in (ideally) any case, not just the case where it was created from a bound field.

    Any help would be great, if you want to answer in code c# or VB.NET is fine.
    Last edited by SLH; Jul 12th, 2010 at 08:03 AM.
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  2. #2
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,966

    Re: Programatically determine bindings on a textbox

    What about the extension methods on the TextBox class:

    http://msdn.microsoft.com/en-us/libr...x_members.aspx

    Namely FindMetaTable and FindFieldTemplate. Can these not give you the information that you are after?

    Gary

  3. #3

    Thread Starter
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051

    Re: Programatically determine bindings on a textbox

    Thanks for the reply Gary.

    I've just tried to use the FindMetaTable extension method on my textbox, but i get the error:
    Code:
    The context type MyLINQtoSQLDataContext does not belong to any registered model.
    I've looked up that error, but it looks like the FindMetaTable is only use with dynamic data driven sites, or am i missing something here?
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  4. #4
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,966

    Re: Programatically determine bindings on a textbox

    In all honesty, I am not sure

    I just had a quick look at the list of members for the TextBox, with relation to DataBinding, and those two methods stood out as being quite useful.

    I will have to do some digging to see exactly when/where they can be applied.

    Gary

  5. #5

    Thread Starter
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051

    Re: Programatically determine bindings on a textbox

    I've just had a play around, turns out i need to add a few things to my global.asax file to correct the error.

    Now i can run the code calling the FindMetaTable extension method. Trouble is, as the name implies that gives details about the table it's bound to, not the column.
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  6. #6
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Programatically determine bindings on a textbox

    In the RowDataBound event, you can get the e.Row.DataItem that has data related information, as well as each cell in the row and the controls inside it (using FindControl method).
    This should be easy to do. Let me know if this much info is not sufficient and I'll try to rig up a demo for you.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." Charles F. Kettering

    Read articles on My Blog 101 LINQ Samples JSON Validator XML Schema Validator "How Do I" videos on MSDN VB.NET and C# Comparison Good Coding Practices VBForums Reputation Saver String Enum Super Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  7. #7
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,966

    Re: Programatically determine bindings on a textbox

    Pradeep,

    How does that get you the name of the underlying column that a TextBox, for instance, is bound to?

    Gary

  8. #8

    Thread Starter
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051

    Re: Programatically determine bindings on a textbox

    Thanks for the reply Paradeep, i don't think i'm explaining too well thought as you've both given good explanations of how to get the object a row is bound to, and information about it's columns, but for my purpose that's not enought.

    I don't know at compile time what textbox is bound to what property of the dataitem.

    In order to set the maxlength of a bound textbox appropriately i need to know the data the textbox is bound to, and the column/property it's bound to.

    Thanks for your patience!
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  9. #9
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Programatically determine bindings on a textbox

    Can you show the gridview aspx code? How you are binding the textbox?
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." Charles F. Kettering

    Read articles on My Blog 101 LINQ Samples JSON Validator XML Schema Validator "How Do I" videos on MSDN VB.NET and C# Comparison Good Coding Practices VBForums Reputation Saver String Enum Super Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  10. #10

    Thread Starter
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051

    Re: Programatically determine bindings on a textbox

    I'm binding in the normal way:
    Code:
    <asp:TextBox ID="TextBox5" runat="server" Text='<&#37;# Bind("Notes") %>' 
                            TextMode="MultiLine"></asp:TextBox>
    My issue is that i want to create a control derived from a gridview that will automatically set the maxlength of any bound textbox. I can get at the textboxes in the databound event (or similar), but can't find out how to get at the name of the field the text property is bound to.
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  11. #11
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Programatically determine bindings on a textbox

    OK Fine.

    So have you ever heard about Expando properties?
    I'll show you how to use it here for your purpose.

    Modify your textbox markup like this:
    Code:
    <asp:TextBox ID="TextBox5" runat="server" FieldName="Notes" Text='<&#37;# Bind("Notes") %>' 
                            TextMode="MultiLine"></asp:TextBox>
    Now in the code-behind:
    Code:
    Protected Sub gvGrid_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvGrid.RowDataBound
        Select Case e.Row.RowType
            Case DataControlRowType.DataRow
                Dim tb As TextBox = e.Row.FindControl("TextBox5")   ' or maybe use a for each loop. 
                Select Case tb.Attributes("FieldName")
                    Case "Notes" : tb.MaxLength = 255
                    Case "Field1" : tb.MaxLength = 10
                    Case "Field2" : tb.MaxLength = 15
                End Select
        End Select
    End Sub
    In the code above, I have shown you with one control only for demo. You can use the For Each.. Next loop to loop thru all the TextBoxes in the row and set their properties.
    Last edited by Pradeep1210; Jul 9th, 2010 at 10:26 AM.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." Charles F. Kettering

    Read articles on My Blog 101 LINQ Samples JSON Validator XML Schema Validator "How Do I" videos on MSDN VB.NET and C# Comparison Good Coding Practices VBForums Reputation Saver String Enum Super Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  12. #12
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,793

    Re: Programatically determine bindings on a textbox

    Pradeep - that's fine and dandy... but rather than hard-coding the lengths:
    Select Case tb.Attributes("FieldName")
    Case "Notes" : tb.MaxLength = 255
    Case "Field1" : tb.MaxLength = 10
    Case "Field2" : tb.MaxLength = 15
    End Select


    is there a way to get those legths DYNAMICALLY? I think that's what SLH is after.... getting back somehow to the column definition so that the max legth becomes driven by the database design... not by code... that way if the col is expanded in the database.... the text area will expand with it...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  13. #13

    Thread Starter
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051

    Re: Programatically determine bindings on a textbox

    I can get the lengths dynamically fine.

    I want to get the field name that a textbox is bound to without adding custom attributes to every textbox in the gridview.
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  14. #14
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,793

    Re: Programatically determine bindings on a textbox

    ?? ok.... color me confused.... if you can get to the lengths dynamically just fine.... logic would dictate that you should be able to get the name...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  15. #15
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Programatically determine bindings on a textbox

    @tg:
    Yes, you're right. The lengths should be got dynamically.
    I was just showing him a sort of proof of concept and didn't really care much about anything other than showing that the fieldname can be made available in the code.
    The way it should really be implemented is get the appropriate properties of field from the datatable and code accordingly.

    @SLH
    I hope you now have what you were looking for and would be able to continue from here yourself. If you need any further help please let me know.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." Charles F. Kettering

    Read articles on My Blog 101 LINQ Samples JSON Validator XML Schema Validator "How Do I" videos on MSDN VB.NET and C# Comparison Good Coding Practices VBForums Reputation Saver String Enum Super Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  16. #16
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,966

    Re: Programatically determine bindings on a textbox

    Okay, I have been giving this some thought, and with a BoundField, you can do something like the following:

    Code:
                int maxLength = 11;
                string forColumn = "FirstName";
    
                foreach (TableCell var in this.GridView1.Rows[0].Cells)
                {
                    BoundField bf = (var as DataControlFieldCell).ContainingField as BoundField;
                    if (bf != null)
                    {
                        if (bf.DataField == forColumn)
                        {
                            foreach (Control ctr in var.Controls)
                            {
                                if (ctr is TextBox)
                                {
                                    (ctr as TextBox).MaxLength = maxLength;
                                }
                            }
                        }
                    }
                }
    And using the DataField property, you can get information about the field that it is bound to. However, when you try and do the following:

    Code:
                foreach (TableCell var in this.GridView1.Rows[0].Cells)
                {
                    TemplateField tf = (var as DataControlFieldCell).ContainingField as TemplateField;
                    if (tf != null)
                    {
    You then don't have access to the DataField member, as this is not a Member of TemplateField.

    The best you could do is something like this:

    Code:
                foreach (TableCell var in this.GridView1.Rows[0].Cells)
                {
                    DataControlFieldCell cell = var as DataControlFieldCell;
    
                    IOrderedDictionary values = new OrderedDictionary();
                    cell.ContainingField.ExtractValuesFromCell(values, cell, this.GridView1.Rows[0].RowState, true);
                }
    Where the ExtractValuesFromCell, is used to find an OrderedDictionary whose Key, in theory, should be the name of the Cell that it is Bound to.

    I have only given this a very quick play, but you should be able to get something from it.

    Hope that helps!!

    Gary

  17. #17
    Frenzied Member brin351's Avatar
    Join Date
    Mar 2007
    Location
    Land Down Under
    Posts
    1,293

    Re: Programatically determine bindings on a textbox

    My issue is that i want to create a control derived from a gridview that will automatically set the maxlength of any bound textbox
    Is this a custom control? It sounds like it is so could you override the datasource member and dig out all the field info there and use it in the onrowdatabind to do your thing.

    Not sure if I'm on the right track here

  18. #18

    Thread Starter
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051

    Re: Programatically determine bindings on a textbox

    Quote Originally Posted by techgnome View Post
    ?? ok.... color me confused.... if you can get to the lengths dynamically just fine.... logic would dictate that you should be able to get the name...

    -tg
    Sorry, maybe i was a bit unclear.

    By saying i can get the lengths dynamically, i mean, given a field name i can get it's max length.

    The issue i have is getting the field name in the first place.

    Quote Originally Posted by paradeep
    I hope you now have what you were looking for and would be able to continue from here yourself. If you need any further help please let me know.
    I still have the issue of getting the name of a field the textbox is bound to.

    @gep13: This is basically where i was when i started the thread; i could get the field name if it was a bound field, but not a template field. Your last code listing looks promising if a bit hack-ish. I'll take a look and see if i can come up with something.


    I can't believe something this simple (get the data bindings of a control) is so complicated!!
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  19. #19
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Programatically determine bindings on a textbox

    Quote Originally Posted by SLH View Post
    The issue i have is getting the field name in the first place.

    I still have the issue of getting the name of a field the textbox is bound to.
    What exactly in post #11 is not clear or is not working?

    The tb.Attributes("FieldName") gets you the fieldname. You can use appropriate dataset/datatable methods to get the field properties from that.
    Last edited by Pradeep1210; Jul 12th, 2010 at 06:17 AM.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." Charles F. Kettering

    Read articles on My Blog 101 LINQ Samples JSON Validator XML Schema Validator "How Do I" videos on MSDN VB.NET and C# Comparison Good Coding Practices VBForums Reputation Saver String Enum Super Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  20. #20

    Thread Starter
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051

    Re: Programatically determine bindings on a textbox

    Quote Originally Posted by gep13 View Post
    The best you could do is something like this:

    Code:
                foreach (TableCell var in this.GridView1.Rows[0].Cells)
                {
                    DataControlFieldCell cell = var as DataControlFieldCell;
    
                    IOrderedDictionary values = new OrderedDictionary();
                    cell.ContainingField.ExtractValuesFromCell(values, cell, this.GridView1.Rows[0].RowState, true);
                }
    Where the ExtractValuesFromCell, is used to find an OrderedDictionary whose Key, in theory, should be the name of the Cell that it is Bound to.

    I have only given this a very quick play, but you should be able to get something from it.

    Hope that helps!!

    Gary
    I've had a play with that and it looks kind of ok, however, if the template has multiple textboxes (one of the main reasons for using a template field as opposed to a bound field) then i can't tell how to determine which textbox relates to which field.
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  21. #21

    Thread Starter
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051

    Re: Programatically determine bindings on a textbox

    Quote Originally Posted by Pradeep1210 View Post
    What exactly in post #11 is not clear or is not working?

    The tb.Attributes("FieldName") gets you the fieldname. You can use appropriate dataset/datatable methods to get the table properties from that.
    I don't want to have to add the field name to every bound textbox in every gridview control i use. I want to be able to change every instance of a gridview to my derived class and have it Just WorkTM
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  22. #22
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Programatically determine bindings on a textbox

    Quote Originally Posted by SLH View Post
    I don't want to have to add the field name to every bound textbox in every gridview control i use. I want to be able to change every instance of a gridview to my derived class and have it Just WorkTM
    You are using the fieldname once at least in the textbox markup (e.g. Text='<%# Bind("Notes") %>'). So how hard is it to use it twice?
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." Charles F. Kettering

    Read articles on My Blog 101 LINQ Samples JSON Validator XML Schema Validator "How Do I" videos on MSDN VB.NET and C# Comparison Good Coding Practices VBForums Reputation Saver String Enum Super Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  23. #23

    Thread Starter
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051

    Re: Programatically determine bindings on a textbox

    Quote Originally Posted by Pradeep1210 View Post
    You are using the fieldname once at least in the textbox markup (e.g. Text='<%# Bind("Notes") %>'). So how hard is it to use it twice?
    It's twice as hard as doing it once!

    I've got quite a few web apps already written, with many gridviews, with many fields. To change the gridiviews i can find/replace, but i'd have to change every templated textbox individually.
    Anyone else using the control would have to know do add/update this extra field as well. Obviously using the designer to change the bindings wouldn't update the field and so could easily create inconsistancies.



    I've worked a bit more with gep13's code, using reflection, and have come up with the following, where TB is the textbox and ControlFieldCell is the cell it's in:
    Code:
    var TF = ControlFieldCell.ContainingField as TemplateField;
    
    if (DataItem != null)
    {
       var EditItemTemplate = TF.EditItemTemplate as IBindableTemplate;
    
        var DataBindings = EditItemTemplate.ExtractValues(TB);
    
        int BoundFieldLength = AquaReturnsDataContext
                                .GetFieldLength(DataItem, DataBindings.Keys.Cast<string>().First());
    
        if (BoundFieldLength != 0)
            TB.MaxLength = BoundFieldLength;
    }
    I guess this won't work in all cases, (e.g. multiple bindings on different properties) however i think it'll work in the vast majority.

    To get it working in all cases i need to know the property of the textbox that the datafield is bound to (then i filter out everything except the one for the 'Text' property).
    Last edited by SLH; Jul 12th, 2010 at 06:35 AM.
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  24. #24
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,966

    Re: Programatically determine bindings on a textbox

    Sounds like you are getting somewhere

    It does raise an interesting point though, about why this isn't more accessible.

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width