473,326 Members | 2,337 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

DataList + FindControl()

How to do it:

My page:

<asp:DataList ID="dataListRoleMembers" ...>
....
<FooterTemplate>
<asp:LinkButton ID="btnAddMember" runat="server"
OnClick="btnAddMember_Click">Add...</asp:LinkButton>
<asp:TextBox ID="txtAddMember" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:DataList>

Code behind:

protected void btnAddMember_Click(object sender, EventArgs e)
{
TextBox txtMember =
(TextBox)dataListRoleMembers.FindControl("txtAddMe mber");
....

}
txtMember is null, FindControl don't get it for me - why?

I've tried also:
Control ctrl = dataListRoleMembers.FindControl("txtAddMember");
but result was the same, null

Thanks for help!
Mar 26 '06 #1
2 6546
I believe the problem is because of how you are calling the FindControl
method. FindControl needs to know which Item it is looking in because a
DataList contains many copies of each Control that is in a template, so you
need to call it as a method of an Item. Here is an example of where I used
in some code of mine to set the Text property of a Label (my code is in
VB.NET, but it should be similar in C#)
Private Sub datResults_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataListItemEventArgs) Handles
datResults.ItemDataBound

CType(e.Item.FindControl("lblPhone1"), Label).Text = "&nbsp;"

End Sub
Notice that I used e.Item.FindControl when calling the FindControl method. I
don't know what event is calling your method, so if you need help figuring
out how to determine which Item called it, let me know. But something that
it looks like you need to read up on a little bit is Event Bubbling (this is
where you use the CommandName and CommandArgument properties of the
Button/LinkButton/ImageButton Controls). Good Luck!
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"christof" <no****@nomail.de> wrote in message
news:uR**************@TK2MSFTNGP10.phx.gbl...
How to do it:

My page:

<asp:DataList ID="dataListRoleMembers" ...>
...
<FooterTemplate>
<asp:LinkButton ID="btnAddMember" runat="server"
OnClick="btnAddMember_Click">Add...</asp:LinkButton>
<asp:TextBox ID="txtAddMember" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:DataList>

Code behind:

protected void btnAddMember_Click(object sender, EventArgs e)
{
TextBox txtMember =
(TextBox)dataListRoleMembers.FindControl("txtAddMe mber");
...

}
txtMember is null, FindControl don't get it for me - why?

I've tried also:
Control ctrl = dataListRoleMembers.FindControl("txtAddMember");
but result was the same, null

Thanks for help!

Mar 26 '06 #2
Nathan Sokalski wrote:
I believe the problem is because of how you are calling the FindControl
method. FindControl needs to know which Item it is looking in because a
DataList contains many copies of each Control that is in a template, so you
need to call it as a method of an Item.

[...]
At the beginning i thought that in footer it is quite easy to
distinguish, not like in other items, but
thank you very much for your help, you were right, i'm doing it now this
way and it works super:

protected void dataListRoleMembers_ItemCommand(object source,
DataListCommandEventArgs e)
{
string cmd = ((LinkButton)e.CommandSource).CommandName;

TextBox txtMember = (TextBox)(e.Item.FindControl("txtAddMember"));
string txt = txtMember.Text;

if (txtMember.Text.Length > 0 && cmd=="add")
{
try
{
....
}
....
}
}

Thanks one more time!
Mar 26 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Fabrizio | last post by:
Hi, I'm using a datalist with a LoadTemplate method from an ascx(text format) file. Everything is working fine till i need to find a control within the datalist in the datalist_itemdatabound event....
1
by: bill yeager | last post by:
I have a datagrid control within a datalist control. When I try and do a "Find" on the control, the object comes back with nothing and then my pgm crashes. I am 100% sure that my datagird inside...
4
by: V. Jenks | last post by:
What seems like a simple thing is apparently not so straightforward? I have a datalist. Inside of that datalist is an <itemtemplate> secion which contains other server controls such as a...
0
by: Luis Esteban Valencia | last post by:
Hi, I'm in serious need of help. I have a datalist that will list a number of 'Agents'. When the user selects the edit button of my datalist, I put it into edit mode. When putting the datalist...
6
by: tshad | last post by:
I need to get to a status label I have on my footer section of my datalist. There is no event happening that would go to the footer. I am just doing some processing and want to update the label...
5
by: Vikas Kumar | last post by:
i had coded like this <ItemTemplate> <table width="100%"> <tr width="100%"> <td width="25%"><%#DataBinder.Eval(Container.DataItem,"FName")%></td> <td width="25%"><input type=text ...
3
by: Crazy Cat | last post by:
Hi all, I am developing an asp.net 2.0 application in Visual Studio 2005. On my page I have a simple datalist that is bound programmatically to a collection of simple objects. On this page I...
1
by: lisles | last post by:
how to find a label control within nested datalist on selectedIndexChanged? protected void SelectedIndexChanged(object sender, EventArgs e) { dlEvntImages.Visible = true; ...
0
by: lisles | last post by:
how to open datalist containing multiple images on click on link button in selectedIndexChanged protected void SelectedIndexChanged(object sender, EventArgs e) { ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.