2016-09-15 69 views
0

我有一個頁面有一個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" /> 

按鈕不是表,網格,或其它控制組的內部。

+0

這是如何具體不符合你的期望?它在哪裏失敗?重定向與記錄的標識符肯定是我會做的。然後在'Clone.aspx'頁面上使用查詢字符串中的標識符來獲取記錄並「克隆」它。 – David

+0

如果將按鈕放入網格中,它將起作用。 –

+0

@ j.v。在我想放置按鈕的詳細信息頁面上沒有網格。來自列表頁面的gridview控件只顯示一個需要複製到另一個頁面和控件類型的工作控件(希望有意義)。 –

回答

1

我看到了一些問題。

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

有一個括號後失蹤>

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

而且

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

是一個普通的HTML腳本標籤中左右〜不會受到服務器替換

如果< %#Eval(「QuestionID」)%>返回像QUEST3123234這樣的字符串,您需要添加引號mar KS。如果它是一個數字,那麼它們是不需要的。

+0

我試着改變'localhost:53269'的本地環境,但仍然得到相同的結果,當前的詳細記錄被重新加載。 –

+0

你創建克隆記錄的代碼是什麼? – derloopkat

+0

我改變了按鈕,使用response.redirect後面的代碼(請參閱原始問題中的編輯,現在應用程序嘗試打開克隆頁面,但與過去的值有問題(我認爲)。 –

0

想通了。

Button控件:

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

感謝所有協助到罐子新手腦細胞。

相關問題