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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you reference a control inside a datalist template?

Status
Not open for further replies.

DJVisuaL

Programmer
May 31, 2000
52
US
Hopefully this is an easy one!

I have a datalist bound to the table "items" which just lists the items.

In the footer template of the datalist I have "New Item" then a textbox and then an "add" button.

I simply want to take the text from the textbox and insert it into the table "items" thus adding a new item.

With the textbox outside on the page (not in the footer template of the datalist) this works fine but when I moved the textbox within the datalist footer it cannot find the control and I have no idea how to set the parameters for the insert because when you go to the datasource insert properties and try to get the value from a control it only lists the datalist's selected value and not any controls that are within the datalist (like my textbox in the footer)

I see in the source where the insert parameters are like this:
Code:
<InsertParameters>
   <asp:ControlParameter Name="ItemName" ControlID="TextBox1" PropertyName="Text" />
</InsertParameters>
I also see in visual studio when I select the textbox it is no longer just TextBox1 it is DataList1.HeaderandFooterTemplates.FooterTemplate.TextBox1
But if i set the ControlID to this it gives an error like:
Exception Details: System.InvalidOperationException: Could not find control 'DataList1.HeaderandFooterTemplates.FooterTemplate.TextBox1' in ControlParameter 'ItemName'.

I know there has to be a way but I am stumped. Any help is appreciated!
 
In the RowDataBound event of the datalist, check if you are accessing the footer. If you are, then get a reference to the button using .findcontrol().
 
I am not accessing the footer anywhere yet.
I am trying to set the insert paramter "@ItemName" to TextBox1.Text which is in the datalist's footer template

I tryed this in my page load but also doesn't work:
Code:
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlDataSource1.InsertParameters.Add("@ListName", DataList1.FindControl("TextBox1").ID);
    }

I get this error:
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

also tried the previous with .ClientID instead
 
Is the datalist bound at that point? If not, the textbox won't exist and you will get the NULL reference error.
 
The DataList is bound but the textbox at the bottom is not bound to anything. Maybe this is my problem? Basically it looks like this:

ITEMS
-----
apples
bananas
oranges
etc..
-----
Add new item <textbox here> <add button here>

table name is Items and there are only 2 fields ItemID and ItemName

I want to set a parameter where ItemName = @ItemName to TextBox1.Text so when I push the add button I can do SqlDataSource1.Insert();

hope this helps clarify
 
You will have to get a reference to the textbox in the ItemDataBound event of the DataList as I said before. You are trying to do it in the PageLoad which will cause the error you are getting.
Code:
    Protected Sub DataList1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles DataList1.ItemDataBound
        If e.Item.ItemType = ListItemType.Footer Then
            'Find your textbox
            Dim tb As New TextBox
            tb = e.Item.FindControl("TextBox1")
            'Do other stuff here for your SqlDataSource
        End If
    End Sub
 
OK grand!
I understand this part now, thanks.

Now I have an insert query tied to SqlDataSource1 as such:

INSERT INTO Items(ItemName) VALUES (@ItemName)

so after 'Do other stuff here for your SqlDataSource I have

SqlDataSource1.InsertParameters.Add("ItemName", tb.Text);

I run the page and type something in TextBox1 and hit the Add button (Button1) and it gives me this error:

Cannot insert the value NULL into column 'ItemName', table '<table name.mdf>'; column does not allow nulls. INSERT fails.
The statement has been terminated.

The field doesn't allow NULLS but it shouldn't be null because I typed something in!
 
I don't use the datasoure objects for DB actions, so I may not be able to help much there. What is the code you are using to call the INsert?
 
OK thanks for your help so far. I think I am finally learning how things are done but I still have much to learn.

I haven't written any code to do the insert I have only set the Insert Query property of the DataList to

INSERT INTO Items(ItemName) VALUES (@ItemName)

so as I understand when SqlDataSource1.Insert() is called it will do the insert for you ASSUMING you have @ItemName set to something. So this part is working. It is trying to INSERT but my way of setting the @ItemName parameter is flawed in some way.

I'm going to search some more. At least this is a different problem and I'm making some progress.
 
Please take a look at this once again...
Now I am back to my original question.

I know how to set the parameters and have tested this and it works.

In my button code I can insert a static value into my table like this:
Code:
protected void Button1_Click(object sender, EventArgs e)
{
    SqlDataSource1.InsertParameters["ItemName"].DefaultValue = "new item test";
    SqlDataSource1.Insert();                
}
OK so my question is how do I get the text from the textbox and put it where "new item test" is? The textbox (TextBox1) and Button1 are both inside the datalist footer template.

I tryed this:
Code:
protected void Button1_Click(object sender, EventArgs e)
{
    TextBox tb = new TextBox();
    tb = (TextBox)Datalist1.FindControl("TextBox1");
    SqlDataSource1.InsertParameters["ItemName"].DefaultValue = tb.Text;
    SqlDataSource1.Insert();                
}

and got this error on line 26:
Code:
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 


Line 24:         TextBox tb = new TextBox();
Line 25:         tb = (TextBox)DataList1.FindControl("TextBox1");
Line 26:         SqlDataSource1.InsertParameters["ItemName"].DefaultValue = tb.Text;
Line 27:         SqlDataSource1.Insert();                
Line 28:     }
I know you said before that I have to get a reference from within the footer template itself in the ItemBound method. How do I make that reference accessible by the button1_click method?
If I try this:
Code:
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Footer)
    {
        //Find insert textbox
        TextBox tb = new TextBox();
        tb = (TextBox)e.Item.FindControl("TextBox1");
    }
}
protected void Button1_Click(object sender, EventArgs e)
{
        SqlDataSource1.InsertParameters["ItemName"].DefaultValue = tb.Text;
		  SqlDataSource1.Insert();
}
I get this error:
Code:
CS0103: The name 'tb' does not exist in the current context

Sorry for my noobness.. I know there has to be a way to do this. You would think since the controls are in the same place that one could access the other easily but I guess not in ASP.NET.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top