输入字符串的格式不正确,无法检索 GridView 值

本文关键字:检索 GridView 字符串 格式 不正确 输入 | 更新日期: 2023-09-27 18:37:02

在我的网格视图中,我希望当用户单击激活然后导航到激活.aspx页面时。我想从上一页检索三个值来激活.aspx页面。我的代码是:

    protected void Page_Load(object sender, EventArgs e)
    {
         if (this.Page.PreviousPage != null)
         {
         int rowIndex = int.Parse(Request.QueryString["RowIndex"]);
        GridView GridView1 = (GridView)this.Page.PreviousPage.FindControl("GridView1");
        GridViewRow row = GridView1.Rows[rowIndex];
        Label1.Text = row.Cells[3].Text;
        Label2.Text = row.Cells[2].Text;
        Label3.Text = row.Cells[2].Text;
        SqlDataAdapter da = new SqlDataAdapter("insert into login values('" + Label1.Text + "','" + Label2.Text + "','" + Label3.Text + "')",con);
        DataSet ds = new DataSet();
        da.Fill(ds);
     }
    }
  protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Redirect")
    {
        String email = "lblemail.Text";
        String mob = "lblmobno.Text";
        Server.Transfer("activate.aspx?RowIndex=" + email +mob, true);
    }
}

Aspx 标记

   <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" 
        onrowcommand="GridView1_RowCommand">
        <Columns>
            <asp:TemplateField HeaderStyle-Font-Bold="true" HeaderStyle-Font-Size="Larger" HeaderText="Activate/Delete"
                ItemStyle-Width="150px">
                <ItemTemplate>
                    <asp:LinkButton ID="linkbutton1" runat="server" Text="Activate" CommandName="Redirect"
                        CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>'></asp:LinkButton>
                    <span onclick="return confirm('Are You sure want to Delete?')">
                        <asp:LinkButton ID="linkbutton2" runat="server" Text="Delete" CommandName="Delete"></asp:LinkButton>
                    </span>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

我收到错误,指出以下行的输入字符串格式不正确:

    int rowIndex = int.Parse(Request.QueryString["RowIndex"]);

请帮助我。

输入字符串的格式不正确,无法检索 GridView 值

 if (e.CommandName == "Redirect")
    {
        String email = "lblemail.Text";
        String mob = "lblmobno.Text";
        Server.Transfer("activate.aspx?RowIndex=" + email +mob, true);
    }

在这里,您可以看到您正在将电子邮件和移动值作为查询字符串值发送,因此您的查询字符串如下所示

激活.aspx?RowIndex=lblemail.Textlblmobno.Text

当你得到行索引值时,它将返回lblemail。Textlblmobno.Text 作为值调试它并观察它,你会看到它,所以现在你要做的是从你正在使用的网格视图中传递 int 值

您也可以使用隐藏字段并从GridView1_RowCommand中找到它

我希望这对你有所帮助:)

这是因为您在 RowIndex 中发送文本/字符串,而作为 RowIndex,即用于访问 Rows 集合的索引应该是整数。你传递到rowindex的是'lblemail。Textlblmobno.Text',当你尝试将其解析为 int 时,它会抛出一个明显的错误。

更新:好的,你不需要传递行索引。

第一次更改:-

String email=lblEmail.Text;
String mob=lblMob.Text;

第二个变化:-

将查询字符串参数重命名为更有意义的全名(我已经在下面的代码中完成了,将 rowIndex 重命名为电子邮件)

第三个变化:-

protected void Page_Load(object sender, EventArgs e)
{
    String email= Request.QueryString["email"];
    String mobNumber= Request.QueryString["mob"];
    String yourThirdParameter = Request.QueryString["thirdOne"]; // Provide your third parameter.
    SqlDataAdapter da = new SqlDataAdapter("insert into login values('" + email + "','" + mob + "','" + yourThirdParameter  + "')",con);
// Assuming this is the sequence.you can change it your way.
    DataSet ds = new DataSet();
    da.Fill(ds);
}

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Redirect")
    {
        String email = lblemail.Text;
        String mob = lblmobno.Text;
        String thirdOne= // Fill it as is the need
        Server.Transfer("activate.aspx?email=" + email+"&mob=" +mob+"&thirdOne="+thirdOne, true);
    }
}

ALert:- 正如我怀疑如果您还必须传递密码,则不应使用查询字符串。同时传递电子邮件,查询字符串中的手机号码是不可取的。