user3489879
user3489879

Reputation: 1

Pass the value of a textbox inside a repeater to the database

I want to pass the value of a textbox inside a repeater to the database but the the value in the database shows up empty.

My code:

if (e.CommandName == "Post")
{
    foreach (RepeaterItem item in Repeater1.Items)
    {
        HiddenField hfproductid = (HiddenField)e.Item.FindControl("hfproductID");
        HiddenField hfshareID = (HiddenField)e.Item.FindControl("hfshareID");

        LinkButton post = (LinkButton)e.Item.FindControl("Post");
        comobj._CommentID = Guid.NewGuid();
        comobj._ShareID = new Guid(hfshareID.Value.ToString());
        comobj._ClientID = new Guid(hfClientID.Value.ToString());

        TextBox txtcomment = (TextBox)e.Item.FindControl("txtcomment");
        comobj._Body = txtcomment.Text;

        comobj.SaveComment();
        post.Enabled = false;
        post.Text = "Posted";
    }
}

Upvotes: 0

Views: 183

Answers (2)

befree2j
befree2j

Reputation: 371

To get the value of the Textbox inside the repeater you can use this code:

 string txtBoxValue = "";
    foreach (RepeaterItem dataItem in Your_Repeaper.Items)
    {
       txtBoxValue =((TextBox)dataItem.FindControl("yourtextboxID")).Text;
    }

After getting the value it will be easier to send it to any functions you use to communicate with the database.

Upvotes: 0

user3559224
user3559224

Reputation:

Change e.Item to item. because your foreach took all repeater items, So you don't need e.Item.

So try this code instead of your code.

if (e.CommandName == "Post")
{
    foreach (RepeaterItem item in Repeater1.Items)
    {
        HiddenField hfproductid = (HiddenField)item .FindControl("hfproductID");
        HiddenField hfshareID = (HiddenField)item.FindControl("hfshareID");

        LinkButton post = (LinkButton)item.FindControl("Post");
        comobj._CommentID = Guid.NewGuid();
        comobj._ShareID = new Guid(hfshareID.Value.ToString());
        comobj._ClientID = new Guid(hfClientID.Value.ToString());

        TextBox txtcomment = (TextBox)e.Item.FindControl("txtcomment");
        comobj._Body = txtcomment.Text;

        comobj.SaveComment();
        post.Enabled = false;
        post.Text = "Posted";
    }
}

Upvotes: 1

Related Questions