我有一個頁面有一個gridview控件,列出項目信息,並有選項來詳細查看項目,也打開頁面編輯項目並保存結果作爲「克隆」。這工作正常,但現在我想添加一個按鈕到詳細視圖頁面克隆記錄,而不必返回到列表頁面並選擇克隆項目。asp.net c#需要重定向到另一個頁面值爲
工作列表頁上的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" />
按鈕不是表,網格,或其它控制組的內部。
這是如何具體不符合你的期望?它在哪裏失敗?重定向與記錄的標識符肯定是我會做的。然後在'Clone.aspx'頁面上使用查詢字符串中的標識符來獲取記錄並「克隆」它。 – David
如果將按鈕放入網格中,它將起作用。 –
@ j.v。在我想放置按鈕的詳細信息頁面上沒有網格。來自列表頁面的gridview控件只顯示一個需要複製到另一個頁面和控件類型的工作控件(希望有意義)。 –