C# 在访问listview中的文本框时遇到问题

C# 在访问listview中的文本框时遇到问题,c#,asp.net,listview,C#,Asp.net,Listview,每次单击lnkUpdate按钮时,我都会收到一个错误(“输入字符串的格式不正确”)。我错过什么了吗?我找不到更新事件中的代码有什么问题 aspx 你的问题不是不能访问文本框 使用而不是int.Parse,因为您正在读取用户输入 int received; if (!int.TryParse(Received, out received) || received < 0) { error.Visible = tr

每次单击lnkUpdate按钮时,我都会收到一个错误(“输入字符串的格式不正确”)。我错过什么了吗?我找不到更新事件中的代码有什么问题

aspx


你的问题不是不能访问文本框

使用而不是int.Parse,因为您正在读取用户输入

        int received;
        if (!int.TryParse(Received, out received) || received < 0)
        {
                error.Visible = true;
                lblError.Text = "Items received must not be lower than zero";
        }
int接收;
如果(!int.TryParse(已接收,未接收)| |已接收<0)
{
错误。可见=真;
lblError.Text=“收到的项目不得低于零”;
}

您能否只发布演示问题的代码部分?哪一行抛出错误?我猜这是一个int.Parse调用,但是哪个呢?调用这些字符串变量时,字符串变量中有什么?@eddie_cat它看起来好像接收到的字符串没有返回值..ProdID和Remaining在我测试时返回一个值,但当涉及到接收时,它似乎没有返回值您的
接收到的
的字符串格式不正确,无法解析为整数。检查该变量来自何处。能否在“代码隐藏”中添加为ListView设置数据绑定的代码?我没有否决,但如果
int.Parse()
每次都失败,那么
int.TryParse()
每次也会失败。后者只是处理错误,实际上并不能解决当前的问题。如果用户每次都将值留空或输入无效值,则每次都会失败。您所能做的就是以某种方式编写代码,以防止由于无效的用户输入而引发异常。@j.f.关于我之前的注释:但是,如果每次都无效,即使使用有效的用户输入,那么是的,我们还有一个单独的问题。
protected void lvSODetails_ItemEditing(object sender, ListViewEditEventArgs e)
{
    lvSODetails.EditIndex = e.NewEditIndex;
    GetInfo();
    GetDetails();
}
protected void lvSODetails_ItemCanceling(object sender, ListViewCancelEventArgs e)
{
    lvSODetails.EditIndex = -1;
    GetInfo();
    GetDetails();
}
protected void lvSODetails_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
    string ProdID = (lvSODetails.Items[e.ItemIndex].FindControl("lblID") as Label).Text;
    string Received = (lvSODetails.Items[e.ItemIndex].FindControl("txtQtyReceived") as TextBox).Text;
    string Remaining = (lvSODetails.Items[e.ItemIndex].FindControl("lblRemaining") as Label).Text;
    if (int.Parse(Received) < 0) //   <~ this is where the code stops (error"input string was not in a correct format")
    {
        error.Visible = true;
        lblError.Text = "Items received must not be lower than zero";
    }
    else if (int.Parse(Received) >= 0 && int.Parse(Received) <= int.Parse(Remaining))
    {
        SODetails = (DataTable)Session["sodelivery"];
        foreach (DataRow row in SODetails.Rows)
        {
            if (row["ProductID"].ToString() == ProdID)
            {
                row["Received"] = Received;
                break;
            }
        }
    }
    lvSODetails.EditIndex = -1;
    GetInfo();
    GetDetails();
}
void GetDetails()
    {
        if (SODetails == null)
        {
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "SELECT sod.ProductID, p.ProductName, sod.SOQtyReceived AS Received, " +
                "(sod.SOQtyOrdered - sod.SOQtyReceived) AS Remaining, sod.SOQtyOrdered AS Quantity FROM SODetails AS sod " +
                "INNER JOIN Products AS p ON sod.ProductID=p.ProductID WHERE sod.SONo=@SONo";
            cmd.Parameters.Add("@SONo", SqlDbType.Int).Value = Request.QueryString["ID"].ToString();
            SqlDataAdapter data = new SqlDataAdapter(cmd);
            SODetails = new DataTable();
            data.Fill(SODetails);

            DataRow[] rowList = SODetails.Select();
            foreach (DataRow dr in rowList)
            {
                dr["Received"] = "0";
            }
            lvSODetails.DataSource = SODetails;
            lvSODetails.DataBind();
            con.Close();
            Session["sodelivery"] = SODetails;
        }
        else
        {
            lvSODetails.DataSource = SODetails;
            lvSODetails.DataBind();
            Session["sodelivery"] = SODetails;
        }
    }
        int received;
        if (!int.TryParse(Received, out received) || received < 0)
        {
                error.Visible = true;
                lblError.Text = "Items received must not be lower than zero";
        }