0

我是一名新的 C# 和 ASP.NET 开发人员。我正在开发一个简单的事件管理系统。我希望管理员能够编辑事件的信息。事件将在绑定到数据库下表的 GridView 中列出:

Events Table: ID, Title, Description, Location, NumberOfSeats, StartDateTime, EndDateTime, IsActive

(IsActive 有点指示活动或非活动事件)

一切正常,除了当我尝试编辑 IsActive 字段时,它给了我以下错误:

输入字符串的格式不正确。

由于以下代码行:

int ID = Convert.ToInt32(gvrow.Cells[0].Text);

你能帮我修一下吗?

ASP.NET 代码:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID"
            width="500px" DataSourceID="SqlDataSource1" OnRowDataBound="GridView1_RowDataBound">
            <Columns>
                <asp:CommandField ButtonType="Image" ShowEditButton="true" ShowCancelButton="true"
                                EditImageUrl="Images/icons/edit24.png" UpdateImageUrl="Images/icons/update24.png" 
                                CancelImageUrl="Images/icons/cancel324.png" />

                <asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" Visible="false" />

                <asp:TemplateField HeaderText="Title">
                    <ItemTemplate>
                        <%# Eval("Title") %>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txtTitle"  runat="server" 
                                        Text='<%# Bind("Title") %>'></asp:TextBox>
                    </EditItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="Description">
                    <ItemTemplate>
                        <%# Eval("Description") %>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txtDescription" TextMode="MultiLine"  Rows="4" cols="30"
                            runat="server" Text='<%# Bind("Description") %>'></asp:TextBox>
                    </EditItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="Location">
                    <ItemTemplate>
                        <%# Eval("Location") %>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txtLocation" runat="server"
                                        Text='<%# Bind("Location") %>'></asp:TextBox>
                    </EditItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="Number of Seats">
                    <ItemTemplate>
                        <%# Eval("NumberOfSeats") %>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txtNumberOfSeats"  runat="server"
                                        Text='<%# Bind("NumberOfSeats") %>'></asp:TextBox>
                    </EditItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="Start Date & Time">
                    <ItemTemplate>
                        <%# Eval("StartDateTime")%>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="start_DateTime"  runat="server"
                                        Text='<%# Bind("StartDateTime") %>'></asp:TextBox>
                    </EditItemTemplate>
                </asp:TemplateField>


                <asp:TemplateField HeaderText="End Date & Time">
                    <ItemTemplate>
                        <%# Eval("EndDateTime")%>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="end_DateTime" runat="server"
                                        Text='<%# Bind("EndDateTime") %>'></asp:TextBox>
                    </EditItemTemplate>
                </asp:TemplateField>


                <asp:TemplateField HeaderText="Is Active?">
                    <ItemTemplate>
                        <asp:Label ID="lblIsActive" runat="server" Text='<%# Eval("IsActive") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:CheckBox ID="isActive" runat="server" AutoPostBack="true"
                                        OnCheckedChanged="isActive_OnCheckedChanged" 
                                        Checked='<%# Convert.ToBoolean(Eval("IsActive")) %>' />
                    </EditItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="Delete?">
                    <ItemTemplate>
                        <span onclick="return confirm('Are you sure to Delete the record?')">
                            <asp:ImageButton ID="lnkB" runat="server" ImageUrl="Images/icons/delete24.png" CommandName="Delete" />
                        </span>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:PM_RegistrationSysDBConnectionString %>" 
            SelectCommand="SELECT ID, Title, Description, Location, StartDateTime, EndDateTime, 
                            NumberOfSeats, IsActive 
                            FROM Events 
                            WHERE (IsActive = 1) 
                            ORDER BY ID DESC"
            UpdateCommand="UPDATE [Events] SET [Title] = @Title, [Description] = @Description, 
                                                [Location] = @Location, [NumberOfSeats] = @NumberOfSeats, 
                                                [StartDateTime]= @StartDateTime, [EndDateTime] = @EndDateTime
                                                WHERE [ID] = @ID"
            DeleteCommand="DELETE FROM [Events] WHERE [ID] = @ID">
            <UpdateParameters>
                <asp:Parameter Name="Title" Type="String" />
                <asp:Parameter Name="Description" Type="String" />
                <asp:Parameter Name="Location" Type="String" />
                <asp:Parameter Name="NumberOfSeats" Type="Int32" />
                <asp:Parameter Name="StartDateTime" Type="DateTime" />
                <asp:Parameter Name="EndDateTime" Type="DateTime" />
            </UpdateParameters>
            <DeleteParameters>
                <asp:Parameter Name="ID" Type="Int32" />
            </DeleteParameters>
        </asp:SqlDataSource>

C#代码:

 //for updating the (IsActive) column using checkbox inside the GridView
    protected void isActive_OnCheckedChanged(object sender, EventArgs e)
    {
        CheckBox chkStatus = (CheckBox)sender;
        GridViewRow gvrow = (GridViewRow)chkStatus.NamingContainer;

        //Get the ID which is the ID of the Event
        int ID = Convert.ToInt32(gvrow.Cells[0].Text);
        bool status = chkStatus.Checked;


        string connString = ConfigurationManager.ConnectionStrings["PM_RegistrationSysDBConnectionString"].ConnectionString;
        SqlConnection conn = new SqlConnection(connString);

        string updateIsActive = "UPDATE Events SET IsActive = @IsActive WHERE ID = @ID";

        SqlCommand cmd = new SqlCommand(updateIsActive, conn);

        cmd.Parameters.AddWithValue("@IsActive", status);
        cmd.Parameters.AddWithValue("@ID", ID);

        try
        {
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
        }
        catch (SqlException se)
        {
            throw se;
        }
        finally
        {
            cmd.Dispose();
            conn.Close();
            conn.Dispose();
        }

        //this is checkbox is unchecked then set backcolor to Gray
        if (!status)
        {
            gvrow.BackColor = Color.Black;
        }
    }
4

2 回答 2

0

最好添加标签并使其可见 false ,将 ID 绑定到它并使用 FindControl 方法找到控件,然后将其转换为 Label 并访问标签的 Text 属性。

  <asp:TemplateField HeaderText="Title">
       <ItemTemplate>
          <%# Eval("Title") %>

           <asp:Label ID="lblID"  runat="server" Visible="false" 
              Text='<%# Bind("ID") %>'></asp:Label>
        </ItemTemplate>
     <EditItemTemplate>
         <asp:TextBox ID="txtTitle"  runat="server" Text='<%# Bind("Title") %>'>         </asp:TextBox>

     </EditItemTemplate>
  </asp:TemplateField>

并在后面的代码中。

Label lblID = (Label)gvrow.FindControl("lblID");
int ID = Convert.ToInt32(lblID.Text); 

然后使用此 ID 更新状态..

于 2012-08-13T12:20:12.003 回答
0

gvrow.Cells[0]将参考您的按钮。

它不是指你的身份证。

您已将 id 放在第二列中。

访问代码应该像

gvrow.Cells[1]
于 2012-08-13T05:52:00.383 回答