Aspx没有';t从中继器更新数据库

本文关键字:中继器 更新 数据库 没有 Aspx | 更新日期: 2023-09-27 18:21:58

我一直在制作一个aspx网站,但我遇到了一些无法按我希望的方式工作的问题。

我有一个中继器,我从数据库中填充。中继器为每个单元格都有一个Literal和一个Textbox。它们都填充了相同的数据,但默认情况下文本框是隐藏的。文本框仅显示,当用户单击编辑按钮时,文本框隐藏。

这一切都取自本教程关于编辑直放站内联:在此处输入链接描述

不过,我的问题是:我还有一个done按钮(doneBtn),当用户编辑完中继器中的行时,这个按钮与我的数据库中准备好的语句一起使用,以便用中继器的新数据更新行。它就是不起作用。。

当我单击编辑时,文字被隐藏,文本框被显示,但如果我编辑了一些文本并单击完成,那么数据库中什么都不会发生。文本框被隐藏,文字再次显示,但没有任何数据被编辑。

如果我在C#代码中将每个参数值都更改为硬编码字符串,我可以强制它"工作"

SqlParameter doneActID = cmd.Parameters.Add("@ActivityID", SqlDbType.Int);
doneActID.Direction = ParameterDirection.Input;
doneActID.Value = "Some String";

我包含了用于中继器编辑的C#代码和准备好的语句。

我希望有人能告诉我我做错了什么愚蠢的事,因为我确信这是愚蠢的事情,但我对正确的编码很陌生(我都是前端),所以对我宽容一点。

