Problem getting value from Datagrid

G

Guest

I have the following cod

private void btnUpdate_Click(object sender, System.EventArgs e

TextBox tbMembers = new TextBox()

tbMembers = (TextBox) dlClubs.FindControl("tbMembers")

string sqlUpdate = "UPDATE tblClub SET "

if (tbMembers.Text.Length > 0
sqlUpdate = sqlUpdate + "Membership = " + tbMembers.Text.Replace("'","''") + ", "
els
sqlUpdate = sqlUpdate + "Membership = null,"
sqlUpdate = sqlUpdate + " WHERE (ClubID = " + ddlClubs.SelectedValue + ")"
Response.Write(sqlUpdate)


This code results in a "Object reference not set to an instance of an object". I have checked to ensure the Datagrid contains this object. The following is how it is defined in the datagrid

<asp:datalist id="dlClubs" runat="server"><ItemTemplate><TABLE id="Table13" width="500"><TR><TD vAlign="top"><asp:label id="lblMembers" runat="server" CssClass="lblDetail">Number of Memebers:</asp:label></TD><TD vAlign="top"><asp:textbox id="tbMembers" runat="server" Width="200px" Text='<%# DataBinder.Eval(Container, "DataItem.Membership") %>'></asp:textbox></TD></TR></TABLE></ItemTemplate></asp:datalist

What am I doing wrong? Shouldn't this work? Is there some other way to get the value out of a textbox element on the form

Thanks
CDWaddel
 
G

Guest

Hi CDWaddell,

Thank you for posting in the community!

Based on my understanding, you use DataList control in your webform, and
you want to use FindControl to get the TextBox and do operation on it.
But the FindControl method will always fail and return null reference.

=======================================
Actually, Control.FindControl(id) method will find the control with "id" IN
"Control"'s NAMINGCONTAINER.

For NamingContainer, it is the ID namespace. For all the Repeating
control(DataGrid, DataList or Repeater), each row in them also implements
the INamingContainer interface. And every TextBox is included in a row, so
the TextBox is not in the NamingContainer of the DataList control, it is in
the NamingContainer of each row. So the DataList.FindControl will not find
the TextBox.

The reason why every row implement INamingContainer is that: every row
contains TextBox, which has the same ID "tbMembers", so it will cause ID
conflict in DataList control, so every row acts as a namingcontainer for
child control to separate the conflict.

For more information about the Control Identification mechanism in Asp.net
Web Form, please refer to the article below:
http://msdn.microsoft.com/library/en-us/vbcon/html/vbconwebformscontrolident
ification.asp

To determine which control implement INamingContainer interface, please
refer to:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfSystemWebUIINamingContainerClassTopic.asp

~~~~~~~~~~~~~~~~~~
To resolve your problem, there are 2 ways:

1). Use the NamingContainer of the child control(That is DataListItem):

foreach(DataListItem dli in dlClubs.Items)
{
TextBox tb=(TextBox)dli.FindControl("tbMembers");
this.Response.Write( tb.ID+ " <br>" );
}

2). Loop through the control collection of each row, and find the TextBox:

foreach(DataListItem dli in dlClubs.Items)
{
if(e.Item.ItemType==ListItemType.Item
||e.Item.ItemType==ListItemType.AlternatingItem)
{
foreach(Control c in dli.Controls)
{
if(c is TextBox)
{
this.Response.Write(c.ID+ "<br>");
}
}
}
}

The second solution is convenient when you did not specify the ID for your
child control.

Additionally, for more information about "Referencing Controls in Web Forms
Pages", please refer to:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/
vbtskreferencingcontrolsinwebformspages.asp

=================================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.
Have a nice day!!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
G

Guest

Sorry, the second solution code should be:

foreach(DataListItem dli in dlClubs.Items)
{
if(dli.ItemType==ListItemType.Item
||dli.ItemType==ListItemType.AlternatingItem)
{
foreach(Control c in dli.Controls)
{
if(c is TextBox)
{
this.Response.Write(c.ID+ "<br>");
}
}
}
}

Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top