Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to add template control programmatically

Status
Not open for further replies.

citizenzen

Programmer
Jun 28, 2007
102
US
I created a customized gridview. i added my major columns and I need to add a templatefield. i want to include a button in the template field, but everything i try seems to fail. how can i programmatically add a button?

I get the following error:
CS1502: The best overloaded method match for 'System.Web.UI.WebControls.GridViewRowCollection.this[int]' has some invalid arguments

I have the following code:

Code:
TemplateField tf1 = new TemplateField();
BoundField ap1 = new BoundField();
BoundField ap2 = new BoundField();

Button mylocation;
mylocation = (Button)testDetailsGV.Rows[mrow.FindControl("locateBtn")];

tf1.ItemTemplate = mylocation;
                    tf1.HeaderText = "Locate";
                    tf1.Visible = true;         

                    ap1.DataField = "DetailsID";
                    ap1.HeaderText = "#";

                    ap2.DataField = "Name";
                    ap2.HeaderText = "User";

testDetailsGV.Columns.Add(tf1);
                    testDetailsGV.Columns.Add(ap1);
                    testDetailsGV.Columns.Add(ap2);
 
The easiest way to assign an ITemplate to the ItemTemplate is to first create a user control, then load it into your ItemTemplate:

Code:
tf1.ItemTemplate = Page.LoadTemplate("/MyControl.ascx");

You can create an ITemplate via code, but it's more work and you don't have VS designer support.

MCP, MCTS - .NET Framework 2.0 Web Applications
 
ok. that works fine. however, I need to add functionality that button. the functionality needs to reference datgrids on my main page and then it builds a datagrid. when I add that functionality, i get the error that the datagrid does not exist. is there a way around this?

 
i added page.LoadTemplate("locateMedia.ascx"); unfortunately, it appears when the user arrives at the grid which contains this control. however, when I click on the button, it disappears!

Here are my questions:

1) is the control like an include, if so, why aren't my grids and other pieces recognized???

2) I am trying to capture the data from this grid (TestDetailsGV). The results affect the output for the grid that the button actually builds. So, i put created a session and performed a few tests. nothing happens when i click on the button that is a part of the itemtemplate.
In fact, the button is initially present, but disappers when I click it.

3) how would i carry over the data from the grid that the templated button lies. it seems like i have less control over what I can do.

page1.aspx
Code:
BoundField ap1 = new BoundField();
BoundField ap2 = new BoundField();
BoundField ap3 = new BoundField();

TemplateField tf1 = new TemplateField();
tf1.ItemTemplate = Page.LoadTemplate("locateMedia.ascx");
tf1.HeaderText = "Find";

testDetailsGV.Columns.Add(ap1);
testDetailsGV.Columns.Add(ap2);
testDetailsGV.Columns.Add(tf1);

//Session.Add("foundData", mydt); 
testDetailsGV.DataBind();

testEntry();
//use this to make the button visible at all times

private void testEntry()
{
//lastbutton = (Button)taskListGrid.Rows[lastindex].FindControl("nextBtn");
//Button mobile;
string mobile = (string)testDetailsGV.Rows[0].Cells[13].ToString();
}


page2.ascx (testing)

Code:
public partial class locateMedia : System.Web.UI.UserControl
{
    //DataTable dtf; uncomment if using a session

    protected void locateBtn_Click(object sender, EventArgs e)
    {
        Response.Write("Testing");
        locateBtn.Visible = true;
    }
}

thanks in advance!
 
Looking at this briefly, I need to ask - what event are you loading your

Code:
page.LoadTemplate("locateMedia.ascx");

code? You need to load these every time the page loads, not just in the !Postback, so try moving that code to the Page_Init event to start.
 
Also, there are a few ways to handle what you're doing, but it depends on deeper requirements.

If all you need to to is run code when the button is clicked, but you rely on a certain row value for the code block, then the appropriate solution is simply to bind a field to the CommandArgument property of the button, and have code within the User Control to handle the Command event of the button.

Code:
<asp:Button ID="btn" runat="server" Text="Click Me" CommandArgument="<%# Eval("FieldName") %>" OnCommand="UserControlHandler" />

MCP, MCTS - .NET Framework 2.0 Web Applications
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top