【问题标题】:input string is not in correct format for retrieveing gridview value输入字符串的格式不正确,无法检索 gridview 值
【发布时间】:2013-10-28 07:11:49
【问题描述】:

在我的网格视图中,我希望当用户单击激活时,它会导航到 activate.aspx 页面。我想从上一页检索三个值到 activate.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"]);

请帮帮我。

【问题讨论】:

  • 你为什么将 email 和 mob 作为 rowindex 传递?除了 email 和 mob,你想传递给 activate.aspx 的三个值是什么?
  • int rowIndex = int.Parse(Request.QueryString["RowIndex"]);中的Request.QueryString["RowIndex"]是什么
  • Request.QueryString["RowIndex"] 中的值是多少?
  • 他将 emial + mob number 作为 rowIndex 传递
  • 我将电子邮件和 mob 作为行索引传递,因为我想在下一页中检索它们,即 activate.aspx。我想将这些值存储在名为 login 的数据库表中,该表具有三列 username、pwd 和mob_no。在activate.aspx的加载事件中

标签: c# asp.net gridview


【解决方案1】:
 if (e.CommandName == "Redirect")
    {
        String email = "lblemail.Text";
        String mob = "lblmobno.Text";
        Server.Transfer("activate.aspx?RowIndex=" + email +mob, true);

    }

在这里您可以看到您将电子邮件和移动值作为查询字符串值发送,因此您的查询字符串看起来像这样

activate.aspx?RowIndex=lblemail.Textlblmobno.Text

当你获得 rowindex 值时,它会返回 lblemail.Textlblmobno.Text 作为值 调试它并观察它,你会看到它,所以现在你必须做什么是从你正在使用的gridview中传递int值

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

希望这对您有所帮助...:)

【讨论】:

    【解决方案2】:

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

    更新:好的,你不需要传递 rowIndex。

    第一个变化:-

    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);
    
        }
    }
    

    警报:- 我怀疑如果您还必须传递密码,则不应使用 QueryString。 也不建议在 QueryString 中传递电子邮件、手机号码。

    【讨论】:

    • Answer 措辞不正确,“RowIndex 应该是 int” 你这是什么意思?
    • 您能否举例说明您的答案。
    • 我该如何解决这个错误。你能举个例子吗?请帮助我
    • 您需要通过电子邮件、mob 和 ?是密码吗?以及这些在 sql 命令中的顺序是什么?
    • 此代码将 lblemail.text、lblmobno.text 存储到表中,而不是实际值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-10
    • 2021-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-27
    相关资源
    最近更新 更多