protected void RepeaterActivities_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        RepeaterItem item = e.Item;
        LinkButton crudEdit = (LinkButton)item.FindControl("crudEdit");
        LinkButton crudDelete = (LinkButton)item.FindControl("crudDelete");
        LinkButton crudDone = (LinkButton)item.FindControl("crudDone");
        LinkButton crudCancel = (LinkButton)item.FindControl("crudCancel");
        Literal LiteralID = (Literal)item.FindControl("LiteralID");
        TextBox TextBoxID = (TextBox)item.FindControl("TextBoxID");
        Literal LiteralTitle = (Literal)item.FindControl("LiteralTitle");
        TextBox TextBoxTitle = (TextBox)item.FindControl("TextBoxTitle");
        Literal LiteralDOTW = (Literal)item.FindControl("LiteralDOTW");
        TextBox TextBoxDOTW = (TextBox)item.FindControl("TextBoxDOTW");
        Literal LiteralTOTD = (Literal)item.FindControl("LiteralTOTD");
        TextBox TextBoxTOTD = (TextBox)item.FindControl("TextBoxTOTD");
        Literal LiteralDesc = (Literal)item.FindControl("LiteralDescription");
        TextBox TextBoxDesc = (TextBox)item.FindControl("TextBoxDescription");
        Literal LiteralRoom = (Literal)item.FindControl("LiteralRoom");
        TextBox TextBoxRoom = (TextBox)item.FindControl("TextBoxRoom");
        Literal LiteralImageUrl = (Literal)item.FindControl("LiteralImgUrl");
        Image ImageImgUrl = (Image)item.FindControl("ImageImgUrl");
        TextBox TextBoxImageUrl = (TextBox)item.FindControl("TextBoxImgUrl");
        Literal LiteralCoachID = (Literal)item.FindControl("LiteralCoachID");
        TextBox TextBoxCoachID = (TextBox)item.FindControl("TextBoxCoachID");
        if (e.CommandName == "editBtn")
        {
            crudEdit.Visible = false;
            crudDelete.Visible = false;
            crudDone.Visible = true;
            crudCancel.Visible = true;
            LiteralID.Visible = false;
            TextBoxID.Visible = true;
            LiteralTitle.Visible = false;
            TextBoxTitle.Visible = true;
            LiteralDOTW.Visible = false;
            TextBoxDOTW.Visible = true;
            LiteralTOTD.Visible = false;
            TextBoxTOTD.Visible = true;
            LiteralDesc.Visible = false;
            TextBoxDesc.Visible = true;
            LiteralRoom.Visible = false;
            TextBoxRoom.Visible = true;
            ImageImgUrl.Visible = false;
            LiteralImageUrl.Visible = false;
            TextBoxImageUrl.Visible = true;
            LiteralCoachID.Visible = false;
            TextBoxCoachID.Visible = true;
        }
        else if (e.CommandName == "deleteBtn")
        {
            SqlConnection conn = new SqlConnection(@"data source = localhost; user = root; password = toor; database = dblocal");
            SqlCommand cmd = null;
            try
            {
                conn.Open();
                cmd = conn.CreateCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "delete";
                SqlParameter activityID = cmd.Parameters.Add("@ActivityID", SqlDbType.Int);
                activityID.Direction = ParameterDirection.Input;
                activityID.Value = LiteralID.Text;
                cmd.ExecuteNonQuery();
                showDataInTables();
            }
            catch (Exception ex)
            {
                Label1.Text = ex.Message;
            }
            finally
            {
                conn.Close();
            }
        }
        else if (e.CommandName == "doneBtn")
        {
            SqlConnection conn = new SqlConnection(@"data source = localhost; user = root; password = toor; database = dblocal");
            SqlCommand cmd = null;
            try
            {
                conn.Open();
                cmd = conn.CreateCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "update";
                SqlParameter doneActID = cmd.Parameters.Add("@ActivityID", SqlDbType.Int);
                doneActID.Direction = ParameterDirection.Input;
                doneActID.Value = TextBoxID.Text;
                SqlParameter doneActTitle = cmd.Parameters.Add("@Title", SqlDbType.NVarChar, 50);
                doneActTitle.Direction = ParameterDirection.Input;
                doneActTitle.Value = TextBoxTitle.Text;
                SqlParameter doneActDOTW = cmd.Parameters.Add("@DayOfTheWeek", SqlDbType.NVarChar, 50);
                doneActDOTW.Direction = ParameterDirection.Input;
                doneActDOTW.Value = TextBoxDOTW.Text;
                SqlParameter doneActTOTD = cmd.Parameters.Add("@TimeOfTheDay", SqlDbType.Time, 7);
                doneActTOTD.Direction = ParameterDirection.Input;
                doneActTOTD.Value = TextBoxTOTD.Text;
                SqlParameter doneActDesc = cmd.Parameters.Add("@Description", SqlDbType.NVarChar, 50);
                doneActDesc.Direction = ParameterDirection.Input;
                doneActDesc.Value = TextBoxDesc.Text;
                SqlParameter doneActRoom = cmd.Parameters.Add("@RoomNumber", SqlDbType.NVarChar, 50);
                doneActRoom.Direction = ParameterDirection.Input;
                doneActRoom.Value = TextBoxRoom.Text;
                SqlParameter doneActImg = cmd.Parameters.Add("@ImgUrl", SqlDbType.NVarChar, 50);
                doneActImg.Direction = ParameterDirection.Input;
                doneActImg.Value = TextBoxImageUrl.Text;
                SqlParameter doneActCoachID = cmd.Parameters.Add("@CoachID", SqlDbType.Int);
                doneActCoachID.Direction = ParameterDirection.Input;
                doneActCoachID.Value = TextBoxCoachID.Text;
                cmd.ExecuteNonQuery();
                showDataInTables();
            }
            catch (Exception ex)
            {
                Label1.Text = ex.Message;
            }
            finally
            {
                conn.Close();
            }
        }
        else if (e.CommandName == "cancelBtn")
        {
            crudEdit.Visible = true;
            crudDelete.Visible = true;
            crudDone.Visible = false;
            crudCancel.Visible = false;
            LiteralID.Visible = true;
            TextBoxID.Visible = false;
            LiteralTitle.Visible = true;
            TextBoxTitle.Visible = false;
            LiteralDOTW.Visible = true;
            TextBoxDOTW.Visible = false;
            LiteralTOTD.Visible = true;
            TextBoxTOTD.Visible = false;
            LiteralDesc.Visible = true;
            TextBoxDesc.Visible = false;
            LiteralRoom.Visible = true;
            TextBoxRoom.Visible = false;
            ImageImgUrl.Visible = true;
            LiteralImageUrl.Visible = true;
            TextBoxImageUrl.Visible = false;
            LiteralCoachID.Visible = true;
            TextBoxCoachID.Visible = false;
        }
    }

    <asp:Repeater ID="RepeaterActivities" runat="server" OnItemCommand="RepeaterActivities_ItemCommand">
    <HeaderTemplate>
        <tr>
            <th>Edit</th>
            <th>ID</th>
            <th>Title</th>
            <th>Day of the week</th>
            <th>Time of the day</th>
            <th class="th-description">Description</th>
            <th>Room Number</th>
            <th>Image URL</th>
            <th>Coach ID - FK</th>
        </tr>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td class="crud-edit-delete">
                 <asp:LinkButton ID="crudEdit" runat="server" CommandName="editBtn" Text='<i class="material-icons">mode_edit</i>'></asp:LinkButton>
                 <asp:LinkButton ID="crudDelete" runat="server" CommandName="deleteBtn" Text='<i class="material-icons">delete</i>'></asp:LinkButton>
                 <asp:LinkButton ID="crudDone" runat="server" CommandName="doneBtn" Visible="false" Text='<i class="material-icons icon-done">done</i>'></asp:LinkButton>
                 <asp:LinkButton ID="crudCancel" runat="server" CommandName="cancelBtn" Visible="false" Text='<i class="material-icons">close</i>'></asp:LinkButton>
            </td>
            <td>
                 <asp:Literal ID="LiteralID" runat="server" Text='<%# Eval("ActivityID") %>'></asp:Literal>
                 <asp:TextBox ID="TextBoxID" CssClass="activity-id form-control" runat="server" Text='<%# Eval("ActivityID") %>' Visible="false" ReadOnly="true"></asp:TextBox>
            </td>
            <td>
                 <asp:Literal ID="LiteralTitle" runat="server" Text='<%# Eval("Title") %>'></asp:Literal>
                 <asp:TextBox ID="TextBoxTitle" CssClass="title form-control" runat="server" Text='<%# Eval("Title") %>' Visible="false"></asp:TextBox>
            </td>
            <td>
                 <asp:Literal ID="LiteralDOTW" runat="server" Text='<%# Eval("DayOfTheWeek") %>'></asp:Literal>
                 <asp:TextBox ID="TextBoxDOTW" CssClass="dotw form-control" runat="server" Text='<%# Eval("DayOfTheWeek") %>' Visible="false"></asp:TextBox>
            </td>
            <td>
                 <asp:Literal ID="LiteralTOTD" runat="server" Text='<%# Eval("TimeOfTheDay").ToString().Substring(0, 5) %>'></asp:Literal>
                 <asp:TextBox ID="TextBoxTOTD" CssClass="totd form-control" runat="server" Text='<%# Eval("TimeOfTheDay").ToString().Substring(0, 5) %>' Visible="false"></asp:TextBox>
            </td>
            <td>
                 <asp:Literal ID="LiteralDescription" runat="server" Text='<%# Eval("Description") %>'></asp:Literal>
                 <asp:TextBox ID="TextBoxDescription" CssClass="description form-control" runat="server" Text='<%# Eval("Description") %>' Visible="false"></asp:TextBox>
            </td>
            <td>
                <asp:Literal ID="LiteralRoom" runat="server" Text='<%# Eval("RoomNumber") %>'></asp:Literal>
                <asp:TextBox ID="TextBoxRoom" CssClass="room form-control" runat="server" Text='<%# Eval("RoomNumber") %>' Visible="false"></asp:TextBox>
            </td>
            <td>
                <asp:Image ID="ImageImgUrl" runat="server" ImageUrl='<%# "pictures/" + Eval("ImgUrl")%>' />
                <asp:Literal ID="LiteralImgUrl" runat="server" Text='<%# Eval("ImgUrl") %>'></asp:Literal>
                <asp:TextBox ID="TextBoxImgUrl" CssClass="img-url form-control" runat="server" Text='<%# Eval("ImgUrl") %>' Visible="false"></asp:TextBox>
            </td>
            <td>
                <asp:Literal ID="LiteralCoachID" runat="server" Text='<%# Eval("CoachID_FK") %>'></asp:Literal>
                <asp:TextBox ID="TextBoxCoachID" CssClass="coach-id form-control" runat="server" Text='<%# Eval("CoachID_FK") %>' Visible="false"></asp:TextBox>
            </td>
        </tr>
    </ItemTemplate>
