Link to home
Start Free TrialLog in
Avatar of riskyricky1972
riskyricky1972

asked on

asp.net c#

I have detailview in a page, and inside of detailview I create templateitem and add textbox and button there...How can I call the value from the textbox?

This is aspx c#
Avatar of McExp
McExp
Flag of United Kingdom of Great Britain and Northern Ireland image

I'm guessing your trying to access the value from code behind?

If So you can use the helper function at the end of this post to get a reference to the control in the template, just call as follows: -

TextBox myTextBox = FindControl(DetailsView1,"textBoxId");

    /// <summary>
    /// Returns a control if one by that name exists in the hierarchy of the controls collection of the start control
    /// </summary>
    /// <param name="start"></param>
    /// <param name="id"></param>
    /// <returns></returns>
    public static Control FindControl(Control start, string id)
    {
        Control foundControl;

        if (start != null)
        {
            foundControl = start.FindControl(id);

            if (foundControl != null)
                return foundControl;

            foreach (Control c in start.Controls)
            {
                foundControl = FindControl(c, id);
                if (foundControl != null)
                    return foundControl;
            }
        }
        return null;
    }
easy way without looops...
when you ASPX page loads,...check the VIEW --> Source from your browser, see the name of the text box that you are adding...it will be norammly a appended string with your details view...just use the name as findControls(thename) which wil lretun the object to you, then box it to a text box like this

textbox txt = (textbox)Findcontrols(the contorlname);
pardon me for an syntax errors in the code
The soluition suggested by the previous poster will probably fail for two reasons you may find that you cant actulally find the control, see quote from msdn site: -

"The FindControl method can be used to access a control whose ID is not available at design time. The method searches only the page's immediate, or top-level, container; it does not recursively search for controls in naming containers contained on the page. To access controls in a subordinate naming container, call the FindControl method of that container."

and the "Find control" command expects the server ID of the control not the Client ID as suggested.
Avatar of riskyricky1972
riskyricky1972

ASKER

McExp: Can U help?
Was my first solution not clear enough?

What sort of further guidance are you looking for?
ASKER CERTIFIED SOLUTION
Avatar of McExp
McExp
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial