By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,483 Members | 1,052 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,483 IT Pros & Developers. It's quick & easy.

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

P: n/a
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?
Nov 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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.FindCont rol("CommentText");
string editedComment = txtComment.Text;

Regards,
Ravi

donnet <do****@discussions.microsoft.com> wrote in message news:<9A**********************************@microso ft.com>...
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?

Nov 19 '05 #2

P: n/a
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.FindCont rol("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.
"Ravi" wrote:
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.FindCont rol("CommentText");
string editedComment = txtComment.Text;

Regards,
Ravi

donnet <do****@discussions.microsoft.com> wrote in message news:<9A**********************************@microso ft.com>...
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?

Nov 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.