Aspx不会从Repeater更新DB

时间:2015-11-14 14:43:24

标签: c# sql asp.net sql-server

我一直在努力创建一个aspx网站,但我遇到的东西根本无法按照我想要的方式运行。

我有一个从数据库中填充的Repeater。转发器具有每个单元格的文字和文本框。它们都填充了相同的数据,但文本框默认隐藏。文本框仅显示,当用户单击编辑按钮时隐藏文字。

本教程全部采用内联编辑Repeater:enter link description here

我的问题是:我还有一个完成按钮( doneBtn ),因为当用户完成在Repeater中编辑行时 - 此按钮与我的数据库中的预准备语句一起用于更新从转发器中获取新数据。它只是不起作用..

当我点击编辑时隐藏文字并显示文本框,但是如果我编辑了一些文本并单击完成,则数据库中没有任何反应。文本框被隐藏,文字再次显示,但没有数据被编辑。

如果我将每个参数值更改为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

1 个答案:

答案 0 :(得分:0)

我更喜欢使用基本的HTML控件而不是asp.net控件。它们更容易管理,您可以从表单数据中提取内容。我将在此处显示代码的相关部分,您将了解到这一点。

ASPX:

200000

aspx.cs:

<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>

我所做的是我用正常的html 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... 控件替换了asp:TextBox。请记住给它一个input属性,以便您可以在回发时从表单数据中读取值。然后在您的代码后面读取值并将其分配给字符串并使用该字符串作为DB方法的输入参数 没有测试所有这些,但它应该有用。