将转发器中文本框的值传递给数据库

时间:2014-05-07 08:40:05

标签: c# asp.net

我想将转发器中文本框的值传递给数据库,但数据库中的值显示为空。

我的代码:

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";
    }
}

2 个答案:

答案 0 :(得分:1)

e.Item更改为item。因为你的foreach占用了所有转发器项目,所以你不需要e.Item。

请尝试使用此代码而不是代码。

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";
    }
}

答案 1 :(得分:0)

要获取转发器中文本框的值,您可以使用以下代码:

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

获取值后,将更容易将其发送到用于与数据库通信的任何函数。