how to edit a textbox field populated by data from a datalist?

G

Guest

Inside my .aspx file, I have a textbox populated with data from a dataset
like this:

<asp:TextBox text='<%# DataBinder.Eval(Container.DataItem, "Comment")%>'
id="CommentText" runat="server" TextMode="MultiLine"></asp:TextBox>
</ItemTemplate>
</asp:datalist>
<asp:button id="EditCommentButton" Text="Edit Comment"
Runat="server"></asp:button

In my .aspx.cs file, I have a method:

private void EditCommentButton_Click(object sender, System.EventArgs e)
{
string editedComment = CommentText.Text.ToString();
// then here do an UPDATE
SqlConnection dataConnection = new SqlConnection(CONNECTION_STRING);
SqlCommand dataCommand = new SqlCommand();
dataCommand.CommandText = "UPDATE table SET Comment = '" + editedComment +
"' ";
dataCommand.CommandText += "WHERE blah blah blah";

}

My codes is able to populate the textbox with all the comments in the html
page, but I cannot edit the text when firing on the "EditCommentButton_Click"
method.
I would get an error: "Object reference not set to an instance of an object".
Is this line incorrect? --> string editedComment =
CommentText.Text.ToString( )

I know the SQL Update works fine because if I do:
string editedComment = "HELLO WORLD";
then the textbox would be updated. So I guess something is wrong with
"CommentText.Text.ToString( )" and it is not casting to a string or something.
How should I change my codes so I can edit a populated textbox field?
 
R

Ravi

Hi,

You cannot refer the textbox control 'CommentText' in your code behind
while editing the datagrid row.

Try this code in EditCommentButton_Click event

System.Web.UI.WebControls.TextBox txtComment = new TextBox();
txtComment = (System.Web.UI.WebControls.TextBox)e.Item.FindControl("CommentText");
string editedComment = txtComment.Text;

Regards,
Ravi
 
G

Guest

Ravi,

when trying to compile, I get the following error "System.EventArgs does not
contain a definition for 'Item' "

(System.Web.UI.WebControls.TextBox)e.Item.FindControl("CommentText");

I don't know whether it's relevant in here, but you mentionned i cannot
refer a text box control when editing datagrid row, however i'm using a
datalist... doesn't a datalist allow you for more html freedom and is not as
restrictive as a datagrid.
 

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

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top