Asp.net c#需要重定向到另一个有值的页面

本文关键字:另一个 net 重定向 Asp | 更新日期: 2023-09-27 18:07:04

我有一个页面,它有一个gridview控件,列出了项目信息,并有选项来详细查看项目,还打开一个页面来编辑项目,并将结果保存为"克隆"。这工作得很好,但是现在我想在详细视图页面添加一个按钮来克隆记录,而不必返回到列表页面并选择克隆项。

列表页上的gridview控件:

<asp:GridView ID="GridView1" runat="server" Caption="Submitted     Questions" AllowSorting="True"
CaptionAlign="Left" EmptyDataText="You have not submitted any Questions." PageSize="5" 
AutoGenerateColumns="false" AlternatingRowStyle-BackColor="#cccccc">
<Columns>
    <asp:TemplateField>
        <ItemTemplate>
            <asp:Label ID="QuestionID" runat="server" Text='<%# Eval("QuestionID") %>' />
        </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField DataField="KeyObjective" HeaderText="Key Objective" ItemStyle-Width="150" />
    <asp:BoundField DataField="SubmitDate" HeaderText="Submitted Date" ItemStyle-Width="50" />
    <asp:TemplateField>
        <ItemTemplate>
            <asp:LinkButton ID="Details" runat="server" Text="View Details" PostBackUrl='<%#"~/Submit/Detail.aspx?Id=" + Eval("QuestionID")  %>'></asp:LinkButton>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField>
        <ItemTemplate>
            <asp:LinkButton ID="Clone" runat="server" Text="Create Clone" PostBackUrl='<%# "~/Submit/Clone.aspx?Id=" + Eval("QuestionID")  %>'></asp:LinkButton>
        </ItemTemplate>
    </asp:TemplateField>
</Columns>
</asp:GridView>

当前对按钮控件的尝试:

删除了下面的javascript并移动到后面的代码:

按钮:

<asp:Button ID="CloneButton" runat="server" Text="Clone This Question" OnClick="CloneButton_Click" />

背后代码:

protected void CloneButton_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Submit/Clone.aspx?Id=" + txt_QuestionID);
    }

现在看起来像应用程序正在尝试打开克隆页面,但有解释Id传递值的问题?

克隆页面出现错误:收到的错误是"输入字符串格式不正确"。

using (Conn)
        {
            SqlCommand command = new SqlCommand("sp_CloneSElect", Conn);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@QuestionID", SqlDbType.BigInt));
            command.Parameters["@QuestionID"].Value = Convert.ToInt32(Request["Id"]);
            Conn.Open();
            SqlDataReader reader = command.ExecuteReader();

忽略此处不再使用(保存以参考已提交的注释)。

Javascript:

 <script type="text/javascript">
    function redirect(QuestionID) { location.href = '~/Submit/Clone.aspx?Id=' + QuestionID; }
 </script>

按钮:

<asp:Button ID="CloneButton" runat="server" OnClientClick='redirect(<%#Eval("QuestionID") %>; return false' Text="Clone Question" />

按钮不在表格、网格或其他控件集中。

Asp.net c#需要重定向到另一个有值的页面

我看到了几个问题。

OnClientClick='redirect(<%#Eval("QuestionID") %>; return false'

后面缺少圆括号

OnClientClick='redirect(<%#Eval("QuestionID") %>); return false'

location.href = '~/Submit/Clone.aspx?Id=' + QuestionID;

在一个普通的HTML脚本标签中,所以~不会被server

取代。

如果<%#Eval("QuestionID") %>返回类似QUEST3123234的字符串,则需要添加引号。如果是数字,则不需要。

我明白了

按钮控制:

<asp:Button ID="CloneButton" runat="server" Text="Clone This Question" OnClick="CloneButton_Click" />

背后的代码:

protected void CloneButton_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Submit/Clone.aspx?Id=" + txt_QuestionID.Text);
    }

感谢所有人帮助一个新生儿的脑细胞。