我是一名新的C#和ASP.NET开发人员。我正在开发一个简单的事件管理系统。我希望管理员能够编辑事件的信息。事件将列在GridView中,该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;
}
}
答案 0 :(得分:0)
gvrow.Cells[0]
会引用您的按钮。
它不是指您的身份证明。
您已在第二列中添加了ID。
访问代码应该像
gvrow.Cells[1]
答案 1 :(得分: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更新状态..