2013-10-28 36 views
0

在我的網格視圖中,我希望當用戶點擊激活時,它會導航到activate.aspx頁面。我想從前一頁檢索三個值到activate.aspx頁面。我的代碼是:輸入字符串格式不正確以檢索gridview值

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"]); 

請幫助我。

+1

你爲什麼要通過電子郵件和暴民如rowIndex位置?除了電子郵件和暴民,你想傳遞給activate.aspx的三個值是什麼? – tariq

+0

什麼是'Request.QueryString [「RowIndex」]'int rowIndex = int.Parse(Request.QueryString [「RowIndex」]);' – Grundy

+0

'Request.QueryString [「RowIndex」]中的值是什麼' ? – Rohit

回答

0

這是因爲您在RowIndex中發送文本/字符串,而RowIndex則是用於訪問Rows集合的索引應該是整數。 你傳遞給rowindex的是'lblemail.Textlblmobno.Text',當你試圖將它解析爲int時,它會引發一個明顯的錯誤。

更新:好的,你不需要傳遞rowIndex。

第一個變化: -

String email=lblEmail.Text; 

String mob=lblMob.Text; 

第二個變化: -

重命名爲更meaningfull名字(我已經做到了在代碼下面改名的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); 

    } 
} 

ALert: - 因爲我懷疑如果你必須通過密碼以及那麼QueryString不應該被使用。 同時通過電子郵件,在QueryString手機號碼不建議。

+0

答案措辭不正確,「RowIndex應該是int」你是什麼意思? –

+0

你可以用例子來詳細說明你的答案嗎? – DK007

+0

我該如何解決這個錯誤。你可以用例子告訴我嗎?請幫助我 – DK007

0
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作爲一種價值調試它,看着它,你會看到它,現在你必須做的是隻傳遞int值從GridView中使用的是

您可以使用隱藏域也並從GridView1_RowCommand

我希望這將有助於你的問候找到它... :)