如何将转发器控制值存储到数据库

时间:2012-02-21 11:23:59

标签: c# asp.net c#-4.0

我想将转发器控件值存储到数据库表中。我有一个带有三个标签的转发器控件和相应的文本框。

<asp:Repeater ID="RepeatInformation" runat="server">
 <ItemTemplate>
   <tr>
      <td valign="top" class="style3"> 
         <%#DataBinder.Eval(Container,"DataItem.fldleavename")%>
      </td>
      <td valign="top">
          <asp:TextBox ID="TextBox1" runat="server" CssClass="textbox" ></asp:TextBox>
      </td>
  </tr>
 </ItemTemplate>
</asp:Repeater>

转发器控制格式:

  Casual Leave  : Textbox1
  Medical Leave : Textbox1
  Annual Leave  : Textbox1

如何将转发器值存储到数据库中。我不知道存储这个值请帮助我..

4 个答案:

答案 0 :(得分:0)

您可以将值存储在列表对象&amp;然后将其绑定到数据库中的数据表中。

答案 1 :(得分:0)

foreach (RepeaterItem item in RepeatInformation.Items)
{
    if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
    {
        TextBox textBox = (TextBox)item.FindControl("TextBox1");

        //Now you have your textbox to do with what you like
        //You can access the .Text property to find the new value that needs saving to the database
    }
}

旁注。我会考虑修改控件的名称。如果你采用某种惯例并使用驼峰的情况,它将在未来使生活变得更加轻松。

关于将其保存到数据库 - 这完全取决于您目前如何处理数据访问。这是我认为的另一个问题。

答案 2 :(得分:0)

使用SQLDataAdapter修改数据表并更新到数据库

答案 3 :(得分:-1)

enter code here protected void repeater_ItemDataBound(object sender,RepeaterItemEventArgs e)         {             if(e.Item.DataItem!= null)             {             //为数据库创建对象             //从rep控件获取数据             //使用e.Item             //将其存储到数据库中          }     }