【问题标题】:C# - pass the user id from selected grid view column to another page using session variablesC# - 使用会话变量将用户 ID 从选定的网格视图列传递到另一个页面
【发布时间】:2018-01-20 03:24:24
【问题描述】:

我将在 PMUserList.aspx 中传递选定的用户 ID,并将其传递给 UserEdit.aspx。网格视图表数据来自 MySQL。 例如,我想编辑 id 为“15001”的用户。然后我点击同一行中的“编辑”按钮。我希望将“15001”传递给 UserEdit.aspx

outcome1

预期结果: expected outcome

数据库结构: user_id 是一个字符串。

PMUserList.aspx

   <asp:GridView ID="gvUserList" runat="server" AllowPaging="True" PageSize="5" SelectedIndex="0" >
                    <Columns>
                        <asp:BoundField DataField="user_id" HeaderText="User ID" SortExpression="id" />
                        <asp:BoundField DataField="name" HeaderText="Name" SortExpression="Name" />
                        <asp:BoundField DataField="contact_no" HeaderText="Contact no" SortExpression="Contact" />
                        <asp:BoundField DataField="email" HeaderText="Email" SortExpression="Email" />
                        <asp:BoundField DataField="role" HeaderText="Role" SortExpression="Role" />
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:Button ID="BtnEdit"
                                    runat="server"
                                    Text="Edit"
                                    ForeColor="Black"
                                    CommandArgument='<%# Eval("user_id") %>'
                                    OnCommand="BtnEdit_Command"
                                    OnClientClick='<%# "javascript:return confirm(\"Edit [" + Eval("user_id") + "]\");"  %>' />
                            </ItemTemplate>

PMUserList.aspx.cs

   protected void BtnEdit_Command(object sender, CommandEventArgs e)
    {
        Session["userid"] = String.Format("PMUserList?user_id={0}", e.CommandArgument);
        string url = String.Format("PMUserList?user_id={0}", e.CommandArgument);
        Response.Redirect("UserEdit.aspx");
    }

UserEdit.aspx.cs

     private void LoadData()
        {
            string userid = Session["userid"].ToString();*/
            string Query = "SELECT user_id, name, contact_no,email,role FROM mydb.User WHERE  user_id = " + userid + ";";
            .....}

错误信息(可以选择 '15001' 但通过 '?15001' ):

{"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?user_id=15001' at line 1"}

请帮助我改进会话变量。谢谢你。

【问题讨论】:

    标签: c# mysql asp.net


    【解决方案1】:

    您的会话变量正在存储查询字符串,而不仅仅是我想要的用户,因此您的 SQL 查询无效。要么只将用户 ID 存储在 btn_command 中,要么只从加载数据中的会话变量中获取我想要的用户并进行 SQL 查询

    【讨论】:

    • 将 String.Format("PMUserList?user_id={0}",e.CommandArgument) 更改为 String.Format("{0}",e.CommandArgument);
    • 非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-03
    • 2020-08-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多