</asp:Repeater>

  CREATE PROCEDURE [dbo].[update]
    @ActivityID int,
    @Title nvarchar(50),
    @DayOfTheWeek nvarchar(50),
    @TimeOfTheDay time(7),
    @Description nvarchar(255),
    @RoomNumber nvarchar(50),
    @ImgUrl nvarchar(50),
    @CoachID int
  AS
    UPDATE Activities
    SET Title = @Title, DayOfTheWeek = @DayOfTheWeek, TimeOfTheDay =  @TimeOfTheDay, Description = @Description, RoomNumber = @RoomNumber, ImgUrl = @ImgUrl, CoachID_FK = @CoachID
    WHERE ActivityID = @ActivityID;
  RETURN

Aspx没有';t从中继器更新数据库

我更喜欢使用基本的HTML控件,而不是asp.net控件。它们更易于管理,并且您可以从表单数据中提取内容。我会在这里展示代码的相关部分,你会明白的。

aspx:

<asp:Repeater ID="RepeaterActivities" runat="server" OnItemCommand="RepeaterActivities_ItemCommand">
    <HeaderTemplate>
        <tr>
            <th>Edit</th>
            <th>ID</th>
            <th>Title</th>
            <th>Day of the week</th>
            <th>Time of the day</th>
            <th class="th-description">Description</th>
            <th>Room Number</th>
            <th>Image URL</th>
            <th>Coach ID - FK</th>
        </tr>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td class="crud-edit-delete">
                 <asp:LinkButton ID="crudEdit" runat="server" CommandName="editBtn" Text='<i class="material-icons">mode_edit</i>'></asp:LinkButton>
                 <asp:LinkButton ID="crudDelete" runat="server" CommandName="deleteBtn" Text='<i class="material-icons">delete</i>'></asp:LinkButton>
                 <asp:LinkButton ID="crudDone" runat="server" CommandName="doneBtn" Visible="false" Text='<i class="material-icons icon-done">done</i>'></asp:LinkButton>
                 <asp:LinkButton ID="crudCancel" runat="server" CommandName="cancelBtn" Visible="false" Text='<i class="material-icons">close</i>'></asp:LinkButton>
            </td>
            <td>
                 <label><%# Eval("ActivityID") %></label>
                 <input id="TextBoxID" runat="server" name="TextBoxID" visible="false" class="activity-id form-control" value='<%# Eval("ActivityID") %>' />
            </td>
        </tr>
    </ItemTemplate>
</asp:Repeater>

aspx.cs:

protected void RepeaterActivities_ItemCommand(object source, RepeaterCommandEventArgs e)
{
    // leave out the TextBox TextBoxID = (TextBox)item.FindControl("TextBoxID");
    // all your other code here...
    else if (e.CommandName == "doneBtn")
    {
        string id = Request.Form["TextBoxId"];
        SqlConnection conn = new SqlConnection(@"data source = localhost; user = root; password = toor; database = dblocal");
        SqlCommand cmd = null;
        try
        {
            conn.Open();
            cmd = conn.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "update";
            SqlParameter doneActID = cmd.Parameters.Add("@ActivityID", SqlDbType.Int);
            doneActID.Direction = ParameterDirection.Input;
            doneActID.Value = id;
       // rest of your code...

我所做的是用普通的html input控件替换asp:TextBox。记住给它一个name属性,这样您就可以在回发时从表单数据中读取值。然后在代码后面读取值并将其分配给一个字符串,并将该字符串用作DB方法的输入参数
没有测试所有这些,但应该可以。