Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在中继器内将textbox值获取到数据库?_C#_Asp.net_Sql Server_Repeater - Fatal编程技术网

C# 如何在中继器内将textbox值获取到数据库?

C# 如何在中继器内将textbox值获取到数据库?,c#,asp.net,sql-server,repeater,C#,Asp.net,Sql Server,Repeater,我有一个从数据库填充的中继器: using (SqlConnection conn = new SqlConnection(connString)) { SqlCommand cmd = new SqlCommand(@"SELECT CommunityName, CID, Budget FROM Donation WHERE Year = year(getdate()) ORDER BY CommunityName", conn); conn.Open(); SqlDataA

我有一个从数据库填充的中继器:

using (SqlConnection conn = new SqlConnection(connString))
{
   SqlCommand cmd = new SqlCommand(@"SELECT CommunityName, CID, Budget FROM Donation WHERE Year = year(getdate()) ORDER BY CommunityName", conn);
   conn.Open();
   SqlDataAdapter adp = new SqlDataAdapter(cmd);
   DataSet myDataSet = new DataSet();
   adp.Fill(myDataSet);
   myRep.ItemDataBound += new RepeaterItemEventHandler(myRep_ItemDataBound);
   myRep.DataSource = myDataSet;
   myRep.DataBind();
}
void myRep_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
   var textbox = e.Item.FindControl("community");
   textbox.ClientIDMode = ClientIDMode.Static;
   textbox.ID = "community" + (e.Item.ItemIndex + 1);
 }
中继器:

<asp:UpdatePanel ID="UpdatePanel" runat="server" UpdateMode="Always">
    <ContentTemplate>
       <asp:Repeater ID="myRep" runat="server">
          <ItemTemplate>
             <div class="form-group">
                <asp:Label ID='thisLbl' runat="server" Text='<%# Eval("CommunityName") %>' />
                <asp:TextBox runat="server" ID="community" Text='<%# Eval("Budget") %>' CssClass="form-control" />
             </div>
          </ItemTemplate>
       </asp:Repeater>
    </ContentTemplate>
</asp:UpdatePanel>

这将创建6个带有标签和值的文本框,现在我的问题是如何检测这些框中的哪些属于最初从数据库中提取的记录?我希望能够修改这些框中的值,并点击按钮将它们保存回数据库,但我似乎无法集中精力将它们保存到正确的记录中


我是否应该将文本框的ID设置为可以解析并与适当记录匹配的内容?在ItemDataBound?

中,您必须在repeater item模板中放置一个隐藏字段,用于从预算中获取值,另一个隐藏字段用于保留必须在回发请求中读取的CID值。当然,您还需要一个按钮及其click事件处理程序

    <asp:Repeater ID="myRep" runat="server">
      <ItemTemplate>
         <div class="form-group">
            <asp:Label ID='thisLbl' runat="server" Text='<%# Eval("CommunityName") %>' />
            <asp:TextBox runat="server" ID="txtBudget" Text='<%# Eval("Budget") %>' CssClass="form-control" />
            <asp:HiddenField runat="server" ID="hdOriginalBudget" Value='<%# Eval("Budget") %>' />
            <asp:HiddenField runat="server" ID="hdCID" Value='<%# Eval("CID") %>' />
         </div>
      </ItemTemplate>
   </asp:Repeater>
    <br />
    <asp:Button ID="btn" runat="server" OnClick="btn_Click" />

请注意,我的代码缺少最基本的验证,因此,如果用户在文本框中写入无效值(例如“yyy”),它将中断。所以请不要把它投入生产,因为它是

将hiddenfield添加到项目模板中,u将使用相应的id填充该模板。然后如何将hiddenfield绑定到文本框?但是您如何知道在数据库中的何处放置文本框。文本值?示例:数据库中的初始预算金额ID为7,我更改了该预算金额,并用新金额更新记录ID 7,如何确保ID 7得到更新?令人惊讶的是,我没有这样想,添加多个隐藏字段。非常感谢你。
    protected void btn_Click(object sender, EventArgs e)
    {
        foreach (RepeaterItem item  in myRep.Items)
        {
            var txtBudget = item.FindControl("txtBudget") as TextBox;
            var hdOriginalBudget = item.FindControl("hdOriginalBudget") as HiddenField;
            var hdCID = item.FindControl("hdCID") as HiddenField;
            if (txtBudget.Text != hdOriginalBudget.Value)
            { 
                //If you enter here means the user changed the value of the text box

                using (SqlConnection conn = new SqlConnection(connString))
                {
                    SqlCommand cmd = new SqlCommand(@"UPDATE Donation SET Budget = @Budget WHERE CID = @CID", conn);
                    cmd.Parameters.Add(new SqlParameter("@Budget", int.Parse(txtBudget.Text)));
                    cmd.Parameters.Add(new SqlParameter("@CID", int.Parse(hdCID.Value)));
                    conn.Open();
                    cmd.ExecuteNonQuery();
                }

                //After you write in the database realign the values
                hdOriginalBudget.Value = txtBudget.Text; 
            }

        }
    }